java 上传文件的最佳位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4548190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best Location for Uploading file
提问by Umesh Awasthi
Working on some import process where i need to first upload the file at some location on server and than later on i need to pick the file from this location to import it in to the system. i am just wondering what might be the best place to store the uploaded file. i have few option
处理一些导入过程,我需要首先在服务器上的某个位置上传文件,然后我需要从该位置选择文件以将其导入系统。我只是想知道什么可能是存储上传文件的最佳位置。我几乎没有选择
1) Can create a folder in the root of tomcat and than can place the upload files there and later on can pick the file for the import process.
1) 可以在 tomcat 的根目录中创建一个文件夹,然后可以将上传的文件放在那里,稍后可以选择导入过程中的文件。
File dir = new File(System.getProperty("catalina.base"), "uploads");
is this a good option and weather the above code will work equally in all enviornment
这是一个不错的选择吗,并且上面的代码在所有环境中都可以正常工作
2) i can create an uploads folder undermy application and can access it for file upload and later on for import by using the following code
2)我可以在我的应用程序下创建一个上传文件夹,并且可以使用以下代码访问它以进行文件上传和稍后导入
ServletActionContext.getServletContext().getRealPath("uploads");
your valuable suggestions are needed the only work i need to do is to upload the file and den run the import process for the uploaded files(s) and once import is successfull remove files from this folder to other like processed etc.
需要您的宝贵建议,我需要做的唯一工作是上传文件并为上传的文件运行导入过程,一旦导入成功,将文件从此文件夹中删除到其他类似处理等。
回答by BalusC
File dir = new File(System.getProperty("catalina.base"), "uploads");
File dir = new File(System.getProperty("catalina.base"), "uploads");
It won't work on environments where catalina.base
property is absent. So you need to either document it properly in the installation manual of the webapp to ensure that this property is been set on the server machine in question, or to look for an alternative approach.
它不适用于没有catalina.base
财产的环境。因此,您需要在 webapp 的安装手册中正确记录它以确保在相关服务器计算机上设置此属性,或者寻找替代方法。
ServletActionContext.getServletContext().getRealPath("uploads");
ServletActionContext.getServletContext().getRealPath("uploads");
This is not a good choice as permanent storage. Everything will get lost whenever you redeploy the WAR.
这不是永久存储的好选择。每当您重新部署 WAR 时,一切都会丢失。
Rather store it in a known and fixed path outside your webapp and add its path as <Context>
in Tomcat's /conf/server.xml
so that it's available online as well.
而是将其存储在您的 web 应用程序之外的已知且固定的路径中,并像<Context>
在 Tomcat 中一样添加其路径,/conf/server.xml
以便它也可以在线使用。
If you don't want to alter the Tomcat's /conf/server.xml
for some reason, then you need to create a servlet which reads the file from disk using FileInputStream
and writes it to the OutputStream
of the response. You can find herea basic example.
如果您/conf/server.xml
出于某种原因不想更改 Tomcat ,那么您需要创建一个 servlet,它使用从磁盘读取文件FileInputStream
并将其写入OutputStream
响应的 。你可以在这里找到一个基本的例子。