java IO流下载.docx文件时报文件损坏的错误,求解?

努力 努力 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
用java IO流从数据库中读取文件下载,以前下载.doc,.xls都没有问题。但是发现.docx. xlsx下载时,出现了文件损坏的提示。如下图:

点确定后,又弹出,
我的代码如下:
String fileId = request.getParameter("fileId");
AttachFileVO attachFileVO = new AttachFileVO();
attachFileVO.setFileId(fileId);
AttachFileVO attachFile = applicationService.getAttachFile(attachFileVO);
String fileName = attachFile.getFileName();
String formatFileName = CommonUtil.encodingFileName(fileName);
if(attachFile!=null&&attachFile.getFileContent()!=null){
InputStream in = null;
OutputStream os = null;
try{
response.reset();
response.setContentType(attachFile.getFileType());
response.setHeader("Content-Disposition", "attachment;filename="+formatFileName);

in = new ByteArrayInputStream(attachFile.getFileContent());
byte[] buffer = new byte[1024];
os = response.getOutputStream();
while (in.read(buffer) > 0) {
os.write(buffer);
}
}catch(Exception ex){
throw ex;
}finally{
if(in!=null){
in.close();
}
if(os!=null){
os.close();
}
}
return null;
}
...全文
评论 12
收藏 已收藏
分享
返回顶部
努力
Web 开发 81,107 成员 341,720 内容