user UrlConnection to upload multipart

Discussion in 'Java' started by autogoor@yahoo.com, Feb 26, 2005.

  1. Guest

    I am wrting a client side program to simulate the web browser form
    upload function. The goal is to send multipart content to server. I
    found some code online. The basic idea is to send http header
    information, then send file content as stream, and then read server
    response. I found the the files are not even send to the server until
    we read server response. This make my work hard, because I need to set
    up a progressbar to monitor the uploading. I do not know where to put
    it.

    Here is my code...



    URL u = new URL(urlString);
    URLConnection c = u.openConnection();
    c.setDoOutput(true);
    c.setDoInput(true);
    c.setUseCaches(false);
    c.setRequestProperty(
    "Connection",
    "Keep-Alive");
    c.setRequestProperty(
    "HTTP_REFERER",
    codebase);
    c.setRequestProperty(
    "Content-Type",
    "multipart/form-data; boundary=" + boundary);

    DataOutputStream dstream = new
    DataOutputStream(c.getOutputStream());
    dstream.writeBytes("--" + boundary + "\r\n");

    ...post file and parameters
    dstream.flush();
    dstream.close();

    //******if we do not read the server response as bellow, the files are
    not upload to server.
    try{
    DataInputStream in = new DataInputStream(new
    BufferedInputStream(c.getInputStream()));
    String sIn = in.readLine();
    while(sIn!=null){
    if(sIn!=null){
    System.out.println(sIn);
    }
    sIn = in.readLine();
    }
    }catch(Exception e){
    e.printStackTrace();
    }

    }catch(Exception e){
    e.printStackTrace();
    }
    }
     
    , Feb 26, 2005
    #1
    1. Advertising

  2. Rhino Guest

    <> wrote in message
    news:...
    > I am wrting a client side program to simulate the web browser form
    > upload function. The goal is to send multipart content to server. I
    > found some code online. The basic idea is to send http header
    > information, then send file content as stream, and then read server
    > response. I found the the files are not even send to the server until
    > we read server response. This make my work hard, because I need to set
    > up a progressbar to monitor the uploading. I do not know where to put
    > it.
    >
    > Here is my code...
    >
    >
    >
    > URL u = new URL(urlString);
    > URLConnection c = u.openConnection();
    > c.setDoOutput(true);
    > c.setDoInput(true);
    > c.setUseCaches(false);
    > c.setRequestProperty(
    > "Connection",
    > "Keep-Alive");
    > c.setRequestProperty(
    > "HTTP_REFERER",
    > codebase);
    > c.setRequestProperty(
    > "Content-Type",
    > "multipart/form-data; boundary=" + boundary);
    >
    > DataOutputStream dstream = new
    > DataOutputStream(c.getOutputStream());
    > dstream.writeBytes("--" + boundary + "\r\n");
    >
    > ...post file and parameters
    > dstream.flush();
    > dstream.close();
    >
    > //******if we do not read the server response as bellow, the files are
    > not upload to server.
    > try{
    > DataInputStream in = new DataInputStream(new
    > BufferedInputStream(c.getInputStream()));
    > String sIn = in.readLine();
    > while(sIn!=null){
    > if(sIn!=null){
    > System.out.println(sIn);
    > }
    > sIn = in.readLine();
    > }
    > }catch(Exception e){
    > e.printStackTrace();
    > }
    >
    > }catch(Exception e){
    > e.printStackTrace();
    > }
    > }
    >

    Have you had a look at the Common FileUpload package available at
    http://jakarta.apache.org/commons/fileupload/? I think you'll find that it
    simplifies the effort of doing file uploads in servlets (and applications)
    quite a bit. I've had pretty good success with it so I can recommend it.

    Rhino
     
    Rhino, Feb 27, 2005
    #2
    1. Advertising

  3. Guest

    I am working with the client side. Not the server side. I am working on
    how to send the http post, not how to parse it.


    Rhino wrote:
    > <> wrote in message
    > news:...
    > > I am wrting a client side program to simulate the web browser form
    > > upload function. The goal is to send multipart content to server. I
    > > found some code online. The basic idea is to send http header
    > > information, then send file content as stream, and then read server
    > > response. I found the the files are not even send to the server

    until
    > > we read server response. This make my work hard, because I need to

    set
    > > up a progressbar to monitor the uploading. I do not know where to

    put
    > > it.
    > >
    > > Here is my code...
    > >
    > >
    > >
    > > URL u = new URL(urlString);
    > > URLConnection c = u.openConnection();
    > > c.setDoOutput(true);
    > > c.setDoInput(true);
    > > c.setUseCaches(false);
    > > c.setRequestProperty(
    > > "Connection",
    > > "Keep-Alive");
    > > c.setRequestProperty(
    > > "HTTP_REFERER",
    > > codebase);
    > > c.setRequestProperty(
    > > "Content-Type",
    > > "multipart/form-data; boundary=" + boundary);
    > >
    > > DataOutputStream dstream = new
    > > DataOutputStream(c.getOutputStream());
    > > dstream.writeBytes("--" + boundary + "\r\n");
    > >
    > > ...post file and parameters
    > > dstream.flush();
    > > dstream.close();
    > >
    > > //******if we do not read the server response as bellow, the files

    are
    > > not upload to server.
    > > try{
    > > DataInputStream in = new DataInputStream(new
    > > BufferedInputStream(c.getInputStream()));
    > > String sIn = in.readLine();
    > > while(sIn!=null){
    > > if(sIn!=null){
    > > System.out.println(sIn);
    > > }
    > > sIn = in.readLine();
    > > }
    > > }catch(Exception e){
    > > e.printStackTrace();
    > > }
    > >
    > > }catch(Exception e){
    > > e.printStackTrace();
    > > }
    > > }
    > >

    > Have you had a look at the Common FileUpload package available at
    > http://jakarta.apache.org/commons/fileupload/? I think you'll find

    that it
    > simplifies the effort of doing file uploads in servlets (and

    applications)
    > quite a bit. I've had pretty good success with it so I can recommend

    it.
    >
    > Rhino
     
    , Feb 27, 2005
    #3
  4. Rhino Guest

    <> wrote in message
    news:...
    > I am working with the client side. Not the server side. I am working on
    > how to send the http post, not how to parse it.
    >

    Oops, sorry, I just saw "file upload" and missed the client-side bit. My
    mistake!

    Rhino
     
    Rhino, Feb 27, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jochen Daum
    Replies:
    1
    Views:
    2,748
  2. Matt
    Replies:
    8
    Views:
    144,705
    zeeshanakhter2009
    May 26, 2012
  3. Replies:
    3
    Views:
    8,809
    Ross Bamford
    Nov 5, 2005
  4. Replies:
    2
    Views:
    3,724
  5. Replies:
    1
    Views:
    469
Loading...

Share This Page