POSTing a file to a web page and reading response

Discussion in 'Java' started by someone12345, Aug 31, 2007.

  1. someone12345

    someone12345 Guest

    Hi.

    I'm trying to read raw data from disk, POSTING it to a site that
    converts the file to another format, and then read and save the
    response.

    I have only found on the web information on how to post a string, not
    binary data.
    My code only writes the html page to file, not the response file.


    This is my code so far:
    ----------------------
    URL site = new URL("http://xxx.xxx.xxx.xxx");


    URLConnection conn = site.openConnection();
    conn.setDoOutput(true);
    conn.setRequestProperty
    ("Content-Type", "multipart/form-data");
    DataOutputStream dos;

    OutputStream os = conn.getOutputStream();
    dos = new DataOutputStream(os);
    FileInputStream fis = new FileInputStream("c:\\temp\
    \test.xxx");


    int data;
    while ((data = fis.read()) != -1) {
    dos.write(data);

    }
    dos.flush();
    DataInputStream in = new
    DataInputStream(conn.getInputStream());
    FileOutputStream fo = new FileOutputStream("c:\\temp\
    \test.yyy");
    while ((data = in.read()) != -1) {
    fo.write(data);
    }
    fo.flush();
    fo.close();
    dos.close();
    -----------------------
    someone12345, Aug 31, 2007
    #1
    1. Advertising

  2. someone12345

    Lew Guest

    someone12345 wrote:
    > Hi.
    >
    > I'm trying to read raw data from disk, POSTING it to a site that
    > converts the file to another format, and then read and save the
    > response.
    >
    > I have only found on the web information on how to post a string, not
    > binary data.


    <http://commons.apache.org/fileupload/>

    The key is to set the correct content type in the <form> tag,
    "multipart/form-data".

    --
    Lew
    Lew, Aug 31, 2007
    #2
    1. Advertising

  3. someone12345

    someone12345 Guest

    On Aug 31, 3:40 pm, Lew <> wrote:
    > someone12345 wrote:
    > > Hi.

    >
    > > I'm trying to read raw data from disk, POSTING it to a site that
    > > converts the file to another format, and then read and save the
    > > response.

    >
    > > I have only found on the web information on how to post a string, not
    > > binary data.

    >
    > <http://commons.apache.org/fileupload/>
    >
    > The key is to set the correct content type in the <form> tag,
    > "multipart/form-data".
    >
    > --
    > Lew


    This project seems to be at the other end of what I want. I want to
    create the client, not the server. I want to submit the file to the
    server and read back the server's response.

    This is my code so far...


    /**
    * Created by IntelliJ IDEA.
    * User: hallgrimur
    * Date: 30.8.2007
    * Time: 18:34:12
    * To change this template use File | Settings | File Templates.
    */

    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;

    public class check {
    /* URL doc2pdf;*/
    // URLConnection conn;

    public static void main(String args[]) {
    if (args.length != 0) {
    System.out.println(
    "Proper Usage: java FileDownloader RemoteFileURL
    LocalFileName");
    System.exit(0);
    }

    try {
    URL site = new URL("http://xxx");
    FileInputStream fis = new FileInputStream("c:\\temp\
    \test.xxx");
    );

    HttpURLConnection conn = (HttpURLConnection)
    site.openConnection();
    conn.setRequestMethod("POST");
    conn.setInstanceFollowRedirects(true);
    conn.setDoOutput(true);
    conn.setRequestProperty
    ("Content-Type", "multipart/form-data");
    conn.setRequestProperty("Content-Length","24470"); //
    hard coded for testing
    conn.setRequestProperty("Cache-Control"," no-cache");
    conn.setRequestProperty("Content-Disposition","form-data;
    name=\"inputDocument\"; filename=\"test.xxx\""); //name of the submit
    form action is inputdocument


    DataOutputStream dos;

    OutputStream os = conn.getOutputStream();
    dos = new DataOutputStream(os);



    int data;
    while ((data = fis.read()) != -1) {
    dos.write(data);

    }
    dos.flush();
    os.flush();
    dos.close();
    os.close();
    DataInputStream in = new
    DataInputStream(conn.getInputStream());
    FileOutputStream fo = new FileOutputStream("c:\\temp\
    \test.yyy");
    while ((data = in.read()) != -1) {
    fo.write(data);
    }
    fo.flush();
    // fo.close();
    // dos.close();

    }
    catch (Exception e) {
    e.printStackTrace();
    }


    }
    }
    someone12345, Aug 31, 2007
    #3
  4. someone12345

    Roedy Green Guest

    Roedy Green, Sep 1, 2007
    #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. alanb
    Replies:
    2
    Views:
    514
    alanb
    Apr 23, 2004
  2. Stanley Beamish

    Posting to web app and reading result

    Stanley Beamish, Aug 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    361
    Mark Rae
    Aug 21, 2005
  3. André
    Replies:
    3
    Views:
    1,884
    André
    Dec 1, 2006
  4. AmitKu
    Replies:
    7
    Views:
    475
    John Timney \(MVP\)
    Jan 8, 2007
  5. Stanley Beamish

    Posting to web app and reading result

    Stanley Beamish, Aug 22, 2005, in forum: ASP General
    Replies:
    0
    Views:
    99
    Stanley Beamish
    Aug 22, 2005
Loading...

Share This Page