开发者

Best Location for Uploading file [duplicate]

This question already has answers here: Recommended way to save uploaded files in a servlet application (2 answers) Closed 7 years ago.

Working on some import process where i need to first upload the file at some location on server and th开发者_高级运维an 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.

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

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");

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");

This is not a good choice as permanent storage. Everything will get lost whenever you redeploy the 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.

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.

Related questions:

  • Simplest way to serve static files from outside application server
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜