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

努力 2015-01-05 10:06:30
用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;
}
...全文
3023 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_41534479 2018-11-21
  • 打赏
  • 举报
回复
厉害了,这种错误,如果不细心,真的找不找,感谢
xiaohe_1572 2016-11-01
  • 打赏
  • 举报
回复
这个问题解决么?分享一下解决方案吧
kay332716 2016-01-22
  • 打赏
  • 举报
回复 1
in = new ByteArrayInputStream(attachFile.getFileContent()); byte[] buffer = new byte[1024]; os = response.getOutputStream(); int len = 0; while ((len = in.read(buffer)) i= 0) { os.write(buffer, 0, len); } 这个是很有用的len = in.read(buffer) 读多少位写多少。不会出现文件损坏的情况!!!
飞不起的鱼 2015-01-06
  • 打赏
  • 举报
回复
引用 5 楼 lg312200538 的回复:
[quote=引用 4 楼 haohao00789 的回复:] 把下面那段改成这样看看。

                in = new ByteArrayInputStream(attachFile.getFileContent());
                byte[] buffer = new byte[1024];
                os = response.getOutputStream();
                int len = 0;
                while ((len = in.read(buffer)) > 0) {
                    os.write(buffer, 0, len);
                }          
这个是对的,十分感谢。但是能解释一下这两行代码的意思吗?[/quote] sorry,现在才回复,os.write(buffer, 0, len);这个的意思是将buffer里面0~len的字节写到输出流里面,下载的文件转换成字节流的字节数一般是不能被1024整除的,当输出最后一部分字节的时候,可能没有1024个字节,那么buffer字节数组里多出来的位置就是初始值0,这些也写到输出流里面去了,也就是多写了很多个值为0的字节进去,所以格式不对。 sorry,语言组织不好,见谅。
努力 2015-01-05
  • 打赏
  • 举报
回复
引用 1 楼 rui888 的回复:
你 excel 版本问题不?换个版本试试。 现在的版本 docx 估计 不兼容。
应该不会不兼容吧,.docx都出来很多年了。
tony4geek 2015-01-05
  • 打赏
  • 举报
回复
你 excel 版本问题不?换个版本试试。 现在的版本 docx 估计 不兼容。
默默的前进 2015-01-05
  • 打赏
  • 举报
回复
write 会将缓冲数组全部写入进去的,而一个byte数组值默认是0,但是0也可以表示一个字节,所以当你的byte数组是空的时候也会写入文件中,也会导致文件打不开字节错乱的问题, 建议 要么用 in.available() 这个来创建数组长度,要么就用楼上的那个方法,指定从数组的位置结束填充
默默的前进 2015-01-05
  • 打赏
  • 举报
回复
每次填充往缓冲数组中填充1024字节,当循还到最后一次的时候流中已经没有1024个字节了,比如最后一次只有100个了,那么会填充缓冲数组的前100个位置字节,那么100以后位置的字节就是保留了上一次的字节,所以写入文件的时候,字节已经错乱了,所以你打开的时候就报错了咯
yanxing2012 2015-01-05
  • 打赏
  • 举报
回复
尝试每次读取1024个字节,写入buffer数组,如果少于1024,就会返回实际读取的字节,os.write(buffer);可能多了
努力 2015-01-05
  • 打赏
  • 举报
回复
引用 4 楼 haohao00789 的回复:
把下面那段改成这样看看。

                in = new ByteArrayInputStream(attachFile.getFileContent());
                byte[] buffer = new byte[1024];
                os = response.getOutputStream();
                int len = 0;
                while ((len = in.read(buffer)) > 0) {
                    os.write(buffer, 0, len);
                }          
这个是对的,十分感谢。但是能解释一下这两行代码的意思吗?
飞不起的鱼 2015-01-05
  • 打赏
  • 举报
回复
把下面那段改成这样看看。

                in = new ByteArrayInputStream(attachFile.getFileContent());
                byte[] buffer = new byte[1024];
                os = response.getOutputStream();
                int len = 0;
                while ((len = in.read(buffer)) > 0) {
                    os.write(buffer, 0, len);
                }          
努力 2015-01-05
  • 打赏
  • 举报
回复
有处理过这个问题的高人,支下招哟。不要让贴子沉了,顶起。

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧