简体   繁体   中英

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.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM