| | |
How to save uploaded image in mysql using jsp?
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
my code :
********uploadImage.jsp*************
******UploadImage.java******
ERROR :
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
root cause
java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:196)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:358)
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
UploadImage.doPost(UploadImage.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
root cause
java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1360)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1206)
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:196)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:358)
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
UploadImage.doPost(UploadImage.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
********uploadImage.jsp*************
JSP Syntax (Toggle Plain Text)
<form action="UploadImage" method="post" enctype="multipart/form-data" name="productForm" id="productForm"><br><br> <table width="400px" align="center" border=0 style="background-color:ffeeff;"> <tr> <td align="center" colspan=2 style="font-weight:bold;font-size:20pt;"> Image Details</td>
******UploadImage.java******
Java Syntax (Toggle Plain Text)
import java.io.*; import java.sql.*; import java.util.*; import java.util.regex.*; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.*; import javax.servlet.*; import javax.servlet.http.*; public class UploadImage extends HttpServlet { @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); boolean isMultipart = ServletFileUpload.isMultipartContent(request); System.out.println("request: " + request); if (!isMultipart) { System.out.println("File Not Uploaded"); } else { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = null; try { items = upload.parseRequest(request); System.out.println("items: " + items); } catch (FileUploadException e) { e.printStackTrace(); } Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); if (item.isFormField()) { String name = item.getFieldName(); System.out.println("name: " + name); String value = item.getString(); System.out.println("value: " + value); } else { try { String itemName = item.getName(); Random generator = new Random(); int r = Math.abs(generator.nextInt()); String reg = "[.*]"; String replacingtext = ""; System.out.println("Text before replacing is:-" + itemName); Pattern pattern = Pattern.compile(reg); Matcher matcher = pattern.matcher(itemName); StringBuffer buffer = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(buffer, replacingtext); } int IndexOf = itemName.indexOf("."); String domainName = itemName.substring(IndexOf); System.out.println("domainName: " + domainName); String finalimage = buffer.toString() + "_" + r + domainName; System.out.println("Final Image===" + finalimage); File savedFile = new File("C:/apache-tomcat-6.0.16/webapps/example/" + "images\\" + finalimage); item.write(savedFile); out.println("<html>"); out.println("<body>"); out.println("<table><tr><td>"); out.println("<img src=images/" + finalimage + ">"); out.println("</td></tr></table>"); Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "thundercatz"; String driver = "com.mysql.jdbc.Driver"; String username = "root"; String userPassword = ""; String strQuery = null; String strQuery1 = null; String imgLen = ""; try { System.out.println("itemName::::: " + itemName); Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url + dbName, username, userPassword); Statement st = conn.createStatement(); strQuery = "insert into testimage values image='" + finalimage + "'"; int rs = st.executeUpdate(strQuery); System.out.println("Query Executed Successfully++++++++++++++"); out.println("image inserted successfully"); out.println("</body>"); out.println("</html>"); } catch (Exception e) { System.out.println(e.getMessage()); } finally { conn.close(); } } catch (Exception e) { e.printStackTrace(); } } } } } }
ERROR :
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
root cause
java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:196)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:358)
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
UploadImage.doPost(UploadImage.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
root cause
java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1360)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1206)
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
org.apache.commons.fileupload.disk.DiskFileItemFactory.createItem(DiskFileItemFactory.java:196)
org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:358)
org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
UploadImage.doPost(UploadImage.java:36)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
Last edited by peter_budo; Jun 16th, 2009 at 5:45 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Include the jar that contains the class "org/apache/commons/io/output/DeferredFileOutputStream" in the WEB-INF/lib directory of your webapp.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Okay? And what does the page source look like (from the browser View --> Source)? Post that here.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
HTML Syntax (Toggle Plain Text)
<html> <head><title>Image Upload</title></head> <body> <form action="UploadImage" method="post" enctype="multipart/form-data" name="productForm" id="productForm"><br><br> <table width="400px" align="center" border=0 style="background-color:ffeeff;"> <tr> <td align="center" colspan=2 style="font-weight:bold;font-size:20pt;"> Image Details</td> </tr> <tr> <td align="center" colspan=2> </td> </tr> <tr> <td>Image Link: </td> <td> <input type="file" name="file" id="file"> <td> </tr> <tr> <td></td> <td><input type="submit" name="Submit" value="Submit"></td> </tr> <tr> <td colspan="2"> </td> </tr> </table> </form> </body> </html>
did u mean this???
Last edited by peter_budo; Jun 16th, 2009 at 5:46 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Well, that should definately not produce a blank page (unless, maybe, you really are missing that opening "less than" sign). I meant the HTML source of the "blank page" you're talking about.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
actually the blank page appears because the image did not save in the database. ..
the page is:
i think it must be something wrong here.but they are no error..
it makes me going crazy

the page is:
Java Syntax (Toggle Plain Text)
import java.io.*; import java.sql.*; import java.util.*; import java.util.regex.*; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.*; import javax.servlet.*; import javax.servlet.http.*; public class UploadImage extends HttpServlet { @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); boolean isMultipart = ServletFileUpload.isMultipartContent(request); System.out.println("request: " + request); if (!isMultipart) { System.out.println("File Not Uploaded"); } else { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); List items = null; try { items = upload.parseRequest(request); System.out.println("items: " + items); } catch (FileUploadException e) { e.printStackTrace(); } Iterator itr = items.iterator(); while (itr.hasNext()) { FileItem item = (FileItem) itr.next(); if (item.isFormField()) { String name = item.getFieldName(); System.out.println("name: " + name); String value = item.getString(); System.out.println("value: " + value); } else { try { String itemName = item.getName(); Random generator = new Random(); int r = Math.abs(generator.nextInt()); String reg = "[.*]"; String replacingtext = ""; System.out.println("Text before replacing is:-" + itemName); Pattern pattern = Pattern.compile(reg); Matcher matcher = pattern.matcher(itemName); StringBuffer buffer = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(buffer, replacingtext); } int IndexOf = itemName.indexOf("."); String domainName = itemName.substring(IndexOf); System.out.println("domainName: " + domainName); String finalimage = buffer.toString() + "_" + r + domainName; System.out.println("Final Image===" + finalimage); File savedFile = new File("C:/apache-tomcat-6.0.16/webapps/example/" + "images\\" + finalimage); item.write(savedFile); out.println("<html>"); out.println("<body>"); out.println("<table><tr><td>"); out.println("<img src=images/" + finalimage + ">"); out.println("</td></tr></table>"); Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String dbName = "thundercatz"; String driver = "com.mysql.jdbc.Driver"; String username = "root"; String userPassword = ""; String strQuery = null; String strQuery1 = null; String imgLen = ""; try { System.out.println("itemName::::: " + itemName); Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url + dbName, username, userPassword); Statement st = conn.createStatement(); strQuery = "insert into testimage values image='" + finalimage + "'"; int rs = st.executeUpdate(strQuery); System.out.println("Query Executed Successfully++++++++++++++"); out.println("image inserted successfully"); out.println("</body>"); out.println("</html>"); } catch (Exception e) { System.out.println(e.getMessage()); } finally { conn.close(); } } catch (Exception e) { e.printStackTrace(); } } } } } }
i think it must be something wrong here.but they are no error..
it makes me going crazy
Last edited by peter_budo; Jun 16th, 2009 at 6:39 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Actually, there probably are errors, but they are being printed to STDERR
So you need to search your server logfiles.
JSP Syntax (Toggle Plain Text)
} catch (Exception e) { e.printStackTrace(); }
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- How to Connect MySQL from JSP Page (JSP)
- Saving n Retrieving Image in mysql (Java)
- Store and retrieve image from mysql db with JSP (JSP)
- how to save and retrieve an image using mysql and php (PHP)
- Problem displaying path of image from mysql (PHP)
- problem saving image into mysql database (VB.NET)
Other Threads in the JSP Forum
- Previous Thread: data file and servlet program
- Next Thread: server deployment in linex with tomacat server
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish imagetodatabse imageupload internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial video web






