java 从 JSF 应用程序的任何 Web 浏览器强制另存为对话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2914025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:25:34  来源:igfitidea点击:

Forcing a save as dialogue from any web browser from JSF application

javajsf

提问by volvox

I've created a JSF application, and I want to embed a link in a page which when clicked causes the backing bean to marshall out some xml and force the opening of a save-as download dialogue box so the user can choose a location to save the file. I've already written the JAXB code.

我创建了一个 JSF 应用程序,我想在一个页面中嵌入一个链接,当点击该链接时,会导致支持 bean 编组一些 xml 并强制打开另存为下载对话框,以便用户可以选择一个位置保存文件。我已经编写了 JAXB 代码。

How is this done?

这是怎么做的?

Thanks

谢谢

回答by BalusC

Set the HTTP Content-Dispositionheader to attachment. This will pop a Save Asdialogue. You can do that using HttpServletResponse#setHeader(). You can obtain the HTTP servlet response from under the JSF hoods by ExternalContext#getResponse().

将 HTTPContent-Disposition标头设置为attachment. 这将弹出一个另存为对话框。您可以使用HttpServletResponse#setHeader(). 您可以通过ExternalContext#getResponse().

In JSF context, you only need to ensure that you call FacesContext#responseComplete()afterwards to avoid IllegalStateExceptions flying around.

在 JSF 上下文中,您只需要确保在FacesContext#responseComplete()之后调用以避免IllegalStateExceptions 飞来飞去。

Kickoff example:

开场示例:

public void download() throws IOException {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

    response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
    response.setContentType("application/xml"); // Check http://www.iana.org/assignments/media-types for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename.
    response.setHeader("Content-disposition", "attachment; filename=\"name.xml\""); // The Save As popup magic is done here. You can give it any filename you want, this only won't work in MSIE, it will use current request URL as filename instead.

    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        input = new BufferedInputStream(getYourXmlAsInputStream());
        output = new BufferedOutputStream(response.getOutputStream());

        byte[] buffer = new byte[10240];
        for (int length; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
    } finally {
        close(output);
        close(input);
    }

    facesContext.responseComplete(); // Important! Else JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
}

回答by Sjoerd

Use the content-disposition: attachmentHTTP header

使用content-disposition: attachmentHTTP 标头

回答by Septimiu

sometimes you need to force the writer to send the contents to the client by calling response.getWriter().flush();before closing the writer. This prompted the save as popup in my case.

有时您需要response.getWriter().flush();在关闭编写器之前通过调用来强制编写器将内容发送到客户端。在我的情况下,这提示了另存为弹出窗口。