Re: POSTing a file to a web page and reading response



On Aug 31, 3:40 pm, Lew <l...@xxxxxxxxxxxxx> 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();
}


}
}

.



Relevant Pages

  • Problem with postback after Response.Write
    ... My web app writes some binary data to a file at the client site via ... // Set up the response to write the print file to the client ... File Download box and the user saves the file, ...
    (microsoft.public.dotnet.framework.aspnet)
  • CAB binary data over servlet response
    ... I created a servlet which sends CAB binary data on the servlet ... The binary data is contained in a byte array. ... I join the sample of code which sends the byte array over the response ...
    (comp.lang.java.programmer)
  • CAB binary data over servlet response
    ... I created a servlet which sends CAB binary data on the servlet ... The binary data is contained in a byte array. ... I join the sample of code which sends the byte array over the response ...
    (de.comp.lang.java)
  • POSTing a file to a web page and reading response
    ... 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 ... My code only writes the html page to file, not the response file. ... DataOutputStream dos; ...
    (comp.lang.java.programmer)
  • Re: IO Stream HELP URGENT!!!
    ... This will fill a byte array with the content of "stream" and write to the ... always clear the response before writing binary data, ... > Response.BinaryWrite does not accept a stream data type. ...
    (microsoft.public.dotnet.languages.csharp)