1

I want to save a copy of Primefaces UploadedFile to my project directory. I have been searching the internet for the solution, what I have found is using Paths#get method. The example given in this answer is Paths.get("/path/to/uploads");, the problem is, where is the exact path of /path/to/uploads? I can't find it. I mean where should I create the path /path/to/uploads? Under my project directory? but which folder? I solve this issue temporary by hard coding the full path like Paths.get("C:/uploads/");

Newbie
  • 1,584
  • 9
  • 33
  • 72
  • Never ever save an uploaded file to your project directory... With a redeploy it is all gone. Save it outside the webapp... https://stackoverflow.com/questions/4358493/j2ee-primefaces-fileupload-file-saving-destination and https://stackoverflow.com/questions/14211843/how-to-save-uploaded-file-in-jsf – Kukeltje Aug 24 '17 at 13:08
  • And the Paths string starts with a / so it is abdolute to the current filesystem – Kukeltje Aug 24 '17 at 13:37

3 Answers3

2

FacesContext.getCurrentInstance().getExternalContext().getRealPath("/") will return you the current installation directory of your project.

And as @Kukeltje suggested, never ever save an uploaded file to your project directory, ... save it outside the webapps or even outside your container.

Therefore create a directory outsite your container (where you want to place your uploaded copies) and append ../ to the above path for each back step.

Say, if your application is deployed at D:/Tools/Tomcat7/webapps/your-application-name (e.g. on Windows using Tomcat) and you want to save copies to D:/Tools/uploads then following will give you required file path:

String uploadsFilePath = FacesContext.getCurrentInstance().getExternalContext()
    .getRealPath("../../../uploads");

Use it with the Paths.get(uploadsFilePath) and develop your download logic (I am not sure which library you are using for the Paths class).

Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
  • I almost always suggest to use absolute paths. Then you are never dependent on the installation location. I know the Q is about avoiding 'absolute' paths, but the absolute paths should be made configurable. You are now dependent on a **hard coded** relative path... – Kukeltje Aug 25 '17 at 07:33
  • Yeah, it is relative path but not **hard coded**, as `FacesContext.getCurrentInstance().getExternalContext().getRealPath("/")` will return the application's dynamic installation directory according to the `OS`. And for the `uploads` folder, it could also be created from code (if that doesn't exist). – Parkash Kumar Aug 25 '17 at 10:21
0

How about getClassLoader().getResource(Path/to/file)

So like

MyClass.class.getResource(bla/bla)

Which are now nested in src/resources

Like this You are system independent #profit

0x45
  • 779
  • 3
  • 7
  • 26
0

You have several options:

  1. For very quick development, you can use a hardcoded path, but be sure that it exists in your SUT (system under test).
  2. You can define it as a static final string in your module, but this means that each time you want to change that path, you will need to recompile...
  3. You can read that value from a property/config file.

There are more options (like using the registry if you are on Windows, or using an environment variable).

datv
  • 585
  • 3
  • 15