如何从图像上传的同一文件夹下载图像? [英] How to download images from the same folder as where the image is uploaded?

查看:117
本文介绍了如何从图像上传的同一文件夹下载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个用户可以上传他的照片的项目。此照片存储在文件夹images / uploads / filename中。当他的个人资料被创建并且照片被显示时,我使用< img> 标签与 src images / uploads / filename,但照片不显示。
如果我手动将照片复制到相邻文件夹images / abc / filename,然后使用它作为源,那么它的工作。



造成了怎么解决呢?我需要使用相同的文件夹来上传和下载照片。

解决方案

如果您将webapp作为IDE项目,并将上传的图像存储在IDE的项目空间中。外部执行的IDE(如您的servlet代码)的IDE项目文件夹中的更改不会立即反映在部署的服务器的工作文件夹中。只有当你触摸它(通过刷新项目)或者通过复制它(就像你刚才发现的那样),那么它将被反映出来。



毕竟,存储webapp的部署文件夹中的上传文件是一个坏主意。当您重新部署webapp时,这些文件将会丢失,因为这些文件不包含在原始WAR文件中。



您需要将它们存储在webapp部署之外的某个位置文件夹在不同的固定路径,如 / var / webapp / uploads 。您不应该使用相对路径或 getRealPath()在其周围创建 File 对象。只需使用固定路径。您可以随时将固定路径配置为上下文参数设置,VM参数,属性文件设置或甚至JNDI条目。



然后,将其提供给万维网,只是添加正确的路径作为另一个docroot到服务器配置。目前还不清楚您正在使用什么服务器,但是在Tomcat中,将< Context> 添加到 server.xml

另请参见:




I am creating a project wherein the user can upload his photo. This photo is stored in the folder "images/uploads/filename". When his profile is created and the photo is to be shown, I use the <img> tag with src as "images/uploads/filename", but the photo does not show up. If I manually copy the photo to an adjacent folder "images/abc/filename" and then use it as the source then it works.

How is this caused and how can I solve it? I need to use the same folder to upload and download photos.

解决方案

That can happen if you're running the webapp as an IDE project and are storing the uploaded images in the IDE's project space. Changes in the IDE's project folder which are performed externally (as by your servlet code) does not immediately get reflected in the deployed server's work folder. Only when you touch it (by refreshing the project) or by copying it (as you happen to have found out), then it will get reflected.

After all, storing uploaded files in the webapp's deploy folder is a bad idea. Those files will get all lost whenever you redeploy the webapp, simply because those files are not contained in the original WAR file.

You need to store them somewhere outside the webapp's deploy folder on a different fixed path like /var/webapp/uploads. You should not use relative paths or getRealPath() to create the File object around it. Just use a fixed path. You can always make the fixed path configureable as a context param setting, a VM argument, a properties file setting or even a JNDI entry.

Then, to serve it to the world wide web, just add exactly that path as another docroot to the server config. It's unclear what server you're using, but in Tomcat it's a matter of adding another <Context> to the server.xml.

See also:

这篇关于如何从图像上传的同一文件夹下载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆