|
|
|
@ -26,6 +26,7 @@ import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.*; |
|
|
|
import java.net.URL; |
|
|
|
import java.net.URLConnection; |
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.List; |
|
|
|
@ -264,42 +265,60 @@ 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()) { |
|
|
|
if (getFileData == null || getFileData.isEmpty()) { |
|
|
|
throw new RuntimeException("该文件不存在,请刷新列表"); |
|
|
|
} |
|
|
|
File file = new File(getFileData.get(0).getUrl()); |
|
|
|
//读取缓存1kb |
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
FileInputStream fis = null; |
|
|
|
|
|
|
|
SysOssEntity fileEntity = getFileData.get(0); |
|
|
|
File file = new File(fileEntity.getUrl()); |
|
|
|
if (!file.exists()) { |
|
|
|
throw new RuntimeException("文件不存在"); |
|
|
|
} |
|
|
|
|
|
|
|
// ⭐ 非常关键:清空所有已存在的 header |
|
|
|
response.reset(); |
|
|
|
|
|
|
|
response.setContentType("application/octet-stream"); |
|
|
|
response.setCharacterEncoding("UTF-8"); |
|
|
|
|
|
|
|
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")); |
|
|
|
ServletOutputStream os = null; |
|
|
|
|
|
|
|
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); |
|
|
|
// Java 8 安全写法 |
|
|
|
String fileName = URLEncoder.encode(fileEntity.getFileName(), "UTF-8") |
|
|
|
.replaceAll("\\+", "%20"); |
|
|
|
|
|
|
|
response.setHeader( |
|
|
|
"Content-Disposition", |
|
|
|
"attachment; filename*=UTF-8''" + fileName |
|
|
|
); |
|
|
|
|
|
|
|
bis = new BufferedInputStream(new FileInputStream(file)); |
|
|
|
os = response.getOutputStream(); |
|
|
|
|
|
|
|
byte[] buffer = new byte[8192]; |
|
|
|
int len; |
|
|
|
while ((len = bis.read(buffer)) != -1) { |
|
|
|
os.write(buffer, 0, len); |
|
|
|
} |
|
|
|
fis.close(); |
|
|
|
//fos.close(); |
|
|
|
os.flush(); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException(e.getMessage()); |
|
|
|
// ⚠️ 这里千万不要再动 response |
|
|
|
throw new RuntimeException("文件下载失败", e); |
|
|
|
} finally { |
|
|
|
try { |
|
|
|
if (bis != null) bis.close(); |
|
|
|
if (os != null) os.close(); |
|
|
|
} catch (IOException ignored) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void downLoadObjectFile2(Integer id,String orderRef3, HttpServletResponse response) throws UnsupportedEncodingException { |
|
|
|
//处理路径和名称 |
|
|
|
|