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?

Brian
  • 17,079
  • 6
  • 43
  • 66
KaR
  • 53
  • 1
  • 5
  • 1
    I strongly suspect some where in the program image path is hardcoded. – kosa Sep 11 '12 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. – KaR Sep 11 '12 at 19:20
  • Try to write `fis` to a new file and see if it's created... – Nir Alfasi Sep 11 '12 at 19:40
  • @alfasin: I strongly recommend you to also read the answer as you seem to not understand this problem at all either. – BalusC Sep 11 '12 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. – Nir Alfasi Sep 11 '12 at 20:07
  • @alfasin, I have tried that, but in vain. – KaR Sep 12 '12 at 06:27
  • @KaR you tried what ? copying the file to your JSP folder ? what about creating a new file using `fis` - did you successfully copy the original file ? if you didn't - you're not reading the file, and if you did, it's time to move on to the other points BalusC mentioned ;) – Nir Alfasi Sep 12 '12 at 06:58

1 Answers1

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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. – KaR Sep 11 '12 at 19:30
  • 1
    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). – BalusC Sep 11 '12 at 19:32
  • 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. – KaR Sep 11 '12 at 19:35