0

I'm developing a small Java application using Spring MVC. I want to store profile pictures por users, but I don't know where to store it so I can serve it later to the users.

I have read this articles:

What does servletcontext.getRealPath(“/”) mean and when should I use it

Recommended way to save uploaded files in a servlet application

But I think I'm missing something. In the second article the accepted answer says that the recommended way to go is hardcoding a path in your system, like C:/webapps/uploads but is this the best way to go? If you run your application in a different OS this is not going to work right? There is no way to store your images for example in some path like the webapp resources folder?

Community
  • 1
  • 1
Elias Garcia
  • 6,772
  • 11
  • 34
  • 62
  • You can store it on disk, in database, on some remote server or anywhere else. Only you can decide this. – talex Oct 17 '16 at 16:02
  • Either hardcode the path or find a way to retrieve it with a parameter somewhere (e.g. in database). – Arnaud Denoyelle Oct 17 '16 at 16:06
  • Yes, I'm asking to store it on disk. I want to store it in a path working in all OS for example – Elias Garcia Oct 17 '16 at 16:07
  • 1
    @EliasGarciaMariño For temporary storage, you can use `Files.createTempFile()`. For a cross-platform permanent storage, you do not have a choice : the application must be given a base directory, either hardcoded or parameterized. – Arnaud Denoyelle Oct 17 '16 at 16:09
  • @EliasGarciaMariño Why not injecting it via VM parameter or environment variable? Your second link suggests these approaches. Dependend on your OS, you can then use different paths. – fxlae Oct 17 '16 at 16:11
  • So, for example, using Tomcat, would be `System.getProperty("catalina.base")` the best approach to go? – Elias Garcia Oct 17 '16 at 16:14
  • @EliasGarciaMariño Maybe not CATALINA_BASE because you do not want to mess up with your tomcat configuration but you can create another environment variable for that purpose so that you can retrieve your base directory with `System.getProperty("BASE_DIR");` – Arnaud Denoyelle Oct 17 '16 at 16:18
  • Thanks @ArnaudDenoyelle, I'd better do that! – Elias Garcia Oct 17 '16 at 16:26

0 Answers0