亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過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 關注
  • 246 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號