Java – Tomcat uses defaultservlet to get static content in external directory

I have a simple web application that allows users to upload and access files When users access uploaded files, the server needs to support all header types, such as accept ranges, so that partial content response can be made to large media files Defaultservlet (in Tomcat) handles everything in the serveresource () method called by doget (), so I want to use defaultservlet for uploaded files This is easy under normal circumstances because it is a default servlet, but unfortunately, in my case, I can't find a way to use the default servlet

To prevent deleting uploaded files when redeploying webapp, I need to store them outside the webapp directory These files can only be accessed by the user who uploaded them, so I created a javax servlet. Filter to handle resource access authorization I know if I'm on the server Add a new context to the external resource directory in XML, they will be accessible and will use defaultservlet. XML

<Context docBase="/path/to/resources" path="/resource" />

But I don't want to do this because I need to systematically modify the deprecated server at that time XML (for reasons), and when the / resource path has a context, I will not call the URL mapping filter Therefore, I don't think creating a new context is a good solution

The only other solution I can think of is to create a servlet for all requests that extend the '/ resource / *' of the defaultservlet and somehow override the function of getting the requested file at its actual external location instead of returning 404 on it Resources that do not exist in webapp However, I haven't found a simple way to convert defaultservlet into a way to get external files in webapp directory Maybe there's a way?

What should I do?

Solution

Not the answer

This means that you are redeploying your stuff incorrectly (in a very bad way), possibly using a war file, or a worse (worst) Tomcat manager

You're right, but you're wrong. There's no need for another background

Solution:

Dear Michael - O points out the simplest and perhaps the best answer

You can also implement things like defaultservlet. Sometimes (not suitable for your situation) it's better to implement something from zer0 instead of configuring and maintaining too many complex things

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>