日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java.net.SocketException: Broken pipe问题解决

發布時間:2025/3/17 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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问题解决的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。