7

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.

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204

1 Answers1

10
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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • @BalucS if i put the context in the server.xml as suggested by you how i can access this path inside my java application because at the end i need to access this file using java IO so that can pass on this to the other part of the system – Umesh Awasthi Dec 29 '10 at 07:55
  • You could hardcode the full path in code, you could configure the full path in `web.xml` and obtain it from servletcontext, you could hardcode/configure the partial path `uploads` and use `getRealPath()` to reveal it, etc. – BalusC Dec 29 '10 at 13:13
  • @BalusC This works for tomcat server. What changes should I make if I want to implement it in glassfish3 server. – Wizard Sultan Feb 25 '13 at 20:59