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?
Related
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
I have written a basic jsp code for storing and retrieving the data from db.
Before that am checking validation of user.
When i click submit button it will redirect to my jsp page.
i have written a db.properties file separately.
When i gave complete path to read properties file., program is executing fine. (Which is not best way to hard code like below).
FileInputStream in = new FileInputStream("C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ServiceDisplay\db.properties");
But when i specify only "db.properties" like
(FileInputStream in = new FileInputStream("db.properties");) program is not executing i got file not found exception.
Please not this properties file is in current working dir only. (i.e., my db.properties file and my jsp file is under ServiceDisplay
I tried to changing the file name as "//db.properties", "/db.properties", "./db.properties", "\db.properties", .\db.properties, ../db.properties", "..\db.properties" .
But still i am getting java.io.FileNotFoundException: db.properties
The file 'ServiceDisplay\db.properties' is in your resources directory, to load this file is necessary to use the getResoursceAsStream. Code example below:
ServletContext context = request.getSession().getServletContext();
InputStream is = context.getResourceAsStream("/db.properties");
I am trying to record a session in which there is a upload functionality.
I have used http proxy server for recording, recording controller and http cookie manager.
When I try to upload a file and click save it shows following error:
java.io.FileNotFoundException: a12-13538_110q.htm (The system cannot
find the file specified) at java.io.FileInputStream.open(Native
Method) at java.io.FileInputStream.(Unknown Source) at
java.io.FileInputStream.(Unknown Source) at
org.apache.jmeter.protocol.http.sampler.PostWriter.writeFileToStream(PostWriter.java:408)
at
org.apache.jmeter.protocol.http.sampler.PostWriter.sendPostData(PostWriter.java:117)
at
org.apache.jmeter.protocol.http.sampler.HTTPJavaImpl.sendPostData(HTTPJavaImpl.java:115)
at
org.apache.jmeter.protocol.http.sampler.HTTPJavaImpl.sample(HTTPJavaImpl.java:510)
at
org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:62)
at
org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1060)
at org.apache.jmeter.protocol.http.proxy.Proxy.run(Proxy.java:238)
This functionality works fine when I am not using jmeter recording.
Kindly help on this.
Is there any way I can perform this test?
Bug 50079: Jmeter does not record the file path.
As per your exception jmeter cannot find file to upload, because of issue mentioned above.
So you have to set path manually in sampler, and it's better to use variable for this, for test parametrization.
Don't use record-and-playback at all, or at least check and adjust your recorded samplers.
In your case you have to set in recorded http [POST] request following:
Use multipart/form-data for POST = true - to sent file as part of request;
Send Files with Request -> File Path = PATH_TO_FILE - path to existent file, directly or using variable.
. . .
UPLOAD HTTP Request
Method = POST
Use multipart/form-data for POST = true
-- Send Files with Request -- section:
File Path = ${testFile}
Parameter Name = datafile
MIME Type = ...
. . .
This is related to the defect mentioned above. But the workaround is (On windows) to place the file that is being uploaded in the same folder as the JMX file is. (The file path is not captured only the file name is captured here. Hence need to update the correct absolute or relative path accordingly once the requests are captured.)
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();
I have the following line in my Java app, and it's causing the following error message :
WebBrowser webBrowser=new WebBrowser();
org.jdesktop.jdic.init.JdicInitException: java.io.IOException: The filename, directory name, or volume label syntax is incorrect
at org.jdesktop.jdic.init.JdicManager.initBrowserNative(Unknown Source)
at org.jdesktop.jdic.browser.WebBrowser.<clinit>(Unknown Source)
The app still works, but I wonder if there is a way to correct the error ?
There's a problem initializing jdic's native browser but it isn't obvious what it is.
Does passing a URL to the constructor help?
URL url = new URL("http://www.stackoverflow.com");
WebBrowser webBrowser = new WebBrowser();
One more thing to check - do you have a default browser? If so, when you double click on an html file in a file manager does the browser window open? If this doesn't work then perhaps jdic is complaining about your default browser path.