在单击打开/保存对话框之前等待几分钟时,文件下载不完整

时间:2021-04-30 20:04:29

I am working on a JSF based web application that provides open/save dialog for the user to download an XML file. If user clicks open/save immediately once open/save dialog appears, the file is downloaded completely. But if there is a delay of more than 90 seconds to click open/save after the dialog appears, the downloaded contents are incomplete.

我正在开发一个基于JSF的Web应用程序,它为用户提供打开/保存对话框以下载XML文件。如果用户在出现打开/保存对话框后立即单击打开/保存,则会完全下载该文件。但是,如果在对话框出现后单击打开/保存有超过90秒的延迟,则下载的内容不完整。

Below is my code snippet.

下面是我的代码片段。

Student student  = getStudentData();
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

response.reset();
response.setContentType("application/xml");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment; filename=\"studentData.xml\"");
jaxbMarshaller.marshal(student, response.getWriter());

Any suggestions on how to fix this?

对于如何解决这个问题,有任何的建议吗?

UPDATE 1: I tried the temp file approach suggested as the data is large. The temp file is created with proper contents. But the original problem still persists. Below is my code snippet. Am I missing anything?

更新1:我尝试了建议的临时文件方法,因为数据很大。使用适当的内容创建临时文件。但最初的问题仍然存在。下面是我的代码片段。我错过了什么吗?

student  = getStudentData();
File tmpFile = new File("C:\\studentDataTmp.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
jaxbMarshaller.marshal(student, tmpFile);

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

response.reset();
response.setContentType("application/xml");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment; filename=\"studentData.xml\"");
InputStream input = new FileInputStream(tmpFile);
OutputStream output = response.getOutputStream();
ByteStreams.copy(input, output); //ByteStreams from Google guava

facesContext.responseComplete();
//Close the streams

1 个解决方案

#1


0  

The issue is fixed after adding JVM option -Dcom.sun.grizzly.writeTimeout=300000. Refer Impact of grizzly.writeTimeout in file download for more details.

添加JVM选项-Dcom.sun.grizzly.writeTimeout = 300000后,问题得以解决。有关更多详细信息,请参阅文件下载中的grizzly.writeTimeout的影响。

#1


0  

The issue is fixed after adding JVM option -Dcom.sun.grizzly.writeTimeout=300000. Refer Impact of grizzly.writeTimeout in file download for more details.

添加JVM选项-Dcom.sun.grizzly.writeTimeout = 300000后,问题得以解决。有关更多详细信息,请参阅文件下载中的grizzly.writeTimeout的影响。