By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,311 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

help in uploading image

P: 1
hi ..problem in uploading image..using servlet to upload image in mysql..use html form so user given the path of image..but giving error.here is the code..help me on this.
Expand|Select|Wrap|Line Numbers
  1. import java.sql.*;
  2. import java.io.*;
  3.  
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6.  
  7. public class LoadImage extends HttpServlet{
  8.  
  9. public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
  10. {
  11. entrance(req, res);
  12. }
  13. public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
  14. {
  15. entrance(req, res);
  16. }
  17. public void entrance(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
  18. {
  19. res.setContentType("text/html");
  20. PrintWriter out = res.getWriter();
  21. String photo=req.getParameter("photo");
  22.  
  23. Connection conn = null;
  24. PreparedStatement ps = null;
  25. try
  26. {
  27. Class.forName( "com.mysql.jdbc.Driver" );
  28.  
  29. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/first","root","root");
  30. ps = conn.prepareStatement("INSERT INTO uplo (photo) VALUES( ? )" );
  31.  
  32.  
  33. File image = new File( photo );
  34. FileInputStream fis = new FileInputStream( image );
  35. ps.setBinaryStream(1, fis, ( int )image.length() );
  36.  
  37. // Execute the INSERT
  38. int count = ps.executeUpdate();
  39. System.out.println( "Rows inserted: " + count );
  40. }
  41. catch( Exception e )
  42. {
  43. e.printStackTrace();
  44. }
  45. finally
  46. {
  47. try
  48. {
  49. if( ps != null ) ps.close();
  50. if( conn != null ) conn.close();
  51. }
  52. catch( Exception ee )
  53. {
  54. ee.printStackTrace();
  55. }
  56. }
  57. }
  58. }
Feb 20 '08 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 785
So what's the error? Please describe in detail.
Or else nobody is able to help!
Feb 20 '08 #2

BigDaddyLH
Expert 100+
P: 1,216
Expand|Select|Wrap|Line Numbers
  1. File image = new File( photo );
  2. FileInputStream fis = new FileInputStream( image );
I don't see how this will work -- or are you assuming the client is sending paths on the server to the web app? The correct way to upload a file is to use something like Apache Commons FileUpload:

http://commons.apache.org/fileupload/
Feb 20 '08 #3

BigDaddyLH
Expert 100+
P: 1,216
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Feb 20 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.