keepmoving
keepmoving

Reputation: 2043

How to store content on local and remote server

I have writing a program which ask for video file from user. once video is browser by user it is save inside war/content. I do not have any problem to do this. when i want to see the video, i used video tag of html and give the relative path. video come perfectly fine and it can be viewed.

I used the following line of code to resolve the path.

    System.setProperty("webapp.root", "C:/solution/totalsolution");
File webappDir = new File(System.getProperty("webapp.root", "./"));

        File webappInfo = new File(webappDir, "war");

        if(!webappInfo.exists()){
            File dir = new File(webappDir, "war");
            if(!dir.exists()){
                if(dir.mkdir()){
                    webappDir = dir;
                }
            }
        }

        webappDir = webappInfo;

        webappInfo = new File(webappDir, "content");

        if(!webappInfo.exists()){
            File dir = new File(webappDir, "content");
            if(!dir.exists() && dir.mkdir()){
                webappDir = dir;
            }
        }


        webappDir = webappInfo;

        if(contentDir != null){
            webappInfo = new File(webappDir, contentDir);

            if(!webappInfo.exists()){
                File dir = new File(webappDir, contentDir);
                if(!dir.exists() && dir.mkdir()){
                    webappDir = dir;
                }
            }

            webappDir = webappInfo; 
        }

this program create new folder in specified path like "C:/solution/totalsolution/war/content/abcd/xysa.mp4" when i show any resource then i make relative path like this

http://localhost:8080/solution/content/abcd/xysa.mp4

till now every thing is ok.But when i extract war file and deploye it into server. Then is way failed due to static location in system property.

So can any body tell be the right approch to save data into war folder and right back reading it on local as well as on server to.

Upvotes: 0

Views: 170

Answers (2)

peter.petrov
peter.petrov

Reputation: 39457

1) Do not store it in the war folder but in a separate location (probably designed specifically for this). 2) When getting the file from the user, you need to generate new unique name for this file, and store somewhere (e.g. DB) the mapping from the original file name to the new file name. Then on the disk you store the file under the new name. This is to guarantee your file names are unique (think what happens if two users upload files with the same name).

Upvotes: 1

Don't try to save data into your webapp; there is no guarantee that this directory will be writable or even unpacked from the jar. Instead, read a property that tells you where to store data saved at runtime.

Upvotes: 1

Related Questions