How to save uploaded image in mysql using jsp?

Reply

Join Date: Dec 2004
Posts: 4,118
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 471
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

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

 
0
  #11
Jun 26th, 2009
TOMCAT_INSTALATION_DIRECTORY/logs

So if it is local it can be C:\Tomcat 6.0.18\logs or on hosted server just tomcat/logs
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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
  #12
Jun 27th, 2009
i dont understand....
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,118
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 471
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

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

 
0
  #13
Jun 28th, 2009
What you do not understand? Explain, I'm not mind reader to find out where you failing.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,421
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: 433
adatapost's Avatar
adatapost adatapost is offline Offline
Nearly a Posting Maven

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

 
0
  #14
Jun 28th, 2009
Did you copy a commons-fileupload-1.0.jar into lib folder of your application under WEB-INF?

>What is the datatype of image field?
Failure is not fatal, but failure to change might be. - John Wooden
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

Upload and save image in mysql through JSP

 
0
  #15
Jul 3rd, 2009
hi...i have this code.
but i will display blank page when i run it :
  1. import java.io.*;
  2. import java.sql.*;
  3. import java.util.*;
  4. import java.text.*;
  5. import java.util.regex.*;
  6. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  7. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  8. import org.apache.commons.fileupload.*;
  9.  
  10. import javax.servlet.*;
  11. import javax.servlet.http.*;
  12.  
  13. public class UploadImage extends HttpServlet{
  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/thundercatz";
  73. String dbName = "test";
  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 set 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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,421
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: 433
adatapost's Avatar
adatapost adatapost is offline Offline
Nearly a Posting Maven

Re: Upload and save image in mysql through JSP

 
1
  #16
Jul 3rd, 2009
farahshafilla,

You already own a thread for the same question.
http://www.daniweb.com/forums/thread197747-2.html

Source code you posted here is a Servlet class. Please post jsp/html code and also write the name/version of your web server.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,118
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 471
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

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

 
0
  #17
Jul 3rd, 2009
MERGED

@farahshafilla please read forum rules in regards of posting
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,421
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: 433
adatapost's Avatar
adatapost adatapost is offline Offline
Nearly a Posting Maven

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

 
0
  #18
Jul 4th, 2009
farahshafilla,

You said that there is no output - nothing is to be displayed.
--- Use out.println() method instead of System.out.println()

Incorrect insert query statement:
INSERT INTO tablename VALUES ('A','B')
  1. strQuery = "INSERT INTO testimage VALUES ('" + finalimage + "')";
  2. st.executeUpdate(strQuery);
  3. out.println("Query Executed Successfully++++++++++++++");
  4. out.println("image inserted successfully");
  5. out.println("</body>");
  6. out.println("</html>");
  7. }
  8. catch (Exception e) {
  9. out.println(e.getMessage());
  10. }
  11. finally {
  12. conn.close();
  13. }
  14. }
  15. catch (Exception e) {
  16. out.println(e.getMessage());
  17. }
  18. ...
Failure is not fatal, but failure to change might be. - John Wooden
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
  #19
Jul 5th, 2009
i already change the sql statement and also make out.println() method.
but it still display blank page when i run it
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,421
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: 433
adatapost's Avatar
adatapost adatapost is offline Offline
Nearly a Posting Maven

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

 
0
  #20
Jul 5th, 2009
Hi,

I want to know the name and version of your webserver.

OK! I have a sample program.

This is a .jsp
index.jsp
  1. <html>
  2. <head><title>Image Upload</title></head>
  3. <%
  4. // A message from servlet
  5. String msg=(String)request.getAttribute("message");
  6. if(msg==null) msg="";
  7. %>
  8. <body>
  9. <form action="UploadImage" method="post" enctype="multipart/form-data"
  10. name="productForm" id="productForm"><br><br>
  11. <table width="400px" align="center" border=0 style="background-color:ffeeff;">
  12. <tr>
  13. <td align="center" colspan=2 style="font-weight:bold;font-size:20pt;">
  14. Image Details</td>
  15. </tr>
  16. <tr>
  17. <td align="center" colspan=2>&nbsp;</td>
  18. </tr>
  19. <tr>
  20. <td>Image Link: </td>
  21. <td>
  22. <input type="file" name="file" id="file">
  23. <td>
  24. </tr>
  25. <tr>
  26. <td colspan="2">
  27. <%=msg%>
  28. <input type="submit" name="Submit" value="Submit"></td>
  29. </tr>
  30. <tr>
  31. <td colspan="2">&nbsp;</td>
  32. </tr>
  33. </table>
  34. </form>
  35. </body>
  36. </html>

and UploadImage servlet

  1. package com.me;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import java.sql.*;
  8. import java.util.*;
  9. import java.util.regex.*;
  10. import org.apache.commons.fileupload.*;
  11. import org.apache.tomcat.jni.Directory;
  12.  
  13. public class UploadImage extends HttpServlet {
  14. @Override
  15. public void doPost(HttpServletRequest request, HttpServletResponse response)
  16. throws ServletException, IOException {
  17. String message="";
  18.  
  19. // Path, where uploaded files are to be saved
  20. String uploaddir=getServletContext().getRealPath("/files");
  21. String filename="";
  22. Connection cn=null;
  23. PreparedStatement st=null;
  24. try{
  25. org.apache.commons.fileupload.DiskFileUpload ds=new DiskFileUpload();
  26.  
  27. // Parse the request
  28. List<FileItem> list=ds.parseRequest(request);
  29. Class.forName("com.mysql.jdbc.Driver");
  30. cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jaydb","root","root");
  31.  
  32. // Loop to retrieve form's name-value
  33. for(FileItem a:list)
  34. {
  35. if(a.getContentType()!=null & a.getContentType().startsWith("image/"))
  36. {
  37. // Create a directory if it is not exist
  38. File f=new File(uploaddir);
  39. f.mkdir();
  40. // filename from the parsed request
  41. filename=a.getName();
  42.  
  43. // Write a record - filename and filedata
  44. // CREATE TABLE `testimage` (
  45. // `filename` varchar(100) collate latin1_general_ci NOT NULL,
  46. // `filedata` blob NOT NULL
  47. // ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  48.  
  49. st=cn.prepareStatement("insert into testimage values (?,?)");
  50. st.setString(1, filename);
  51. st.setBlob(2,a.getInputStream());
  52. st.executeUpdate();
  53. st.close();
  54.  
  55. message=" File is uploaded, saved into disk and written into a db. ";
  56.  
  57. // Save a file into /files folder
  58. String filePath=uploaddir + "/" + filename;
  59. FileOutputStream fs=new FileOutputStream(filePath);
  60. fs.write(a.get());
  61. fs.close();
  62. }
  63. }
  64. cn.close();
  65. }catch(Exception ex)
  66. {
  67. request.setAttribute("message", ex.getMessage());
  68. }
  69. // Dispatch a request
  70. request.setAttribute("message",message);
  71. RequestDispatcher reqDisp=request.getRequestDispatcher("index.jsp");
  72. reqDisp.forward(request, response);
  73. }
  74. }

I configured UploadImage servlet - Servlet Mapping
web.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  3. <servlet>
  4. <servlet-name>UploadImage</servlet-name>
  5. <servlet-class>com.me.UploadImage</servlet-class>
  6. </servlet>
  7. <servlet-mapping>
  8. <servlet-name>UploadImage</servlet-name>
  9. <url-pattern>/UploadImage</url-pattern>
  10. </servlet-mapping>
  11. <session-config>
  12. <session-timeout>
  13. 30
  14. </session-timeout>
  15. </session-config>
  16. <welcome-file-list>
  17. <welcome-file>index.jsp</welcome-file>
  18. </welcome-file-list>
  19. </web-app>

and also copy these files -
commons-fileupload-1.0.jar and mysql-connector-java-5.1.5-bin.jar into WEB-INF/lib folder.
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