java.net.SocketException: Broken pipe问题解决
2019獨角獸企業重金招聘Python工程師標準>>>
javax.servlet.ServletException:?ClientAbortException:??java.net.SocketException:?Broken?pipe錯誤完美解決
現在百度太垃圾了,啥都搜不出來,啥時候才能上google,簡直成一個夢想了
?
用chrome跟蹤發現下載文件時出現如下錯誤:
錯誤 349 (net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION):我們收到了多個“Content-Disposition”標頭。我們不允許此行為,以防遭到 HTTP 響應拆分攻擊。
?
原代碼為:
public String downloadFile() throws Exception {
??HttpServletResponse response = ServletActionContext.getResponse();
??HttpSession session = HttpRequester.getHttpSession();
??Object su = session.getAttribute("ACCOUNT");
??if (su == null) {
???System.out.println("session超時");
???throw new Exception("session超時");
??}
??taskResultExcel = downloadFile.substring(downloadFile.lastIndexOf("/")+1, downloadFile.length());
??response.setContentType("application/vnd.ms-excel");
??response.setHeader("Content-Disposition", "attachment;filename=\""+ taskResultExcel + "\"");
??response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
??response.setHeader("Pragma", "public");
??return SUCCESS;
?}
?
?
修改為如下代碼即可解決
Caused by: ClientAbortException:? java.net.SocketException: Broken pipe
?
?
public String download() {
??HttpServletResponse response = getResponse();
??String flag = SUCCESS;
??log.info("下載的文件為: " + downloadFile);
??????? try {
??????????? String filePath2 = ServletActionContext.getServletContext().getRealPath("/"); //取當前系統路徑
??????????? // path是指欲下載的文件的路徑。在path 路徑下創建名為
//??????????? File file = new File(filePath2 + "zjh/下載.xls");
??????????? File file = new File(filePath2 + downloadFile);
??????????? if (!file.exists()){
??????????? ?log.info("文件沒有找到:" + downloadFile);
??????????????? flag = null;
??????????? }
??????????? // 取得文件名。
??????????? String filename = file.getName();
??????????? // 取得文件的后綴名。
??????????? String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
??????????? // 以流的形式下載文件。
//??????????? InputStream fis = new BufferedInputStream(new FileInputStream(filePath2 + "zjh/下載.xls"));
??????????? InputStream fis = new BufferedInputStream(new FileInputStream(new File(filePath2 + downloadFile)));
??????????? byte[] buffer = new byte[fis.available()];
??????????? fis.read(buffer);
??????????? fis.close();
??????????? // 清空response
??????????? taskResultExcel = downloadFile.substring(downloadFile.lastIndexOf("/")+1, downloadFile.length());
??????????? log.info("頁面文件為: " + taskResultExcel);
??????????? response.reset();
??????????? response.addHeader("Content-Disposition", "attachment;filename="+ java.net.URLEncoder.encode(taskResultExcel,"utf-8"));
??????????? response.addHeader("Content-Length", "" + file.length());
??????????? OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
??????????? response.setContentType("application/octet-stream");
??????????? toClient.write(buffer);
??????????? toClient.flush();
??????????? toClient.close();
??????? } catch (IOException ex) {
??????????? log.info("下載文件出現異常" + ex.getMessage());
??????????? flag = null;
??????? }
??????? //??????? return response;
??????? return flag;
??? }
轉載于:https://my.oschina.net/nixuewen/blog/348110
總結
以上是生活随笔為你收集整理的java.net.SocketException: Broken pipe问题解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql format 格式化 返回值
- 下一篇: 事件EVENT与waitforsingl