上传文件的最佳位置 [英] Best Location for Uploading file

查看:217
本文介绍了上传文件的最佳位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

处理一些导入过程,我需要先在服务器上的某个位置上传文件,然后我需要从这个位置选择文件将其导入系统。
我只是想知道什么是存储上传文件的最佳位置。我有几个选项

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)可以在tomcat的根目录中创建一个文件夹,然后可以将上传文件放在那里,稍后可以选择导入过程的文件。

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.

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)我可以在我的应用程序下创建一个uploads文件夹,可以访问它进行文件上传,然后使用以下代码进行导入

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

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.

推荐答案


File dir = new File(System.getProperty("catalina.base"), "uploads");


它不适用于 catalina.base 属性不存在。因此,您需要在webapp的安装手册中正确记录它,以确保在相关的服务器计算机上设置此属性,或者寻找替代方法。

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.


ServletActionContext.getServletContext().getRealPath("uploads");


这不是永久存储的好选择。重新部署WAR时,一切都会丢失。

This is not a good choice as permanent storage. Everything will get lost whenever you redeploy the WAR.

而是将其存储在webapp外部的已知且固定的路径中,并将其路径添加为<在Tomcat的 /conf/server.xml 中的上下文> ,以便它也可以在线使用。

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.

如果由于某种原因你不想改变Tomcat的 /conf/server.xml ,那么你需要创建一个servlet,使用 FileInputStream 从磁盘读取文件,并将其写入响应的 OutputStream 。您可以在这里找到一个基本示例。

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 here a basic example.

  • Simplest way to serve static files from outside application server

这篇关于上传文件的最佳位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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