0

Here is my situation:

  • My table must have an button to show images, I am inserting data in database containing only the name of the image file (filename.jpg) and not an specific path, the server will show it based in what context it is in real server or not. For it after a long search I found the method getServletContext().getRealPath("/images") who finds the context where the paste is located, for example local host or real one (at least that's what I understood).

But, when I am at localhost it's showing this context for the method

getServletContext().getRealPath("/images"):

C:\Documents and Settings\josue\Meus documentos\workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testeDeFuncoes\images\file.jpg

I deduce it's calling context of a temporary folder, that was made only for localhost test. To recover it in localhost I think will work, but in real server will work it work, I think not, right?

Can I put in Webcontent in dynamic way that when I export WAR file it is stored in file and when upload it works no matter of what location server decides to choose. If not, what is generally used to do it a webpage in the most cases?

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
JamesB
  • 475
  • 1
  • 6
  • 24

1 Answers1

1

You might consider storing the images in the database, depending on your needs, see Storing Images in DB - Yea or Nay?

I'm personally a big fan of storing images in a database and caching them in a temporary directory on the local filesystem.

Either way you should have a reliable known canonical place to store the images. If you're not storing them in a database, decide on a non-temporary directory, or make it configurable.

The webapp may be served from a war file, and servletContext.getRealPath(...) is unlikely to return a reliable place to store anything.

Community
  • 1
  • 1
Christoffer Hammarström
  • 24,236
  • 4
  • 44
  • 54