How to save uploaded image in mysql using jsp?

Reply

Join Date: Jun 2009
Posts: 19
Reputation: farahshafilla is an unknown quantity at this point 
Solved Threads: 0
farahshafilla's Avatar
farahshafilla farahshafilla is offline Offline
Newbie Poster

How to save uploaded image in mysql using jsp?

 
0
  #1
Jun 16th, 2009
my code :
********uploadImage.jsp*************
  1. <form action="UploadImage" method="post" enctype="multipart/form-data"
  2. name="productForm" id="productForm"><br><br>
  3. <table width="400px" align="center" border=0 style="background-color:ffeeff;">
  4. <tr>
  5. <td align="center" colspan=2 style="font-weight:bold;font-size:20pt;">
  6. Image Details</td>

******UploadImage.java******
  1. import java.io.*;
  2. import java.sql.*;
  3. import java.util.*;
  4. import java.util.regex.*;
  5. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  6. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  7. import org.apache.commons.fileupload.*;
  8.  
  9. import javax.servlet.*;
  10. import javax.servlet.http.*;
  11.  
  12. public class UploadImage extends HttpServlet {
  13. @Override
  14. public void doPost(HttpServletRequest request, HttpServletResponse response)
  15. throws ServletException, IOException {
  16. PrintWriter out = response.getWriter();
  17. boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  18. System.out.println("request: " + request);
  19. if (!isMultipart) {
  20. System.out.println("File Not Uploaded");
  21. } else {
  22. FileItemFactory factory = new DiskFileItemFactory();
  23. ServletFileUpload upload = new ServletFileUpload(factory);
  24. List items = null;
  25.  
  26. try {
  27. items = upload.parseRequest(request);
  28. System.out.println("items: " + items);
  29. } catch (FileUploadException e) {
  30. e.printStackTrace();
  31. }
  32. Iterator itr = items.iterator();
  33. while (itr.hasNext()) {
  34. FileItem item = (FileItem) itr.next();
  35. if (item.isFormField()) {
  36. String name = item.getFieldName();
  37. System.out.println("name: " + name);
  38. String value = item.getString();
  39. System.out.println("value: " + value);
  40. } else {
  41. try {
  42. String itemName = item.getName();
  43. Random generator = new Random();
  44. int r = Math.abs(generator.nextInt());
  45.  
  46. String reg = "[.*]";
  47. String replacingtext = "";
  48. System.out.println("Text before replacing is:-" + itemName);
  49. Pattern pattern = Pattern.compile(reg);
  50. Matcher matcher = pattern.matcher(itemName);
  51. StringBuffer buffer = new StringBuffer();
  52.  
  53. while (matcher.find()) {
  54. matcher.appendReplacement(buffer, replacingtext);
  55. }
  56. int IndexOf = itemName.indexOf(".");
  57. String domainName = itemName.substring(IndexOf);
  58. System.out.println("domainName: " + domainName);
  59.  
  60. String finalimage = buffer.toString() + "_" + r + domainName;
  61. System.out.println("Final Image===" + finalimage);
  62.  
  63. File savedFile = new File("C:/apache-tomcat-6.0.16/webapps/example/" + "images\\" + finalimage);
  64. item.write(savedFile);
  65. out.println("<html>");
  66. out.println("<body>");
  67. out.println("<table><tr><td>");
  68. out.println("<img src=images/" + finalimage + ">");
  69. out.println("</td></tr></table>");
  70.  
  71. Connection conn = null;
  72. String url = "jdbc:mysql://localhost:3306/";
  73. String dbName = "thundercatz";
  74. String driver = "com.mysql.jdbc.Driver";
  75. String username = "root";
  76. String userPassword = "";
  77. String strQuery = null;
  78. String strQuery1 = null;
  79. String imgLen = "";
  80.  
  81. try {
  82. System.out.println("itemName::::: " + itemName);
  83. Class.forName(driver).newInstance();
  84. conn = DriverManager.getConnection(url + dbName, username, userPassword);
  85. Statement st = conn.createStatement();
  86. strQuery = "insert into testimage values image='" + finalimage + "'";
  87. int rs = st.executeUpdate(strQuery);
  88. System.out.println("Query Executed Successfully++++++++++++++");
  89. out.println("image inserted successfully");
  90. out.println("</body>");
  91. out.println("</html>");
  92. } catch (Exception e) {
  93. System.out.println(e.getMessage());
  94. } finally {
  95. conn.close();
  96. }
  97. } catch (Exception e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }

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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How to save uploaded image in mysql using jsp?

 
0
  #2
Jun 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 19
Reputation: farahshafilla is an unknown quantity at this point 
Solved Threads: 0
farahshafilla's Avatar
farahshafilla farahshafilla is offline Offline
Newbie Poster

Re: How to save uploaded image in mysql using jsp?

 
0
  #3
Jun 16th, 2009
tq...but a blank page appears...
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How to save uploaded image in mysql using jsp?

 
0
  #4
Jun 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 19
Reputation: farahshafilla is an unknown quantity at this point 
Solved Threads: 0
farahshafilla's Avatar
farahshafilla farahshafilla is offline Offline
Newbie Poster

Re: How to save uploaded image in mysql using jsp?

 
0
  #5
Jun 16th, 2009
  1. <html>
  2.  
  3. <head><title>Image Upload</title></head>
  4.  
  5. <body>
  6. <form action="UploadImage" method="post" enctype="multipart/form-data"
  7. name="productForm" id="productForm"><br><br>
  8. <table width="400px" align="center" border=0 style="background-color:ffeeff;">
  9. <tr>
  10. <td align="center" colspan=2 style="font-weight:bold;font-size:20pt;">
  11. Image Details</td>
  12. </tr>
  13.  
  14. <tr>
  15. <td align="center" colspan=2>&nbsp;</td>
  16. </tr>
  17.  
  18. <tr>
  19. <td>Image Link: </td>
  20. <td>
  21. <input type="file" name="file" id="file">
  22. <td>
  23. </tr>
  24.  
  25. <tr>
  26. <td></td>
  27. <td><input type="submit" name="Submit" value="Submit"></td>
  28. </tr>
  29. <tr>
  30. <td colspan="2">&nbsp;</td>
  31. </tr>
  32.  
  33. </table>
  34. </form>
  35. </body>
  36.  
  37. </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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How to save uploaded image in mysql using jsp?

 
0
  #6
Jun 16th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 19
Reputation: farahshafilla is an unknown quantity at this point 
Solved Threads: 0
farahshafilla's Avatar
farahshafilla farahshafilla is offline Offline
Newbie Poster

Re: How to save uploaded image in mysql using jsp?

 
0
  #7
Jun 16th, 2009
actually the blank page appears because the image did not save in the database. ..

the page is:
  1. import java.io.*;
  2. import java.sql.*;
  3. import java.util.*;
  4. import java.util.regex.*;
  5. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  6. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  7. import org.apache.commons.fileupload.*;
  8.  
  9. import javax.servlet.*;
  10. import javax.servlet.http.*;
  11.  
  12. public class UploadImage extends HttpServlet {
  13. @Override
  14. public void doPost(HttpServletRequest request, HttpServletResponse response)
  15. throws ServletException, IOException {
  16. PrintWriter out = response.getWriter();
  17. boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  18. System.out.println("request: " + request);
  19. if (!isMultipart) {
  20. System.out.println("File Not Uploaded");
  21. } else {
  22. FileItemFactory factory = new DiskFileItemFactory();
  23. ServletFileUpload upload = new ServletFileUpload(factory);
  24. List items = null;
  25.  
  26. try {
  27. items = upload.parseRequest(request);
  28. System.out.println("items: " + items);
  29. } catch (FileUploadException e) {
  30. e.printStackTrace();
  31. }
  32. Iterator itr = items.iterator();
  33. while (itr.hasNext()) {
  34. FileItem item = (FileItem) itr.next();
  35. if (item.isFormField()) {
  36. String name = item.getFieldName();
  37. System.out.println("name: " + name);
  38. String value = item.getString();
  39. System.out.println("value: " + value);
  40. } else {
  41. try {
  42. String itemName = item.getName();
  43. Random generator = new Random();
  44. int r = Math.abs(generator.nextInt());
  45.  
  46. String reg = "[.*]";
  47. String replacingtext = "";
  48. System.out.println("Text before replacing is:-" + itemName);
  49. Pattern pattern = Pattern.compile(reg);
  50. Matcher matcher = pattern.matcher(itemName);
  51. StringBuffer buffer = new StringBuffer();
  52.  
  53. while (matcher.find()) {
  54. matcher.appendReplacement(buffer, replacingtext);
  55. }
  56. int IndexOf = itemName.indexOf(".");
  57. String domainName = itemName.substring(IndexOf);
  58. System.out.println("domainName: " + domainName);
  59.  
  60. String finalimage = buffer.toString() + "_" + r + domainName;
  61. System.out.println("Final Image===" + finalimage);
  62.  
  63. File savedFile = new File("C:/apache-tomcat-6.0.16/webapps/example/" + "images\\" + finalimage);
  64. item.write(savedFile);
  65. out.println("<html>");
  66. out.println("<body>");
  67. out.println("<table><tr><td>");
  68. out.println("<img src=images/" + finalimage + ">");
  69. out.println("</td></tr></table>");
  70.  
  71. Connection conn = null;
  72. String url = "jdbc:mysql://localhost:3306/";
  73. String dbName = "thundercatz";
  74. String driver = "com.mysql.jdbc.Driver";
  75. String username = "root";
  76. String userPassword = "";
  77. String strQuery = null;
  78. String strQuery1 = null;
  79. String imgLen = "";
  80.  
  81. try {
  82. System.out.println("itemName::::: " + itemName);
  83. Class.forName(driver).newInstance();
  84. conn = DriverManager.getConnection(url + dbName, username, userPassword);
  85. Statement st = conn.createStatement();
  86. strQuery = "insert into testimage values image='" + finalimage + "'";
  87.  
  88. int rs = st.executeUpdate(strQuery);
  89. System.out.println("Query Executed Successfully++++++++++++++");
  90. out.println("image inserted successfully");
  91. out.println("</body>");
  92. out.println("</html>");
  93. } catch (Exception e) {
  94. System.out.println(e.getMessage());
  95. } finally {
  96. conn.close();
  97. }
  98. } catch (Exception e) {
  99. e.printStackTrace();
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }


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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How to save uploaded image in mysql using jsp?

 
0
  #8
Jun 16th, 2009
Actually, there probably are errors, but they are being printed to STDERR
  1. } catch (Exception e) {
  2. e.printStackTrace();
  3. }
So you need to search your server logfiles.
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 19
Reputation: farahshafilla is an unknown quantity at this point 
Solved Threads: 0
farahshafilla's Avatar
farahshafilla farahshafilla is offline Offline
Newbie Poster

Re: How to save uploaded image in mysql using jsp?

 
0
  #9
Jun 16th, 2009
ooo..ok...
i try to find it...
tq sir
1
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 19
Reputation: farahshafilla is an unknown quantity at this point 
Solved Threads: 0
farahshafilla's Avatar
farahshafilla farahshafilla is offline Offline
Newbie Poster

Re: How to save uploaded image in mysql using jsp?

 
0
  #10
Jun 26th, 2009
how to find to search server logfiles?
i have no idea...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC