0

I have created a JavaEE based web application that I then exported as a WAR file and deployed to a tomcat server that is running on a vm.

The Application allows users to upload an image to a location on the vm disk, and then to run an image processing class that accepts a path to the previously uploaded file.

I am trying to use ImageIO.read() to read in the file as a buffered image.

When running in the IDE, the image processing class works fine and is able to accept the image stored on the disk and output the processed image. However when accessing it as a deployed web application I am met with the error:

HTTP Status 500 - Can't read input file!

type Exception report
message Can't read input file!
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.imageio.IIOException: Can't read input file!
    javax.imageio.ImageIO.read(Unknown Source)
    upload.servlet.resizeImageAndSaveSobel2.sobelFilter(resizeImageAndSaveSobel2.java:22)
    upload.servlet.resizeImageAndSaveSobel2.processImage(resizeImageAndSaveSobel2.java:14)
    upload.servlet.RunTongue.doPost(RunTongue.java:24)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
note The full stack trace of the root cause is available in the Apache Tomcat/9.0.0.M13 logs.

This is the code which is a part of the image processing class that reads in the uploaded image:

public void sobelFilter(String input, String output) throws IOException {
  File inputFile = new File(input);
  BufferedImage img = ImageIO.read(inputFile);
  ImageReader ir = new ImageReader();
  BufferedImage sobelImg = ir.greyscale(img);
  File outputFile = new File(output);
  ImageIO.write(sobelImg, "jpg", outputFile);

 }

The input file string is as follows: C:/Users/SRCMIPS/Desktop/M2/Image Upload/PatientImage.jpeg

Is this a problem regarding ImageIO.read with a deployed WAR of the project? Is the application unable to read in images from paths located on the vm?

Any guidance with regards to a solution to this problem would be appreciated.

PGH
  • 1
  • 1
  • Put a break point in `sobelFilter` method and inspect `input`. Most likely it is wrong. – VHS Apr 07 '17 at 00:20
  • you have to configure Tomcat to serve external static data. please refer this. [link](http://www.moreofless.co.uk/static-content-web-pages-images-tomcat-outside-war/) – Rajith Pemabandu Apr 07 '17 at 00:23
  • @PGH Hope this help too [link](http://stackoverflow.com/questions/1812244/simplest-way-to-serve-static-data-from-outside-the-application-server-in-a-java) – Rajith Pemabandu Apr 07 '17 at 00:27
  • Assuming you have the file *on the machine that runs the Tomcat server*. Most likely, this is a file access rights issue. The file path may be correct, but the Tomcat process is still not able to read it. Make sure the file is readable as the user that runs the Tomcat process. – Harald K Apr 07 '17 at 10:53
  • Thank you for your input. May I ask for more details regarding this? How can I make sure it is readable, is there documentation out there? Also, is there something I can change regarding security options on the VM to allow access to the file located on the machine ? – PGH Apr 07 '17 at 18:47

1 Answers1

0

The VM is external of your system. You need to basically create an image folder in your project and have it read there (/project name/lib name/image name).

Also please see this stackoverflow question that discusses this problem: imageio.IIOException: Can't read input file

Community
  • 1
  • 1
  • Yes absolutely, just think of it as a traditional res folder in desktop applications, or referencing your css stylesheets in a css folder. Same principle applies. – derekjgrove Apr 07 '17 at 19:26