0

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?

7
  • 1
    I strongly suspect some where in the program image path is hardcoded. Commented Sep 11, 2012 at 19:17
  • Thankyou Nambari for taking time to answer.But as i mentioned above, the same program without any changes works in my friends computer. Commented Sep 11, 2012 at 19:20
  • Try to write fis to a new file and see if it's created... Commented Sep 11, 2012 at 19:40
  • @alfasin: I strongly recommend you to also read the answer as you seem to not understand this problem at all either. Commented Sep 11, 2012 at 19:53
  • @BalusC I was thinking about the same problem you mentioned at #2 (relative path). I got to admit I didn't think about the other things you mentioned. For @KaR, btw, the file has to be located in the same folder as your JSP file is - not in any other desktop/root folder. Commented Sep 11, 2012 at 20:07

1 Answer 1

3

There are at least two serious conceptual mistakes here.

  1. 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).

  2. 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot, but I have tried to insert the image from the same directory as the application is sitting in. The result is the same. I have tried this entire process without the eclipse IDE and with it. The error is always the same. The question that i have not been able to answer to myself is WHY IS IT THAT WHEN ON A ANOTHER MACHINE,IT WORKS WITH THE IMAGE BEING SELECTED FROM ANYWHERE ON THE HDD.
In other words, you aren't understanding the concrete problem at all. As long as you don't understand a problem, solving it would be a very long and hard process. Stop shouting rudily and carefully go through the three provided links (the first and second one should explain why it has failed for you and the third one shows how to do it the right way).
Hmmm... Thanks a lot.I Really appreciate for your answers. I will try all the things you have mentioned and get back if I'm facing any problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.