Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java EE - Best way to get real path to uploaded files? [duplicate]

I have a web application with an upload form where users can upload files.

I'd like to store the files a folder 'files'. And i'd like this folder to be placed directly under the webapp-root.

Using struts2, in my uploadFileAction, i can easily set this path with String uploadDir = ServletActionContext.getServletContext().getRealPath("/files")+ "/";

But when i'm trying to retrieve the files, apparently my bean which tries to access the file does not have access to getServletContext() and thus throws a nullpointer on getRealPath

Now im trying to figure out how i should approach this. I've heard about spring resource is the way to go, but i cant find any relevant examples.

Is there an easy way to get my paths? I just want the rootpath to my webapp. Nothing more nothing less...

like image 483
user829237 Avatar asked Sep 01 '11 18:09

user829237


People also ask

Which path is hold by location attribute in file uploading in Java?

The destination is the path to the location where the file will be saved on your computer. Pressing the Upload button at the bottom of the form posts the data to the servlet, which saves the file in the specified destination.

How can I save my uploaded file in Java?

Java file uploads From the form, invoke a Java Servlet to handle the server-side processing of the file; Code a Java Servlet to handle the file upload process; Annotate the file upload Servlet with the @MultipartConfig annotation; In the Servlet, save the uploaded file to the server's file system; and.

Which points are important while uploading a file?

To upload a single file you should use a single <input .../> tag with attribute type="file". To allow multiple files uploading, include more than one input tags with different values for the name attribute. The browser associates a Browse button with each of them.


2 Answers

I'd like this folder to be placed directly under the webapp-root.

This isn't going to work. All those files will get lost whenever you redeploy the webapp or even when you restart the server. Those files are namely not contained as part of the original WAR.

You need to store them in a fixed path outside the webapp's context. E.g. /var/webapp/uploads. You can configure this path in a properties file or as a system property.

like image 180
BalusC Avatar answered Oct 26 '22 05:10

BalusC


I've found that a few ways to accomplish this regardless of what OS platform you're using. I typically like to store my files in some way relative to my web application. This being said, the easiest way to do this is using your class file as a reference to get the real path. I use something similar to the following to store image uploads for a few web applications that I've built:

public class FileLocationTest {

    public static void main(String[] args) {
        String path = FileLocationTest.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        System.out.println(path);
    }
}

This can be done using a static utility class in your web app, or any class for that matter to obtain the real path of your application regardless of OS. Alternatively in a servlet I've also used the request class to get the Tomcat location if my appbase is in a different area, something like this:

    String location = request.getClass().getProtectionDomain().getClassLoader().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
    String path = location.substring(0, location.indexOf("/tomcat")) + "/data/events/images/";
like image 24
Russell Shingleton Avatar answered Oct 26 '22 07:10

Russell Shingleton