2012-01-24 59 views
0

我从绝对路径中查看了BalusC的定制下载servlet的代码(请参阅http://balusc.blogspot.com/2007/07/fileservlet.html#FileServletServingFromAbsolutePath)。我不是一个Java Web开发专家,所以我会很喜欢,如果什么时候init方法被调用有人能explainme代码自定义下载servlet

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方法中设置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中我有两个字段fileNamefilepath(只是在filewith扩展名,比如Image.jpeg的名称)的文件只是部分(服务器中文件的ABSOLUTE路径)。最后我想用自定义servelet下载文件,我该怎么做?

干杯,

UPDATE

假设MI基础-dir是/SRC和目录下,我有我所有的XHTML页面和WEB-INF和META-INF和方法,另外我有一个目录所谓数据文件下的数据文件,我有以下结构

--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.txt或bad.txt),但我无法弄清楚如何通过树中的链接下载指向的文件。

回答

0

好了,终于搞定了。

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

  1. 在Servlet的初始化方法文件路径变量必须指向绝对所在的路径要下载的文件是。
  2. web.xml中在servlet-地图的url-pattern会导致执行servlet当浏览器有一个URL模式。
  3. 在链接的XHTML页面THA值应与url-pattern的依次是姓名(或路径+文件名),所以你点击下载开始链接时开始。

这就是我不得不这样做!

在问题的例子,在servlet的init方法的文件路径变量将指向绝对路径,像C:\对myApp \数据文件 然后在XHTML将调用servlet的东西,如

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

注1:表示outputLink的值的第一部分是DL/这是因为为servlet到下载链接模式被映射到它

注2:在outputLink的所述的价值 mypath中可能是DL/Enterprise1 /用户1/file1.ext

干杯

+0

我因为这个问题成了方式过于宽泛和本地化的,我没有看到你真正的问题了自己删除了。仅仅花费更多的时间来计算/猜测你的具体问题是不值得的。 – BalusC

+0

认为你是对的。但是,无论如何,在您的建议下我解决了我的问题。 – BRabbit27