Results 1 to 5 of 5
Thread: Trying upload file
- 06-30-2010, 03:14 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
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();"/>The above is a snipset of the most crucial part of my code.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 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.
- 07-01-2010, 06:53 AM #2
Member
- Join Date
- Jun 2010
- Location
- Bangalore,India
- Posts
- 70
- Rep Power
- 0
Instead of
pstmt.setBlob(1, istream, sizeInBytes );
use the below statement
pstmt.setBinaryStream(1, istream, (int) (fImage.length()));
- 07-01-2010, 09:34 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
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...
- 07-01-2010, 09:59 AM #4
Member
- Join Date
- Jun 2010
- Location
- Bangalore,India
- Posts
- 70
- Rep Power
- 0
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
:)
- 07-01-2010, 10:31 AM #5
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
file upload in jsp
By mudit222 in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 04-20-2010, 08:10 PM -
:large file upload to server(chunk upload)
By tommy_725 in forum NetworkingReplies: 0Last Post: 10-16-2009, 12:21 AM -
how to upload a file?
By tommy in forum JavaServer Pages (JSP) and JSTLReplies: 4Last Post: 06-30-2008, 02:50 PM -
file upload
By sundarjothi in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-27-2008, 11:52 AM -
File Upload
By ShoeNinja in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-09-2007, 10:09 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks