1

I am able to upload the file successfully, but the file get stored to the deployed directory on the server.

As soon as I remove the project and redeployed the project to my Tomcat server 7.0 all the files I had uploaded gets deleted from that.

I am using JSF as my server side technology with Tomcat server 7.0. in eclipse Ide

Muthu
  • 269
  • 3
  • 9
  • 23

2 Answers2

2

You should not store uploaded files in the deploy folder and definitely also not in WEB-INF folder as suggested by others. That they get lost when you redeploy the webapp is simply because they are not contained in the original WAR file.

You need to store the uploaded files on a fixed path outside the deploy folder. For example /var/webapp/uploads. You can always make the folder available to the web by adding a new <Context> to Tomcat.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Save your files to the /WEB-INF folder or anywhere to your hard drive (except deployment directory of course)

I used this piece of code (it stores files for each user into it's own folder)

String path = Util.getPathToFiles() + "WEB-INF" + File.separator + "upload";
    try {
        path += File.separator + session.getCurrentUser().getLogin();
        if (!isFolderExists(path)) {
            new File(path).mkdir();
        }
        path += File.separator + file.getName();
        FileUtils.copyFile(file, new File(path));
    } catch (IOException ex) {
        Logger.getLogger(ThesisBean.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        file.delete();
    }

getPathToFilessimply returns the realPath from ServletContext and FileUtils is class from Apache IO (but you can write your code for file copy on your own, it's just helper class)

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • This is my logic.String tmpFile = scontext.getRealPath(("/ngoPhotos/") + event.getFile().getFileName()); File result = new File(tmpFile); FileOutputStream fos; try { fos = new FileOutputStream(tmpFile); fos.write(imageByte); fos.close(); } catch (Exception e) { e.printStackTrace(); }.How to store into web-inf or else other folder.can you help me – Muthu May 24 '12 at 06:57