【发布时间】: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.txt 或 bad.txt),但我不知道如何下载树中链接指向的文件。
【问题讨论】: