|
|
|
@ -26,6 +26,8 @@ import jakarta.servlet.http.HttpServletResponse; |
|
|
|
import java.io.*; |
|
|
|
import java.net.URL; |
|
|
|
import java.net.URLConnection; |
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.List; |
|
|
|
@ -264,39 +266,39 @@ public class EamObjectServiceImpl implements EamObjectService { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void downLoadObjectFile(Integer id, HttpServletResponse response) throws UnsupportedEncodingException { |
|
|
|
//处理路径和名称 |
|
|
|
public void downLoadObjectFile(Integer id, HttpServletResponse response) { |
|
|
|
List<SysOssEntity> getFileData = eamObjectMapper.getFileData(id); |
|
|
|
if (getFileData.isEmpty()) { |
|
|
|
throw new RuntimeException("该文件不存在,请刷新列表"); |
|
|
|
} |
|
|
|
File file = new File(getFileData.get(0).getUrl()); |
|
|
|
//读取缓存1kb |
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
FileInputStream fis = null; |
|
|
|
BufferedInputStream bis = null; |
|
|
|
//FileOutputStream fos = null; |
|
|
|
//BufferedOutputStream bos = null; |
|
|
|
response.setContentType("application/force-download;charset=utf-8"); |
|
|
|
response.setHeader("Content-disposition", "attachment; filename=" + new String(getFileData.get(0).getFileName().getBytes("gbk"), "iso8859-1")); |
|
|
|
File file = new File(getFileData.getFirst().getUrl()); |
|
|
|
if (!file.exists()) { |
|
|
|
throw new RuntimeException("文件不存在"); |
|
|
|
} |
|
|
|
// 核心:先 reset,清空所有可能的 header |
|
|
|
response.reset(); |
|
|
|
response.setContentType("application/octet-stream"); |
|
|
|
response.setCharacterEncoding("UTF-8"); |
|
|
|
try { |
|
|
|
//读取文件 |
|
|
|
fis = new FileInputStream(file); |
|
|
|
//加缓存 |
|
|
|
bis = new BufferedInputStream(fis); |
|
|
|
//开始读取 |
|
|
|
int i = bis.read(buffer); |
|
|
|
//设置输出流 |
|
|
|
ServletOutputStream os = response.getOutputStream(); |
|
|
|
//开始循环读取 |
|
|
|
while (i != -1) { |
|
|
|
os.write(buffer, 0, i); |
|
|
|
i = bis.read(buffer); |
|
|
|
String fileName = URLEncoder.encode(getFileData.getFirst().getFileName(), StandardCharsets.UTF_8).replaceAll("\\+", "%20"); |
|
|
|
response.setHeader( |
|
|
|
"Content-Disposition", |
|
|
|
"attachment; filename*=UTF-8''" + fileName |
|
|
|
); |
|
|
|
try ( |
|
|
|
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); |
|
|
|
ServletOutputStream os = response.getOutputStream() |
|
|
|
) { |
|
|
|
byte[] buffer = new byte[8192]; |
|
|
|
int len; |
|
|
|
while ((len = bis.read(buffer)) != -1) { |
|
|
|
os.write(buffer, 0, len); |
|
|
|
} |
|
|
|
os.flush(); |
|
|
|
} |
|
|
|
fis.close(); |
|
|
|
//fos.close(); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException(e.getMessage()); |
|
|
|
// 不能再写 response 了 |
|
|
|
throw new RuntimeException("文件下载失败", e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|