【问题标题】:Custom download servlet自定义下载 servlet
【发布时间】:2012-01-24 20:28:32
【问题描述】:

我查看了 BalusC 的代码,用于从绝对路径自定义下载 servlet(请参阅 http://balusc.blogspot.com/2007/07/fileservlet.html#FileServletServingFromAbsolutePath)。我不是 Java Web 开发专家,所以如果有人可以解释这部分代码,我会很高兴

private String filePath;

// Actions ------------------------------------------------------------------------------------

public void init() throws ServletException {

    // Define base path somehow. You can define it as init-param of the servlet.
    this.filePath = "/files";

    // In a Windows environment with the Applicationserver running on the
    // c: volume, the above path is exactly the same as "c:\files".
    // In UNIX, it is just straightforward "/files".
}

什么时候调用 init 方法?为什么我们需要在init方法中设置filePath?

我有一个 XHTML (Mojarra+IceFaces),其中的代码类似于下面的代码,效果很好。我的页面缺少下载 outputLink 标签引用的文件的部分

                <ice:tree id="tree"
                          value="#{treeBean.model}"
                          var="item"
                          hideRootNode="false"
                          hideNavigation="false"
                          >
                    <ice:treeNode>
                        <f:facet name="icon">
                            <ice:panelGroup style="display: inline">
                                <h:graphicImage value="#{item.userObject.icon}" />
                            </ice:panelGroup>
                        </f:facet>
                        <f:facet name="content">
                            <ice:panelGroup style="display: inline-block">
                                <ice:outputLink value="#{item.userObject.filePath}">
                                    <ice:outputText value="#{item.userObject.fileName}"/>
                                </ice:outputLink>
                            </ice:panelGroup>
                        </f:facet>
                    </ice:treeNode>
                </ice:tree>

在我的支持 bean 中,我有两个字段 fileName(只是带有扩展名的文件的名称,例如 Image.jpeg)和 filepath(服务器中文件的绝对路径)。最后我想用自定义的servlet下载文件,我该怎么做??

干杯,

更新

假设 mi base-dir 是 /SRC 并且在该目录下我有我所有的 xhtml 页面以及 WEB-INF 和 META-INF,另外我在 dataFiles 下还有一个名为 dataFiles 的目录有以下结构

  --dataFiles
  |----Enterprise1
  |    |--User1
  |    |   |--goodFiles
  |    |   |  |--ok.txt
  |    |   |--badFiles
  |    |      |--bad.txt
  |    |--User2 
  |    |   |--goodFiles
  |    |   |  |--ok.txt
  |    |   |--badFiles
  |    |      |--bad.txt
  |----Enterprise2
       |--User1
       |   |--goodFiles
       |   |  |--ok.txt
       |   |--badFiles
       |      |--bad.txt
       |--User2 
           |--goodFiles
           |  |--ok.txt
           |--badFiles
              |--bad.txt

这就是我用 IceFaces 渲染树的方式,我只有在支持 bean 中的文件名(即 ok.t​​xt 或 bad.txt),但我不知道如何下载树中链接指向的文件。

【问题讨论】:

    标签: jsf servlets download


    【解决方案1】:

    终于搞定了。

    首先感谢 BalusC,这里有一些帖子帮助我理解,但有人删除了它们。无论如何,这就是我所学到的。

    1. 在 Servlet 的 init 方法中,filePath 变量必须指向要下载的文件所在的绝对 路径.
    2. web.xml 中,当浏览器具有该 url 模式时,servlet-map-url-pattern 将导致执行 servlet。
    3. 在 xhtml 页面中,链接的值应以 url 模式开头,后跟名称(或路径+文件名),因此当您单击链接时,下载开始。

    这就是我所要做的!

    在问题的例子中,servlet的init方法中的filePath变量会指向绝对路径,类似于C:\myApp\dataFiles 然后在 xhtml 中将调用 servlet 类似的东西

    <ice:outputLink value="dl/myPath/#{myBean.fileName}>
        <ice:outputText value="#{myBean.fileName}"/>
    </ice:outputLink>
    

    注意1:outputLink的值的第一部分是dl/这是因为要下载的servlet的url-pattern映射到它

    注意 2:在 outputLink 中 myPath 的值可能是 dl/Enterprise1/User1/file1.ext

    干杯

    【讨论】:

    • 我自己删除了它,因为问题变得过于广泛和本地化,我再也看不到你真正的问题了。花更多的时间来弄清楚/猜测你的具体问题是不值得的。
    • 认为你是对的。但无论如何,谢谢你的建议,我解决了我的问题。
    猜你喜欢
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    相关资源
    最近更新 更多