473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

help in uploading image

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
3 2030
chaarmann
785 Expert 512MB
So what's the error? Please describe in detail.
Or else nobody is able to help!
Feb 20 '08 #2
BigDaddyLH
1,216 Expert 1GB
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
1,216 Expert 1GB
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

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

Similar topics

6
by: Ralph Freshour | last post by:
I'm trying to code the ability for my users to upload up to photo's to mysql database - can someone point me in the right direction as to how this might be done in php? Perhaps a tutorial or some...
5
by: ok | last post by:
Hello, Q: How do I get image width and height before uploading an image? This because, I want to restrict people uploading huge files. Thanks in advance
5
by: Tank | last post by:
I have had this post up here when i was trying to figure out how to make leading zeros and have been able to fudge that to work. I am now have trouble getting the loop that makes the folders to...
11
by: bissatch | last post by:
Hi, I am trying to upload an image, create a new file based on that image and then store the base64 encoded image data in a database. I dont really know where my code is going wrong so I will...
10
by: John Smith | last post by:
I know that uploading an image to a database has been covered, oh, about 3 trillion times. However, I haven't found anything covering uploading to a MySQL database with .net. Please don't...
0
by: Alex | last post by:
my app was working fine in VB.NET 2003 (and framework 1.1). Now with VB.NET 2005 (framework 2.0) the uploading to an http server (ie. www.sharebigfile.com) stops with the error "The request was...
3
by: peterf | last post by:
Posted in alt.comp.lang.php before I saw how much more active this group is... Hope someone can help. I am new to this group and needing a little bit of guidance. First off I need to say that...
3
by: Charlie | last post by:
Hi: I'm working on an application that uploads images into binary fields on SQL Server. Before submitting, I would like to validate size of image (ie, 75 pixels by 75 pixels) so that images are...
1
pezholio
by: pezholio | last post by:
Hi, It seems that every time I put together a new script to upload a file I always have problems, here's the latest one: I've got a form with two file input fields, when I submit the form,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.