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.
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