1

I have the following problem:

I created the servlet which should draw a dynamic graph. During the drawing process it should fetch the picture from another directory and to draw it upon another image. Everything should work fine:

try {
            BufferedImage temp = ImageIO.read(new File("image/arrow.png"));
            tempIm = temp.getScaledInstance(55, 55, Image.SCALE_SMOOTH);
        } catch (IOException e) {

            e.printStackTrace();

        }

But it prints the following:

SEVERE: javax.imageio.IIOException: Can't read input file! at javax.imageio.ImageIO.read(ImageIO.java:1275) at CertificateDraw.doGet(CertificateDraw.java:36)

I tried to change the path of the File object in all possible ways it just gives the same problem even though the part of the image is still sent to the browser. So the problem is with the ImageIO.read part - how can I find why it does not load the image?!

I am working in Eclipse - servlet is in the src folder. The image is in "image" folder under the rot directory "WebContent".

Artem Moskalev
  • 5,488
  • 11
  • 34
  • 52

1 Answers1

1

Relative paths in java.io.File are relative to the current working directory (CWD). This is the folder which is currently opened when the command is given to start the Java runtime environment (in your case, the webserver). When starting the server in Eclipse, this is usually the /bin folder of the project. You can figure this by printing new File(".").getAbsolutePath().

But you shouldn't be relying on relative paths in File at all. The CWD is not controllable from inside the code.

As it's in the webcontent folder already, just get it by ServletContext#getResourceAsStream() instead.

InputStream input = getServletContext().getResourceAsStream("/image/arrow.png");
BufferedImage image = ImageIO.read(input);
// ...

Note that the getServletContext() is inherited from the GenericServlet class which the HttpServlet extends from, so you don't need to provide the method yourself.

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • But could you explain more about the concept? What is CWD? Should not be the files deployed in the web server? The servlet processes the request only when it is called - it means File class is looking for resources before the deployment? + how does the getResource() find the same resource? should I also do the same thing with all types of fragments - not only picures? - what about jsp fragments - how are they loaded if they are not under the root directory directly but in some subfolder? – Artem Moskalev May 14 '12 at 21:13
  • CWD is the "current working directory". It's the local disk file system folder which is currently opened when the JRE/JVM is started. As an exercise, create a simple java class which prints `new File(".").getAbsolutePath()`. Compile it and save it at a fixed location and run it in command prompt from inside different folders. E.g. the windows folder, programfiles, documents, etc. You'll see that it prints the currently opened folder everytime. The `java.io.File` works on local disk file system only and is **not** aware about the context the application is running in (e.g. an Java EE webapp) – BalusC May 14 '12 at 21:16
  • In other words, just never use `File` with relative paths in a webapp. See also http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream – BalusC May 14 '12 at 21:17