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

时间:2013-11-20 06:35:57

标签: xml jsf jaxb download

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

以下是我的代码段。

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());

有关如何解决此问题的任何建议吗?

更新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 个答案:

答案 0 :(得分:0)

添加JVM选项-Dcom.sun.grizzly.writeTimeout = 300000后,问题得以解决。有关详细信息,请参阅Impact of grizzly.writeTimeout in file download