This question already has answers here:
Simplest way to serve static data from outside the application server in a Java web application
(10个答案)
2年前关闭。
这听起来像是一个显而易见的问题,但事实并非如此。我已经回答了一堆,没有人问或说文件的URL是什么。
我希望能够托管.dtd文件,以便我的xml可以像这样引用它:
我试过将文件放在/ static /
但是没有证据表明Tomcat托管了该文件。
这些是我已经看过的一些问题。他们都没有询问或陈述文件的URL,因此这个问题不是重复的:
How to serve static content from tomcat
https://serverfault.com/questions/143667/how-to-access-a-simple-file-or-folder-from-tomcat-webapps-folder
How to serve static files in my web application on Tomcat
Simplest way to serve static data from outside the application server in a Java web application
https://www.moreofless.co.uk/static-content-web-pages-images-tomcat-outside-war/
在这种情况下,文件的url将是:
(10个答案)
2年前关闭。
这听起来像是一个显而易见的问题,但事实并非如此。我已经回答了一堆,没有人问或说文件的URL是什么。
我希望能够托管.dtd文件,以便我的xml可以像这样引用它:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Somexml SYSTEM "http//:example.com/my.dtd">
<Somexml>
</Somexml>
我试过将文件放在/ static /
但是没有证据表明Tomcat托管了该文件。
这些是我已经看过的一些问题。他们都没有询问或陈述文件的URL,因此这个问题不是重复的:
How to serve static content from tomcat
https://serverfault.com/questions/143667/how-to-access-a-simple-file-or-folder-from-tomcat-webapps-folder
How to serve static files in my web application on Tomcat
Simplest way to serve static data from outside the application server in a Java web application
https://www.moreofless.co.uk/static-content-web-pages-images-tomcat-outside-war/
最佳答案
您可以创建servlet:
@WebServlet("/images/*")
public class ImageServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filename = request.getPathInfo().substring(1);
File file = new File(PATH_TO_LIBRARY_WHERE_FILES_ARE_STORED, filename);
response.setHeader("Content-Type", getServletContext().getMimeType(filename));
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + filename + "\"");
Files.copy(file.toPath(), response.getOutputStream());
}
}
在这种情况下,文件的url将是:
http://www.yourdomain.com/images/name_of_file.xxx
关于tomcat - 如何在Tomcat中托管静态文件,以及该文件的URL是什么? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46698356/