使用“空响应,从服务器接收到空响应”的JSF错误中的IOUtils下载zip文件 [英] Downloading zip file with IOUtils in JSF errors with "Empty response, an empty response was recieved from the server"

查看:121
本文介绍了使用“空响应,从服务器接收到空响应”的JSF错误中的IOUtils下载zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的jsf bean中使用Java从Web服务器下载zip文件。我的代码适用于JPEG,但不适用于ZIP。这是我的代码。

  private void createDownloadFile(final URL downloadUrl,final String mimeType){
final FacesContext fc = FacesContext.getCurrentInstance();
final ExternalContext context = fc.getExternalContext();
final HttpServletResponse response =(HttpServletResponse)context.getResponse();
response.setContentType(mimeType);
response.addHeader(Content-Disposition,attachment; filename = \+ downloadUrl +\);

try {
final OutputStream out = response.getOutputStream();
IOUtils.copy(downloadUrl.openStream(),out);
fc.responseComplete();
} catch(final IOException exc){
exc.printStackTrace();
}

}

这是错误:空响应,从服务器收到一个空的响应。任何帮助不胜感激。

解决方案


空的回复,从服务器。


此消息可识别为Chrome中的XML解析错误; XML解析器找不到请求的XML根元素。结合您尝试的任何操作,这表示您尝试通过JavaScript / Ajax下载该文件。您无法执行此操作是因为JavaScript由于明显的安全原因而无法强制使用保存在变量中的文件内容的另存为对话框。



此错误消息是因为JSF ajax响应被定义为返回特定的XML格式,< partial-response>< update> 等。但是如果在JSF ajax响应中缺少< partial-response> 根元素,则浏览器特定的XML解析器将/可能显示这种错误。



您需要通过同步请求下载文件。删除< f:ajax> 或将第三方组件的任何ajax-toggling属性设置为 false ,例如作为< p:commandButton ajax =false>



您不需要担心浏览器中的页面更改,因为您设置了 Content-Disposition:附件响应头,当前页面将保持不变。



另请参见:




I am downloading zip file from web server using Java in my jsf bean. My code works for JPEG well but not for ZIP. Here is my code.

private void createDownloadFile(final URL downloadUrl, final String mimeType) {
    final FacesContext fc = FacesContext.getCurrentInstance();
    final ExternalContext context = fc.getExternalContext();
    final HttpServletResponse response = (HttpServletResponse) context.getResponse();
    response.setContentType(mimeType);
    response.addHeader("Content-Disposition", "attachment; filename=\"" + downloadUrl + "\"");

    try{
        final OutputStream out = response.getOutputStream();
        IOUtils.copy(downloadUrl.openStream(), out);
        fc.responseComplete();
    }catch (final IOException exc){
      exc.printStackTrace();
    }

}

And this is the error: Empty response, an empty response was recieved from the server. Any help is appreciated.

解决方案

Empty response, an empty response was recieved from the server.

This message is recognizeable as a XML parsing error in Chrome; the XML parser couldn't find the requested XML root element. In combination with whatever you're trying to do, this suggests that you were trying to download the file by JavaScript/Ajax. You cannot do this because JavaScript has due to obvious security reasons no facilities to force a Save As dialog with file content which is been hold in a variable.

This error message is caused because JSF ajax responses are definied to return a specific XML format, <partial-response><update>, etc. But if the <partial-response> root element is missing in the JSF ajax response, then the browser-specific XML parser will/may show this kind of error.

You need to download the file by a synchronous request instead. Remove the <f:ajax> or set whatever ajax-toggling attribute of the 3rd party component to false, such as <p:commandButton ajax="false">.

You don't need to worry about a page change in the browser, as you've set the Content-Disposition: attachment response header, the current page will just remain the same. This problem is in no way specific to Commons IO, by the way.

See also:

这篇关于使用“空响应,从服务器接收到空响应”的JSF错误中的IOUtils下载zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆