自定义下载 servlet

发布于 2024-12-28 13:01:25 字数 3061 浏览 0 评论 0原文

我从绝对路径查看了 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.t​​xt 或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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神妖 2025-01-04 13:01:25

终于成功了。

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

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

这就是我所要做的!

在问题的示例中,servlet 的 init 方法中的 filePath 变量将指向绝对路径,例如 C:\myApp\dataFiles
然后在 xhtml 中将使用类似注释 1 的内容调用 servlet

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

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

  1. In Servlet's init method the filePath variable must point to the absolute path where the files to download are.
  2. In the web.xml the servlet-map-url-pattern will cause to execute the servlet when the browser has that url pattern.
  3. In the xhtml page tha value of the link should begin with the url-pattern followed by the name (or a path+FileName) so when you click the link the download begins.

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文