Results 1 to 5 of 5
  1. #1
    johniem is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default Trying upload file

    I'm trying ti upload a jpg file through a multipart form.

    XML Code:
    form name="formEdit" method="post" action="CRUDEdit.jsp" enctype="multipart/form-data">
    	<input type="hidden" id="memUserId" name="memUserId" value="<%=request.getParameter("memUserId") %>" />
    	<input type="hidden" id="crudAction" name="crudAction" value="<%=crudAction %>"/>
    <input type="text" size="100" name="recID" id="recID"/>
    <input type="file" name="recIMG" id="recIMG"/>
    <input type="button" value="Insert" onclick="crudAction.value='INSERT';submit();"/>
    Java Code:
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if(isMultipart){
    	
    	FileItemFactory factory = new DiskFileItemFactory();
    
    	ServletFileUpload upload = new ServletFileUpload(factory);
    
    	List items = upload.parseRequest(request);
    
    	Iterator iter = items.iterator();
    
    while (iter.hasNext()) {
    	    FileItem item = (FileItem) iter.next();
     if (item.isFormField()) {
    String name = item.getFieldName();
    	    	 
    	    	 if(name.equals("crudAction"))	    	 
    	    		 crudAction = item.getString();
    	    	 else
    	    		 if(name.equals("memUserId"))
    	    			 memUserId = item.getString();
    } else {
    	    	
    	    	fieldName = item.getFieldName();
    	       	fileName = item.getName();
    	        contentType = item.getContentType();
    	        sizeInBytes = item.getSize();
    	        uploadFile = item.get();
    	        upItem = item;
    	    }
         }
    }else{
    	
    	crudAction = request.getParameter("crudAction");
    	memUserId = request.getParameter("memUserId");
    }
    
    query_INS = "INSERT INTO TEMPLATES (ID, IMG)" +
    				" VALUES ( '" + recID + "',?)";
    
    if (crudAction.equals("INSERT")) {
    
    			try {
    				Connection con = DriverManager.getConnection(url, dbuser, dbpassword);
    				PreparedStatement pstmt = con.prepareStatement(query_INS);
    				
    				if(fileName != null && !fileName.equals("")){	
    					
    					InputStream istream = upItem.getInputStream();
    					pstmt.setBlob(1, istream, sizeInBytes );
    				}
    				out.println(query_INS);
    				ret = pstmt.executeUpdate();
    				pstmt.close();
    				con.close();
    				
    			} catch(SQLException ex) {
    				logger.error("SQLException [INSERT] in crud/templates/CRUDEdit.jsp: ",ex);				
    			}
    			
    		if (ret==1)
    			out.write("<DIV align='center'> RECORD INSERTED </DIV>");
    		else
    			out.write("<DIV align='center'> ERROR - NO RECORD INSERTED </DIV>");
    	}
    The above is a snipset of the most crucial part of my code.
    The problem is that tomcat throws a wrong query exception on the IMG insert field but I really can't figure out what i'm doing wrong in my code to upload the image and store it in the database in the blob IMG field. Here's the error:

    ERROR 30 ╔ΎΫΊ 2010 16:39:20,531 http-8091-Processor25 gr.marketing.beans.BeanUse
    r - SQLException [INSERT] in crud/templates/CRUDEdit.jsp:
    com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: You have an error in
    your SQL syntax; check the manual that corresponds to your MySQL server version
    for the right syntax to use near ''test',_binary'????\0►JFIF\0☺☻☺\0H\0H\0\0??◄\0
    Exif\0\0MM\0*\0\0\\0☺↕\0♥\0\0\0' at line 1

    Any help whould be really appriciated.

    Thanks in advance.

  2. #2
    arun9683 is offline Member
    Join Date
    Jun 2010
    Location
    Bangalore,India
    Posts
    70
    Rep Power
    0

    Default

    Instead of
    pstmt.setBlob(1, istream, sizeInBytes );

    use the below statement
    pstmt.setBinaryStream(1, istream, (int) (fImage.length()));

  3. #3
    johniem is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    I've tried that but still the same.. I really can't understand why the prepare statement can't set the binary stream in the query correctly, if this is the problem...

  4. #4
    arun9683 is offline Member
    Join Date
    Jun 2010
    Location
    Bangalore,India
    Posts
    70
    Rep Power
    0

    Default

    Refer the below snippet.. Its working for me..

    Java Code:
    File fImage = new File(logopath);
    			FileInputStream fStream = new FileInputStream(fImage);
    
    			PreparedStatement ps = con
    					.prepareStatement("insert into  myTable(COMPANY_LOGO) values(?)");
    			ps.setBinaryStream(1, fStream, (int) (fImage.length()));
    ps.executeUpdate();
    Arun K R,Bangalore,India
    :)

  5. #5
    johniem is offline Member
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Default

    In case i use the apache FileItemStream to proccess the upload file and store it to db how ca i get the size to set it in the prepare statement?
    I know that i can't know the size of a stream..

Similar Threads

  1. file upload in jsp
    By mudit222 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-20-2010, 08:10 PM
  2. :large file upload to server(chunk upload)
    By tommy_725 in forum Networking
    Replies: 0
    Last Post: 10-16-2009, 12:21 AM
  3. how to upload a file?
    By tommy in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 06-30-2008, 02:50 PM
  4. file upload
    By sundarjothi in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-27-2008, 11:52 AM
  5. File Upload
    By ShoeNinja in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-09-2007, 10:09 PM

Posting Permissions

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