如何修复java.io.IOException:下载文件时,远程主机强行关闭了现有连接

问题描述 投票:2回答:1

我正在尝试在我的应用程序中下载文件,但是引发了以下异常:

org.apache.catalina.connector.ClientAbortException:java.io.IOException:现有连接被强制关闭远程主机

我已经阅读了关于SO的大多数相关问题,特别是this one,但是没有一个问题帮助我解决了此异常。

代码:

response.setContentType(mimeType);
response.setContentLength((int) file.length());
response.addHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");

InputStream inputStream = new FileInputStream(file);
OutputStream responseOutPutStream = response.getOutputStream();

byte buffer[] = new byte[32 * 1024];
int bytes;
while ((bytes = inputStream.read(buffer)) != -1) {
    responseOutPutStream.write(buffer, 0, bytes);
}
responseOutPutStream.flush();
inputStream.close();
responseOutPutStream.close();

[mimeTypeapplication/octet-stream

此外,我使用FileCopyUtils.copy(inputStream, responseOutPutStream);实现了此代码,但结果相同。

该请求是使用<a>标签从ExtJS应用程序发送的。

我们试图通过Ajax发送一个Get请求,但这并不能解决问题。

自从我进行搜索和调试以来已经有将近2天的时间,找不到解决此问题的任何方法。

如何解决此问题?

最好的问候


编辑:

更多信息:我使用Spring Boot作为底层框架,并且上面的代码在控制器类中调用(用@Controller注释注释)

java spring download ioexception
1个回答
0
投票

我遇到了同样的问题,认为这是我修改了实现的响应超时,替换了

OutputStream responseOutPutStream = response.getOutputStream();

with

尝试(PrintWriter pw = response.getWriter()){//您的逻辑在这里}

以这种方式工作。

© www.soinside.com 2019 - 2024. All rights reserved.