引起:java.io.IOException:已建立的连接被主机中的软件中止

时间:2017-09-12 15:50:36

标签: java ios download

我正在尝试使用Spring RestTemplate从另一台服务器下载多个文件,点击JSF UI界面中的按钮。文件正确下载(意味着REST服务工作正常)。从Rest服务下载每个文件时,我需要将它们放在ZIP文件中。一旦下载了所有文件,用户就必须在浏览器页脚中看到1个zip文件作为正在下载的文件。

但我得到了Caused by: java.io.IOException: An established connection was aborted by the software in your host machine

我的示例代码如下:

public void downLoadFile() throws IOException {
String fileNameToDownload;
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext ec = fc.getExternalContext();
ec.responseReset();
String fileName = "MyDownloadFile.zip";
ec.setResponseContentType("application/zip");
ec.setResponseHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
OutputStream out = ec.getResponseOutputStream();
zos = new ZipOutputStream(out);

Iterator<String> it = fileNamesList.iterator();

//Iterate through all fileNames and download each file from REST service
while (it.hasNext()) {
    fileNameToDownload = (String) it.next();
    byte bytes[] = new byte[2048];

    //Makes REST WS call to get document content in byte[] and convert to InputStream
    MyResponseObject myResponseObject = SpringRestService.getDocumentByDocumentName(fileNameToDownload);
    byte[] fileBytes = myResponseObject.getFileContent();
    InputStream is = new ByteArrayInputStream(fileBytes);

    zos.putNextEntry(new ZipEntry(fileNameToDownload));
    int numBytesRead = is.read(bytes);
    while (numBytesRead > 0) {
        zos.write(bytes, 0, numBytesRead);
        numBytesRead = is.read(bytes);
    }
    is.close();
}
zos.closeEntry();

zos.flush();
out.flush();

zos.close();
out.close();

ec.getResponseOutputStream().flush();
ec.getResponseOutputStream().close();
fc.responseComplete();

}

0 个答案:

没有答案