栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

java broken pipe

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java broken pipe

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;

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/906276.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号