简体   繁体   English

如何从内部服务器提供Java Web应用程序中的静态内容?

[英]How to serve static content in a Java web app from internal server?

It's a standard (possibly trivial) situation, but I cannot find detailed information on the topic. 这是一个标准(可能是微不足道的)情况,但我无法找到有关该主题的详细信息。

Suppose we have a web application A ( http://my-webapp ) and a file server F ( http://file-server ). 假设我们有一个Web应用程序Ahttp://my-webapp )和一个文件服务器Fhttp://file-server )。

For clarity: 为清楚起见:

  • A is run on Jetty 9; A在Jetty 9上运行;
  • F is visible for Jetty server, and NOT visible for a client. F对Jetty服务器可见,对客户端不可见。

What is the best practice to show in A a picture stored on F ? 什么是一个展示存储F上的图像的最佳做法是什么?

Suppose client makes a request http://my-webapp/pictures/123 , where 123 - any id, which somehow points to a picture stored as http://file-server/storage/xxx123.jpg , and expects to see the picture in the browser. 假设客户端发出请求http://my-webapp/pictures/123 ,其中123 - 任何id,以某种方式指向存储为http://file-server/storage/xxx123.jpg的图片,并期望看到浏览器中的图片。

"Best practice" covers a lot of ground. “最佳实践”涵盖了很多方面。

For load and performance reasons, it's a good idea to use a web server (like NGINX or Apache) rather than an application server to serve static assets. 出于负载和性能原因,使用Web服务器(如NGINX或Apache)而不是应用程序服务器来提供静态资产是个好主意。 Most production environments have this set up, using a web server to proxy requests to the application server when necessary. 大多数生产环境都使用此设置,必要时使用Web服务器将请求代理到应用程序服务器。

If you have such a setup, you could map the images drive on F as a drive on your web server, and use a .htaccess rewrite rule to deal with file name logic. 如果您有这样的设置,您可以将F上的图像驱动器映射为Web服务器上的驱动器,并使用.htaccess重写规则来处理文件名逻辑。

If that's not possible because the file name logic cannot be captured in a regex or similar, you could write a servlet on A to issue a redirect to a "regular" web location. 如果由于无法在正则表达式或类似文件中捕获文件名逻辑而无法实现,则可以在A上编写一个servlet来重定向到“常规”Web位置。 Something along the lines of: 有点像:

I strongly recommend you do not use a servlet to read the file from F and then stream that to the browser; 我强烈建议你不要使用servlet来读取F中的文件,然后流式传输到浏览器; this consumes large amounts of memory on your application server, and may slow down or even fail depending on your local network conditions. 这会占用应用程序服务器上的大量内存,并且可能会因本地网络状况而变慢甚至失败。 Your application's performance will almost certainly deteriorate very quickly under load. 您的应用程序的性能几乎肯定会在负载下很快恶化。

I propose the following solution as a minimal example that could be a good starting point. 我提出以下解决方案作为一个很好的起点可以作为一个很好的起点。

Redirection by .htaccess seems to be doing similar things on the low level. .htaccess重定向似乎在低级别上做了类似的事情。

Actually the problem is supposed to be solved by web application server itself without intervention of external tools (like Apache httpd or Nginx). 实际上,问题应该由Web应用程序服务器本身解决,而无需外部工具(如Apache httpd或Nginx)的干预。

1. Declare servlet in web.xml 1.在web.xml中声明servlet

<servlet>
    <servlet-name>pictures</servlet-name>
    <servlet-class>myapplication.servlets.HiddenFileServlet </servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>pictures</servlet-name>
    <url-pattern>/pictures/*</url-pattern>
</servlet-mapping>

2. Implement the servlet 2.实现servlet

public class HiddenFileServlet extends HttpServlet
{     
  @Inject
  MyService myService; // a service for paths finding on http://file-server

  @Override
  protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws IOException
  {        
    String requestedUri = req.getRequestURI();

    String fileName = myService.getFileName( requestedUri );

    String mime = getServletContext().getMimeType( fileName );

    if ( mime == null )
    {
      resp.setStatus( HttpServletResponse.SC_INTERNAL_SERVER_ERROR );
      return;
    }
    else
    {
      resp.setContentType( mime );
    }

    // path on http://file-server/storage
    URL fileFullPath = myService.getInternalPath( requestedUri );

    URL file = new URL( fileFullPath );

    try (
        InputStream in = file.openStream();
        OutputStream out = resp.getOutputStream()
    )
    {
       org.apache.commons.compress.utils.IOUtils.copy( in, out );
    }
  }
}

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

相关问题 在Java Web应用程序中从应用程序服务器外部提供静态数据的最简单方法BUT In WEB LOGIC - Simplest way to serve static data from outside the application server in a Java web application BUT In WEB LOGIC 如何从 tomcat 提供静态内容 - How to serve static content from tomcat 尝试从我的 SpringBoot 应用程序提供 static 内容,但失败了 - Trying to serve static content from my SpringBoot app, but it fails 如何在 Java Spring Boot MVC 中提供静态和上传的内容 - How to serve static and uploaded content in Java Spring Boot MVC Java(Tomcat):如何配置无cookie子域以提供静态内容 - Java (Tomcat): how to configure a cookieless subdomain to serve static content 如何从 Java 中的端口提供 HTTP 内容 - How to serve HTTP content from a port in Java 在Web服务器中提供静态内容和tomcat中的动态内容仍然是一个很好的性能实践? - Serve static content in a web server and dynamic content in tomcat is still a good performance practice? 从Google App Engine上的Java webapp提供静态文件 - Serve static files from Java webapp on Google App Engine 从Java Web应用程序调用静态方法 - Calling a static method from a Java web app 如何在Spring MVC中投放静态内容? - How to serve static content in spring mvc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM