-
Trying upload file
I'm trying ti upload a jpg file through a multipart form.
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();"/>
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.
-
Instead of
pstmt.setBlob(1, istream, sizeInBytes );
use the below statement
pstmt.setBinaryStream(1, istream, (int) (fImage.length()));
-
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...
-
Refer the below snippet.. Its working for me..
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();
-
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..