Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

POSTing a file to a web page and reading response

Thread view: 
someone12345 - 31 Aug 2007 13:15 GMT
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();
-----------------------
Lew - 31 Aug 2007 14:40 GMT
> Hi.
>
[quoted text clipped - 4 lines]
> 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".

Signature

Lew

someone12345 - 31 Aug 2007 19:16 GMT
> > Hi.
>
[quoted text clipped - 12 lines]
> --
> 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();
       }

   }
}
Roedy Green - 01 Sep 2007 14:12 GMT
On Fri, 31 Aug 2007 12:15:10 -0000, someone12345
<hallgrimur@gmail.com> wrote, quoted or indirectly quoted someone who
said :

>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.

You will have to "armour" the data in the post with urlencoding.
See http://mindprod.com/products.html#HTTP

See http://mindprod.com/jgloss/armouring.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.