help in uploading image

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jatin299
    New Member
    • Feb 2008
    • 1

    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.
    [CODE=Java]
    import java.sql.*;
    import java.io.*;

    import javax.servlet.* ;
    import javax.servlet.h ttp.*;

    public class LoadImage extends HttpServlet{

    public void doPost(HttpServ letRequest req, HttpServletResp onse res) throws ServletExceptio n, IOException
    {
    entrance(req, res);
    }
    public void doGet(HttpServl etRequest req, HttpServletResp onse res) throws ServletExceptio n, IOException
    {
    entrance(req, res);
    }
    public void entrance(HttpSe rvletRequest req, HttpServletResp onse res) throws ServletExceptio n, IOException
    {
    res.setContentT ype("text/html");
    PrintWriter out = res.getWriter() ;
    String photo=req.getPa rameter("photo" );

    Connection conn = null;
    PreparedStateme nt ps = null;
    try
    {
    Class.forName( "com.mysql.jdbc .Driver" );

    conn = DriverManager.g etConnection("j dbc:mysql://localhost:3306/first","root"," root");
    ps = conn.prepareSta tement("INSERT INTO uplo (photo) VALUES( ? )" );


    File image = new File( photo );
    FileInputStream fis = new FileInputStream ( image );
    ps.setBinaryStr eam(1, fis, ( int )image.length() );

    // Execute the INSERT
    int count = ps.executeUpdat e();
    System.out.prin tln( "Rows inserted: " + count );
    }
    catch( Exception e )
    {
    e.printStackTra ce();
    }
    finally
    {
    try
    {
    if( ps != null ) ps.close();
    if( conn != null ) conn.close();
    }
    catch( Exception ee )
    {
    ee.printStackTr ace();
    }
    }
    }
    }[/CODE]
    Last edited by BigDaddyLH; Feb 20 '08, 04:54 PM. Reason: added code tags
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    So what's the error? Please describe in detail.
    Or else nobody is able to help!

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      [CODE=Java]File image = new File( photo );
      FileInputStream fis = new FileInputStream ( image );[/CODE]

      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:

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        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

        Comment

        Working...