使用RichFaces下载文件 [英] File download using RichFaces

查看:85
本文介绍了使用RichFaces下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成以下工作:


  1. 用户可以上传文件(即压缩档案)

  2. 用户可以解压缩服务器上的文件

  3. 用户可以对这些文件执行一些操作,这样可以生成更多文件

现在我需要让第4步工作:

Now I need to get step 4 to work:


  • 用户可以下载再次将文件发送到自己的计算机

任何人都可以给我一个提示吗?我试图了解我在谷歌上发现的东西,但它并没有像预期的那样发挥作用。我必须设置内容类型吗?当我设置应用程序/八位字节流时​​,只有txt和csv文件会正确显示(在浏览器中,而不是我想要的下载弹出窗口)其他文件无法正常工作...

Can anyone give me a hint? I tried to understand the stuff I found on Google, but it does not work quite as expected. Do I have to set a content type? When I set application/octet stream only txt and csv files would display correctly (in the browser, not as download popup as I wanted) other files would not work...

JSP:

<a4j:commandLink value="Download" action="#{appController.downloadFile}" rendered="#{!file.directory}">
   <f:param name="file" value="#{file.absoluteFilename}" />
</a4j:commandLink>

appController:

appController:

public String downloadFile() {
    String filename = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("file");
    File file = new File(filename);
    HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  

    writeOutContent(response, file, file.getName());

    FacesContext.getCurrentInstance().responseComplete();
    return null;
}

private void writeOutContent(final HttpServletResponse res, final File content, final String theFilename) {
    if (content == null) {
        return;
    }
    try {
        res.setHeader("Pragma", "no-cache");
        res.setDateHeader("Expires", 0);
        res.setHeader("Content-disposition", "attachment; filename=" + theFilename);
        FileInputStream fis = new FileInputStream(content);
        ServletOutputStream os = res.getOutputStream();
        int bt = fis.read();
        while (bt != -1) {
            os.write(bt);
            bt = fis.read();
        }
        os.flush();
        fis.close();
        os.close();
    } catch (final IOException ex) {
        Logger.getLogger(ApplicationController.class.getName()).log(Level.SEVERE, null, ex);
    }
}


推荐答案

您的具体问题是你试图通过Ajax下载文件。这是不正确的。 JavaScript无法处理二进制响应,也没有任何强制另存为对话的工具。您需要将其设置为正常的同步请求,以便它是必须处理它的webbrowser本身。

Your concrete problem is that you're attempting to download files by Ajax. This is not correct. JavaScript can't deal with binary responses nor has it any facilities to force a Save As dialogue. You need to make it a normal synchronous request instead so that it's the webbrowser itself who has to deal with it.

<h:commandLink value="Download" action="#{appController.downloadFile}" rendered="#{!file.directory}">
   <f:param name="file" value="#{file.absoluteFilename}" />
</h:commandLink>

关于设置内容类型,如果你手边有一个扩展名的文件名,你可以使用 ServletContext#getMimeType()中根据< mime-mapping> 解决它web.xml (服务器的默认值或webapp的一个)。

As to setting the content type, if you have a file name with extension at your hands, you could use ServletContext#getMimeType() to resolve it based on <mime-mapping> in web.xml (either the server's default one or your webapp's one).

ServletContext servletContext = (ServletContext) externalContext.getContext();
String contentType = servletContext.getMimeType(file.getName());

if (contentType == null) {
    contentType = "application/octet-stream";
}

response.setContentType(contentType);
// ...

(请注意,我认为你是使用JSF 1.x,看看你如何获得servlet响应,你可以使用JSF 2.x,否则也使用 ExternalContext#getMimeType()

这篇关于使用RichFaces下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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