Java,Spring MVC,从服务器提取任何类型的文件(.png、.jpg、.pdf、.doc 等)并提供给用户下载的最简单方法?

发布于 2024-10-28 06:23:32 字数 1000 浏览 1 评论 0原文

我一直在搜索这个,但我发现的信息对于我想要做的事情来说似乎过于复杂,或者涉及必须准确指定正在下载的文件类型并为此使用特定的 Java 类,我想知道是否有是一种不必执行此操作并且能够使用一种方法执行此操作的方法,无论文件类型如何,因为我不生成文件,只是从目录中存在的位置获取它。我在本地的一个文件夹中有一组文件,例如:

/media/files/files

在这个目录中我有各种不同的文件,.jpg、.doc。 .xls、.png 等,并且想要以最简单的方式获取给定的文件路径,从目录中检索它并将其发送回 JSP,以便用户能够下载该文件。我找到的信息似乎总是涉及从 blob 反序列化、从远程位置拉取或类似的代码相当长的地方,我认为必须有一种相当简单的方法来完成我想要做的事情考虑到我在本地有文件并且它们是常规格式(不在数据库中等),请

提供任何建议

编辑感谢科斯蒂我认为我现在走在正确的道路上但我在下面尝试了这个,但仍然没有能够访问该文件:

我已将其放入 tomcat /conf 中的 server.xml 文件中:

然后尝试访问 http://myip:8080/files/test.txt 处的文件,但它不起作用,在我的 Spring web.xml 中,我唯一的 Servlet 映射是:

<servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/d2/*</url-pattern>
    </servlet-mapping>

任何建议非常感谢

修复:忘记了 Eclipse 中的 server.xml 覆盖了 tomcat 服务器中的 server.xml,这就是为什么它不起作用的问题

I have been searching on this but the info I am finding seems overly complex for what I am trying to do or involves having to specify exactly what type of file is being downloaded and use a specific Java class for that, I'm wondering if there is a way to not have to do this and to be able to use one way of doing this regardless of the file type as I am not generating the file, just taking it from where it exists in my directory. I have a set of files in a folder locally, say:

/media/files/files

and in this directory I have a variety of different files, .jpg, .doc. .xls, .png, etc and want the simplest way that I can take a given file path, retrieve it from the directory and send it back to a JSP so that it will enable the user to download the file. The info I am finding always seems to involve de-serializing from a blob, pulling from a remote location, or something like this to where the code is rather lengthy and I figure there must be a fairly simple way to do what I am trying to do given that I have the files locally and they are in regular format (not in a DB, etc)

Any advice is appreciated

EDIT Thanks to Costi I think I am on the right path now but I tried this, below, and am still not able to access the file:

I have put this in my server.xml file in my tomcat /conf:

<Context path="/files" docBase="/media/files/" />

and then tried accessing a file at http://myip:8080/files/test.txt but it is not working, in my Spring web.xml the only Servlet-mapping I have is:

<servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/d2/*</url-pattern>
    </servlet-mapping>

Any advice is greatly appreciated

FIXED: Forgot that the server.xml in Eclipse overrides the server.xml in the tomcat server which was the issue as to why it wasn't working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2024-11-04 06:23:32

如果您不希望在下载过程中包含任何逻辑,则可以简单地将文件作为静态资源提供,前提是身份验证/授权内容由容器在需要时处理。为此,您只需映射 Spring DispatcherServlet,以便它不会处理对 /media/files/ 的请求,实现此目的的一种简单方法是将 spring servlet 映射到某些扩展名,例如 /< /em>.html,或已映射到子目录而不是 /。这样,容器将处理对 /media/ 内容的请求,并且仅提供静态文件。

如果您需要以某种方式干扰下载(例如,如果您在容器外部处理身份验证内容,或者您​​想在每次下载之前或之后执行某些操作),那么您可以从 spring 控制器返回一个仅代理的视图在设置正确的标头(如内容类型、长度、配置等)后,将请求的文件发送到输出流。我不知道有任何这样的内置视图,但自己创建一个视图很容易,也许可以编写自己的 ViewResolver,它将返回 /media/whatever 下文件名的该视图的实例。

我希望这有帮助。

If you do not want any logic wrapped around the download, you can simply make the files available as static resources, provided that the authentication/authorisation stuff is handled by the container if needed. For this to work, you simply need to map Spring DispatcherServlet so that it won't handle requests to /media/files/, one simple way to achieve this would be to have the spring servlet mapped to some extension like /.html, or have mapped to a subdirectory instead of /. This way, the container will handle requests to your /media/ stuff and it will simply serve static files.

If you need to interfere with the download somehow (for instance, if you handle auth stuff outside the container or you want to take some action prior to or after every download), then you can have a View returned from your spring controller that simply proxies the requested file to the output stream, after setting the proper headers like Content-Type, length, disposition and so on. I don't know of any such built-in view, but it's pretty easy to create one yourself and perhaps write your own ViewResolver that will return an instance of that view for file names under /media/whatever.

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文