我在用servlet写了一个下载功能,先把文件打包成RAR,然后通过SERVLET下载,但遇到了无法下载的问题。代码是:public void exportDown(HttpRequest request, HttpServletResponse response, User user) throws ServletException, IOException { ExportLogApp app = new ExportLogApp( "from Test1 where 1=1 and id <1001 "); int i = app.exportDownRAR(user.getId(), null, null); if (i == app.LOG) { user.addLog("数据交换", "导出下载数据", "用户[" + user.getId() + "]导出数据成功!"); String filePath = app.getRARpath_save() + File.separator + app.getRARname_save() + ".rar"; filePath = filePath.replaceAll("/", "\\\\"); // 定义输出类型(下载) response.setContentType("application/force-download"); // 定义输出文件头 response.setHeader("Content-Disposition", "attachment;filename=\"" + app.getRARname_save() + ".rar\""); File file = new File(filePath); int len = (int) file.length(); byte[] buf = new byte[len]; FileInputStream fis = new FileInputStream(file); OutputStream out = response.getOutputStream(); len = fis.read(buf); out.write(buf, 0, len); out.flush(); out.close(); fis.close(); file.delete(); } else if (i == app.LOG_ERROR) { user.addLog("数据交换", "导出下载数据", "用户[" + user.getId() + "]导出数据失败!"); PrintWriter out = response.getWriter(); out.println("<script language='javascript'>"); out.println("alert('导出数据出错,请联系管理员!');"); out.println("window.location.href='../expert/expert_account.jsp';"); out.println("window.close()"); out.println("</script>"); } else if (i == app.NO_DATA) { } } 请帮我解答下,为什么会这样,我测试用的浏览器是IE7.
2 回答
慕尼黑的夜晚无繁华
TA贡献1864条经验 获得超6个赞
byte[] buf = new byte[len];
FileInputStream fis = new FileInputStream(file);
OutputStream out = response.getOutputStream();
len = fis.read(buf);
out.write(buf, 0, len);
你都没有控制文件读写结束循环控制,这样能下载全部的文件内容嘛,加上什么时候读写文件结束判断
添加回答
举报
0/150
提交
取消