简体   繁体   中英

How to store uploaded file in project folder in Java

I am new at Java web and currently I'm trying to store uploaded files (with servlet) in folder which exists in project. In web.xml I can sepcify full path but the full path will be different in somebody else's computer and it wouldn't work, so how is that possible to store uploaded files in folder which is located in project I'm using. My web.xml looks like this:

<web-app>
...
<context-param> 
    <description>Location to store uploaded file</description> 
    <param-name>file-upload</param-name> 
    <param-value>
        C:\Users\George\Desktop\dataFolder\
     </param-value> 
</context-param>
...
</web-app>

But I want to make the path look something like: thisProject\\dataFolder

There are several ways to do so, but the gist is:

You use an absolute path and make it deploy dependent.

Webapps are quite special, you don't really know where they will be deployed, you can't assume much about your working directory, there will be nothing like a project folder once deployed.

So, if you need a folder to put uploaded file, you just assume you will have one and use a parameter to locate it.

If you put such parameter in web.xml you can override it with server settings, how you can do it depends on the application server you are using.

Another way to set a deploy specific setting is to use environment variables. So you can define an environment variable like MYAPP_FILESTORAGE and set in this variable the actual path of where your files gets uploaded.

In eclipse (I think also other IDEs have similar mechanism) you can set environment variables for an application RUN/DEBUG.

If you can't set it in your ide you can still set it in your OS as an user variable or a system variable.

So in your project there won't be an hardocoded reference to the path, but a logical reference resolved by an environment variable.

Each of you and your friends will need just to set it once.

You can also store such information on a DB or use container specific mechanism.

Its the same problem that i came across when i was making this exact project myself.What i did was i made the storage directory in my projectApp only.It was myProject/WEB-INF/saveFiles . Due this, my download was not limited to any machine.I could use it in my ubuntu/windows/mac without any problem. Since you already know why you should not use the absolute path, lets come to the solution.In your servlet code.Assume your directory for storage is FileStorage ,and your application structure is

myProject-->WEB-INF -->FileStorage .

  /*  Get the directory location.myContextPath is the path of your 
      application. It will return "somepath/myProject/". */
      File storageDirectory=new File(request.myContextPath()+"/WEB-INF/FileStorage");

    //Now check if the directory is already created or not.If its not created,then it means your application is being run for the 1st time on this machine.
    if(!storageDirectory.exists())
         storageDirectory.mkdir();

Now you have got yourself a storage directory indepentdent of platform.Now you can proceed with your work of saving files. Note: It can be argued that why i choose FileStorage location as myProject/WEB-INF/FileStorage instead of myProject/FileStorage .I did it so because anything outside WEB-INF can be accessed directly by the user.Only the content inside WEB-INF cannot be accessed directly.It is much safer to keep importants stuffs there.

When You get the bytes of file in servlet Just write it using fileoutputstream in java

For example

Part p=request.getpart("file");

Inputstream is=p.getinputstram();

byte[] b=IOutils.getbytes(is);

FileOutputStream fos = null;

try { // create new file output stream

fos = new FileOutputStream("C://test.txt");

// writes bytes to the output stream

fos.write(b);

// flushes the content to the underlying stream

fos.flush();

catch(Exception ex) { // if an error occurs

ex.printStackTrace();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM