简体   繁体   English

我应该将pdf放在Java Web应用程序项目中的哪里?

[英]Where should I place a pdf to be download in a Java web app project?

I have a maven directory structure: 我有一个Maven目录结构:

 /src

    /main

        /java 
        /resources 
        /webapp 

So if I put the file under /resources, when I deploy the application to tomcat the file ends up in: webapps/applicationRoot/WEB-INF/classes/xx.pdf 因此,如果我将文件放在/ resources下,则在将应用程序部署到tomcat时,文件最终位于:webapps / applicationRoot / WEB-INF / classes / xx.pdf

If I put in webapp/ then it ends up in webapps/applicationRoot/xx.pdf 如果我放入webapp /,那么它最终会出现在webapps / applicationRoot / xx.pdf中

If I create a folder under webapp like webapp/myResources it ends up in: webapps/applicationRoot/myResources/xx.pdf 如果我在像webapp / myResources这样的webapp下创建一个文件夹,则它的最终目录为:webapps / applicationRoot / myResources / xx.pdf

The 2nd one looks the prettiest I believe because then in the code I can say: 第二个看起来我最漂亮,因为然后在代码中我可以说:

    InputStream is = servletContext.getResourceAsStream("/re.pdf");

How would I get my pdf file in case 1 in my servlet? 在我的servlet中,如何获取案例1中的pdf文件?

Also, which one is the standart? 另外,标准是哪一个? Which should I use? 我应该使用哪个?

If you put your pdf under webapp/bla/my.pdf, a user could directly access it in his browser from http://{yoursercer}/{contextroot}/bla/my.pdf. 如果将pdf放在webapp / bla / my.pdf下,则用户可以从http:// {yoursercer} / {contextroot} /bla/my.pdf在浏览器中直接访问它。 Is that what you want? 那是你要的吗? Put it there then. 然后把它放在那里。

If you want the PDF to be accessible to your Java code, then put it under resources. 如果您希望Java代码可访问PDF,则将其放在资源下。 It will not be directly accessible from a browser. 无法从浏览器直接访问它。

set the content type. 设置内容类型。 response.setContentType("application/pdf");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM