HTTP Status 500 - Cannot read input file - java

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.

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

Related

Box Java SDK - Download OutOfMemoryError when downloading 500MB file

java-sdk-2.8.1.jar from my Java program. In this program using the com.box.sdk.BoxFile download api I download the file from box and process it in my application. The code below works fine with smaller files however when downloading 500 MB file I get java.lang.OutOfMemory error at line file.download(outputStream). Please advise.
public void downloadFile(BoxAPIConnection boxConn,String boxFileID, ByteArrayOutputStream outputStream) throws Exception {
BoxFile file = new BoxFile(boxConn, boxFileID);
file.download(outputStream);
}

Unable to convert .docx to pdf using JODconverter 2. 2.1 with Apache Open Office

Apache Open Office service running on port 8100 Input file is .docx
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
connection.disconnect();
Getting Exception
Exception in thread "main" java.lang.IllegalArgumentException: unknown document format for file: D:\vctest\EDItestingprocess.docx
at com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter.guessDocumentFormat(AbstractOpenOfficeDocumentConverter.java:121)
at com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter.convert(AbstractOpenOfficeDocumentConverter.java:93)
at com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter.convert(AbstractOpenOfficeDocumentConverter.java:74)
at com.artofsolving.jodconverter.openoffice.converter.AbstractOpenOfficeDocumentConverter.convert(AbstractOpenOfficeDocumentConverter.java:70)
at com.redrock.util.ExcelToPdf.main(ExcelToPdf.java:18)
1st start openoffice using cmd.
If you already have a servlet container/application server you can download the jodconverter-webapp-x.y.z.zip distribution that contains a war archive ready for deployment.
For example with Tomcat 4.1 or 5.5 all you have to do is
optionally rename jodconverter-webapp-x.y.z.war to something more user-friendly since the name will be visible in the URL, e.g. converter.war
copy it into the webapps folder inside the Tomcat installation
it will be automatically deployed and visible at (tipically) http://localhost:8080/converter/
if required, configure your firewall or servlet container to restrict access to the webapp.

Azure : Image Magick gives 0kb output?

I have integrated Image magick with a Java WebApp and have deployed it on Azure App Service. On azure , I am getting 0kb as output image for a image while the same image gets converted fine on my local machine.
I am using im4java for integration with Image Magick.
Below is the code:
public void compressImage(String imageSource, String destinationPath) throws IOException, InterruptedException,
IM4JavaException {
ConvertCmd cmd = getCommand();
// create the operation, add images and operators/options
IMOperation op = new IMOperation();
op.strip();
op.interlace();
op.addRawArgs(compressionRawArguments);
op.gaussianBlur(compressionGaussianBlur);
op.quality(compressedImageQuality);
op.addImage(imageSource); // source file
op.addImage(destinationPath); // destination file
// execute the operation
cmd.run(op);
}
Both imageSource and destination are temp files created using java. I have checked that imageSource file has correct size but after running this code, the destination file is always 0 Kb.
Please advise what could I be doing wrong?
Answering my own question so that It might be helpful for fellow developers who might face this problem.
Azure App Service normally has Windows Server VMs. You can check the OS of your server in web container logs.
Image Magick for windows does not allow conversion of remote http
image urls while for Unix System, it allows so. My Local machine is
MAC So it was working correctly on my local system.
2 Solutions to this problem that I found:
Either you use a linux VM on Azure
In your application, download the image URL to a temp file and
supply the absolute path of that temp file to image magick for
conversion.
I have tried both and are both working.

Unable to Insert image to mysql database using servlet

Could anyone please help me out with the following?
I'm trying to insert an image to the blob column in mysql database through a servlet.
I'm selecting the image through the "HTML FILE INPUT TYPE" which is in a JSP file.So the full path of the image is selected.
The error I have been getting is:
HTTP Status 500 - Desert.jpg (The system cannot find the file specified)
type Exception report
message Desert.jpg (The system cannot find the file specified)
description The server encountered an internal error (Desert.jpg (The system cannot find the file specified)) that prevented it from fulfilling this request.
exception
java.io.FileNotFoundException: Desert.jpg (The system cannot find the file specified)
java.io.FileInputStream.open(Native Method)
java.io.FileInputStream.<init>(Unknown Source)
Image.ImgInsert.doGet(ImgInsert.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.28 logs.
Apache Tomcat/7.0.28
Where "Desert.jpg" is the image which is on my desktop.
This same program works on my friends computer.
Here is the servlet code:
String s_id = request.getParameter("myid");
int id = Integer.parseInt(s_id);
//IMAGE ACQUIRED FROM THE FROM THE JSP PAGE
String img = request.getParameter("myimg");
File f = new File(img);
FileInputStream fis = new FileInputStream(f);
String query="insert into images.imageinsert(id,image) values(?,?)";
try
{
PreparedStatement pStatement = conn.prepareStatement(query);
pStatement.setInt(1, id);
pStatement.setBinaryStream(2, fis);
int result = pStatement.executeUpdate();
if(result != 0)
{
response.sendRedirect("ImageHome.html");
}
pStatement.close();
Could anyone please help me out?
There are at least two serious conceptual mistakes here.
You seem to think that having the client side local disk file system path is sufficient to obtain the entire file contents in the server side. This is impossible as the client and server don't share the same disk file system (unless they both happen to run on physically the same computer, which of course don't occur in real world).
You're relying on relative paths in java.io stuff. It becomes relative to the so-called "Current Working Directory" which is the folder which is been opened at exactly that moment the webserver was started. This is definitely not the folder where the webapplication is directly sitting in. This is for example C:\path\to\tomcat\bin. The uploaded file surely isn't magically been placed in there.
As to why it works on the machine of your "friend", that's undoubteldly because he's using Internet Explorer which has a security bug wherein the full file path instead of only the file name is been sent as request parameter on an <input type="file"> field of a <form> without enctype="multipart/form-data". Again, as answered, this approach surely won't work in a real production environment.
Your concrete problem can be understood and solved by carefully reading the following answers.
How to get the file path from HTML input form in Firefox 3
getResourceAsStream() vs FileInputStream
How to upload files to server using JSP/Servlet?

Deployment in tomcat

i am getting a problem
i have deployed a war file, when i run localy through tomcat it works fine but when i run on another system by giveing my system ip and then project folder e.g
http:\192.168.0.145\DllTest it loads the applet but when i click on a button to load the functionality it is throwing an exception
Exception in thread "AWT-EventQueue-3" java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: http:\192.168.0.145:8080\DllTest\lib\jinvoke.dll
while it is working fine localy but not in another system. Please tell me what is the problem.
Is it a rights issue or something else.
You cannot load a DLL on an external host. It has to be an absolute disk file system -as the exception message already hints. Your best bet is to download it manually, create a temp file and load it instead.
File dllFile = File.createTempFile("jinvoke", ".dll");
InputStream input = new URL(getCodeBase(), "lib/jinvoke.dll").openStream();
OuptutStream output = new FileOutputStream(dllFile);
// Write input to output and close streams the usual Java IO way.
// Then load it using absolute disk file system path.
System.loadLibrary(dllFile.getAbsolutePath());
dllFile.deleteOnExit();

Categories