为了账号安全,请及时绑定邮箱和手机立即绑定

通过SERVLET下载,但是遇到了无法下载的问题。

通过SERVLET下载,但是遇到了无法下载的问题。

叮当猫咪 2023-04-09 17:13:57
我在用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贡献1155条经验 获得超0个赞

1,导出之前加response.reset(). 
2,导出之后 加return;试试。

查看完整回答
反对 回复 2023-04-12
?
慕尼黑的夜晚无繁华

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);  


你都没有控制文件读写结束循环控制,这样能下载全部的文件内容嘛,加上什么时候读写文件结束判断


查看完整回答
反对 回复 2023-04-12
  • 2 回答
  • 0 关注
  • 144 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信