我在用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
提交
取消