简体   繁体   English

使用Spring从压缩文件中提供静态资源

[英]Serve static resources from within a compressed file using Spring

Is it possible to serve static resources from within a compressed file using Spring MVC? 使用Spring MVC是否可以从压缩文件中提供静态资源? Something like this . 这样的东西。

I have some data packaged into individual JSON files (eg 123.json, 1634.json, etc.) and and currently serving them via 我将一些数据打包到单个JSON文件中(例如123.json,1634.json等),目前通过

<mvc:resources mapping="/resources/**" location="/resources/" />

The files are under .../resources/dataFiles/ . 这些文件位于.../resources/dataFiles/ So a user can go to http://mywebsite.com/resources/dataFiles/123.json to retrieve the data for entity 123. 因此,用户可以转到http://mywebsite.com/resources/dataFiles/123.json检索实体123的数据。

However, I have ~10,000 JSON files. 但是,我有约10,000个JSON文件。 It would be great if I could compress them under one file ( .../resources/dataFiles/entities.zip ) and tell Spring to serve the individual JSON files from within the compressed file. 如果我可以将它们压缩在一个文件( .../resources/dataFiles/entities.zip )下,并告诉Spring从压缩文件中提供单个JSON文件,那就太好了。

So the user would still go to http://mywebsite.com/resources/dataFiles/123.json but the only file under .../resources/dataFiles/ is entities.zip. 因此,用户仍然可以访问http://mywebsite.com/resources/dataFiles/123.json但是.../resources/dataFiles/下的唯一文件是.../resources/dataFiles/

I'm using Tomcat 7.0 if this question is outside the scope of the MVC framework. 如果此问题不在MVC框架的范围内,则我正在使用Tomcat 7.0。

I'm not sure if there's a Spring out-of-the-box component that does that, but you can create an independent Servlet to handle the incoming requests for static resources, then this servlet would parse the file name, and dynamically read from the zipped file the correct Zip Entry and return the content to the OutputStream of the Response. 我不确定是否有一个Spring的现成组件可以做到这一点,但是您可以创建一个独立的Servlet来处理对静态资源的传入请求,然后该Servlet将解析文件名,并从中动态读取压缩的文件正确的Zip条目,并将内容返回到Response的OutputStream。 Take a look at: 看一眼:

sample code 样例代码

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

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