自定义下载 servlet
我从绝对路径查看了 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>
在我的 Backing bean 中,我有两个字段 fileName
(只是带有扩展名的文件的名称,例如Image.jpeg)和 filepath
(服务器中文件的绝对路径)。最后我想用自定义servlet下载文件,我该怎么做?
干杯,
更新
假设我的基本目录是/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),但我不知道如何下载树中链接指向的文件。
I looked into BalusC's code for a custom download servlet from absolute path (see http://balusc.blogspot.com/2007/07/fileservlet.html#FileServletServingFromAbsolutePath). I'm not a Java Web Developer Expert so I would love if someone can explainme this part of the code
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".
}
When does the init method gets called? Why do we need the filePath to be set in the init method?
I have an XHTML (Mojarra+IceFaces) with something like the code below that works great. My page is missing just the part of downloading the file which is referenced by the outputLink
tag
<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>
In my Backing bean I have two fields fileName
(just the name of the filewith extension e.g. Image.jpeg) and filepath
(the ABSOLUTE path of the file in the server). Finally I want to download the file with the custom servelet, how can I do that??
Cheers,
UPDATE
Let's say mi base-dir is /SRC
and under that dir I have all my xhtml pages and the WEB-INF and META-INF and aditionally I have a dir called dataFiles under dataFiles I have the following structure
--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
that's how I render the tree with IceFaces and I just have the filename in the backing bean (i.e. ok.txt or bad.txt) but I cannot figure out how to download the file pointing by the link in the tree.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
终于成功了。
首先感谢BalusC,这里有一些帖子帮助我理解,但有人删除了它们。无论如何,这就是我所学到的。
这就是我所要做的!
在问题的示例中,servlet 的 init 方法中的 filePath 变量将指向绝对路径,例如 C:\myApp\dataFiles
然后在 xhtml 中将使用类似注释 1 的内容调用 servlet
:outputLink 值的第一部分是 dl/ 这是因为要下载的 servlet 的 url 模式已映射到它
注2:在outputLink中,myPath的值可以是dl/Enterprise1/User1/file1.ext
干杯
Well finally got it work.
First of all thanks to BalusC, there were some posts here that helped me understand but someone delete them. Anyway, here's what I've learned.
That's all I had to do !
In the example of the question, the filePath variable in the servlet's init method will point to the absolute path, something like C:\myApp\dataFiles
Then in the xhtml will call the servlet with something like
Note 1: that the first part of the value of the outputLink is dl/ this is because the url-pattern for the servlet to download is mapped to it
Note 2: in the outputLink the value of myPath could be dl/Enterprise1/User1/file1.ext
Cheers