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

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

 
0
  #31
Jul 6th, 2009
Originally Posted by adatapost View Post
Drop the table you have and create a new one.
i had been follow your advised.
drop and make a new table.
the problem is not settle.
the image still not save in the database and when i click submit,
it tooks about 4 minutes to loading but the image still not save in the database.
am I too bad?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 5
Reputation: swathirao is an unknown quantity at this point 
Solved Threads: 1
swathirao swathirao is offline Offline
Newbie Poster

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

 
0
  #32
Jul 14th, 2009
Originally Posted by farahshafilla View Post
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)
hi
  1. <html>
  2.  
  3. <head><title>Image Upload</title></head>
  4.  
  5. <body>
  6. <form action="insertImage11.jsp" method="post" enctype="multipart/form-data" name="productForm" id="productForm"><br><br>
  7. <table width="400px" align="center" border=0 style="background-color:ffeeff;"><tr>
  8. <td align="center" colspan=2 style="font-weight:bold;font-size:20pt;">Image Details</td>
  9. </tr>
  10. <tr>
  11. <td align="center" colspan=2>&nbsp;</td>
  12. </tr>
  13. <tr>
  14. <td>Image Link: </td>
  15. <td>
  16. <input type="file" name="file" id="file">
  17. <td>
  18. </tr>
  19. <tr>
  20. <td></td>
  21. <td><input type="submit" name="Submit" value="Submit"></td>
  22. </tr>
  23. <tr>
  24. <td colspan="2">&nbsp;</td>
  25. </tr>
  26. </table>
  27. </form>
  28. </body>
  29. </html>
  30. <%@ page import="java.util.List" %>
  31. <%@ page import="java.util.Iterator" %>
  32. <%@ page import="java.io.File" %>
  33. <%@ page import="java.io.*" %>
  34. <%@ page import="java.util.*" %>
  35. <%@ page import="javax.servlet.ServletException.*" %>
  36. <%@ page import="javax.servlet.*"%>
  37. <%@ page import="org.apache.commons.fileupload.*"%>
  38. <%@ page import="org.apache.commons.fileupload.disk.*"%>
  39. <%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
  40. <%@ page import="org.apache.commons.fileupload.disk.*"%>
  41. <%@ page import="org.apache.commons.fileupload.servlet.*"%>
  42. <%@ page language="java" import="java.sql.*;"%>
  43. <%
  44. Connection connection = null;
  45. String connectionURL = "jdbc:mysql://localhost/vrc";
  46. ResultSet rs = null;
  47. PreparedStatement pre = null;
  48. try
  49. {
  50. Class.forName("com.mysql.jdbc.Driver");
  51. connection = DriverManager.getConnection(connectionURL, "javavrc", "vrc07");
  52. //FileUpload fup = new FileUpload();
  53. //FileUpload fup=new FileUpload();
  54. //DiskFileUpload upload=new DiskFileUpload();
  55. DiskFileItemFactory factory = new DiskFileItemFactory();
  56. ServletFileUpload upload = new ServletFileUpload(factory);
  57. boolean isMultipart = ServletFileUpload.isMultipartContent(request);
  58. List items = upload.parseRequest(request);
  59. Iterator iter = items.iterator();
  60.  
  61. int count = 0;
  62. while (iter.hasNext()) {
  63. count++;
  64. FileItem item = (FileItem) iter.next();
  65. File cfile = new File(item.getName());
  66. File tosave = new File(getServletContext().getRealPath("/temp/"), cfile.getName());
  67. item.write(tosave);
  68. String file_name = item.getName();
  69. FileInputStream fis = new FileInputStream(tosave);
  70. int len = (int) tosave.length();
  71. pre= connection.prepareStatement("insert into image(imageName, image) "+ "values(?,?)");
  72. pre.setString(1,"Delhisswa");
  73. fis = new FileInputStream(tosave);
  74. //image_data column holds LONGBLOB data type
  75.  
  76. pre.setBinaryStream(2,fis,(int)tosave.length());
  77. int rows = pre.executeUpdate();
  78. }
  79. }
  80.  
  81. catch (Exception ex)
  82.  
  83. {
  84. out.println(ex.getMessage());
  85. }
  86. %>
this code use for u .
Last edited by peter_budo; Jul 14th, 2009 at 5:33 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: Oct 2008
Posts: 2,552
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 452
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

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

 
0
  #33
Jul 14th, 2009
swathirao,

Use code tags. Source code must be surrounded with code tags.
For example,

[CODE=Jsp]
... statements..
[/CODE]
Failure is not fatal, but failure to change might be. - John Wooden
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