SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    unable to update records of blob using Commons FileUpload

    getting error as "sorry. No file uploaded" in stdout file in tomcat6/logs. i am able to insert and retrieve all details with images successfully. plz help me. using oracle 10g xe,apache 6.

    my sql table is
    Code:
    SQL> desc product;
     Name                                      Null?    Type
     ----------------------------------------- -------- -----------------
    
     PRODUCT_DIV                               NOT NULL VARCHAR2(8)
     PRODUCT_NAME                              NOT NULL VARCHAR2(8)
     PRODUCT_ID                                NOT NULL NUMBER(8)
     PRODUCT_PHOTO                             NOT NULL BLOB
     PRODUCT_DES                               NOT NULL VARCHAR2(80)
    my html code for accepting details is

    Code:
    <html>
    <script src="insert.js" language="javascript">
    </script>
    <body>
    <form name=f action='./update' method='post' onSubmit="return isValid(this)">
    <table border=1 cellpadding =7 cellspacing=7 align=center>
    <caption><i><font size=4><u>update records</caption>
    <tr>
    <th>product division <th><input type="number" name="product_div"></tr>
    <tr>
    <th>product name <th><input type="text" name="product_name"></tr>
    <tr>
    <th>product id <th><input type="number" name="product_id"></tr>
    <tr>
    <th>Select Photo <th><input type="file"  name="photo"/> </tr>
    <tr>
    <th>product description<th><input type="text" name="product_des"></tr>
    <tr>
    <th>click<th><input type="submit" value="update"></tr>
    </table>
    </form>
    </body>
    </html>
    and my servlet code is
    Code:
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.util.List;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.commons.fileupload.FileItem;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;
    
    public class updatedemo extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try {
                // Apache Commons-Fileupload library classes
                DiskFileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload sfu  = new ServletFileUpload(factory);
    
                if (! ServletFileUpload.isMultipartContent(request)) {
                    System.out.println("sorry. No file uploaded");
                    return;
                }
    
                // parse request
                List items = sfu.parseRequest(request);
                FileItem  product_div = (FileItem) items.get(0);
                String productdiv =  product_div.getString();
    
    			FileItem product_name = (FileItem) items.get(1);
                String   productname =  product_name.getString();
    
    			FileItem  product_id = (FileItem) items.get(2);
                String productid =  product_id.getString();
                
                
                // get uploaded file
                FileItem file = (FileItem) items.get(3);
    
    			FileItem product_des = (FileItem) items.get(4);
                String   productdes =  product_des.getString();
                            
                // Connect to Oracle
                Class.forName("oracle.jdbc.driver.OracleDriver");
                Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "tiger");
                con.setAutoCommit(false);
    
                PreparedStatement ps = con.prepareStatement("update product set product_div=?,product_name=?,product_id=?,product_photo=?,product_des=?");
                ps.setString(1, productdiv);
    			ps.setString(2, productname);
                ps.setString(3, productid);
                // size must be converted to int otherwise it results in error
                ps.setBinaryStream(4, file.getInputStream(), (int) file.getSize());
    			ps.setString(5, productdes);
                ps.executeUpdate();
    			ps.close();
                con.commit();
                con.close();
                out.println("Product details updated sucessfully. <p> <a href='listphotos'>List Photos </a>");
            }
            catch(Exception ex) {
                out.println( "Error --> " + ex.getMessage());
            }
        } 
    }

  2. #2
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.cs.tut.fi/~jkorpela/forms/file.html

    I believe you need to add the enctype="multipart/form-data" attribute to your form.
    baby steps... baby steps...

  3. #3
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @hooknc thank you. you saved my day it was my last day for project submission and thanks to you it's done now.
    As you said i added enctype="multipart/form-data" in form elemt of html and its done.

  4. #4
    SiteP0int Weazle hooknc's Avatar
    Join Date
    Dec 2004
    Location
    Socialist Republic of Boulder
    Posts
    937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Fantastic.

    Congrats on getting your project done.
    baby steps... baby steps...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •