com.ccg.net
Class URLPostMultiPartForm

java.lang.Object
  extended by com.ccg.net.URLPostMultiPartForm

public class URLPostMultiPartForm
extends Object

Class designed to communicate with a HTTP server like a multi-part form.

WARNING: This class has not been fully tested as it was originally created to work with servlets - and then this programmer discovered that servlet engines don't like to deal with parameters posted in this manner (at least not the simple servlet tester).

When dealing with HTTP server's one may find themselves in a position where they need to "POST" arguments to the server in a fashion similar to the way in which a web browser might post data from a multi part form submission (a form that uploads a file).

This class provides a lot of help in this regard. Basically, you use it as follows:

Here is an example program demonstrating how to use this class

import com.ccg.io.*;
import com.ccg.net.*;

import java.io.*;
import java.net.*;

public class Foo {

  static public void main(String args[]) {
    try {
      if (args.length == 0) {
        System.err.println("usage: java Foo URL");
        System.exit(1);
      }

      URLPostMultiPartForm post = new URLPostMultiPartForm();

      System.err.println("Opening connection");
      post.open(new URL(args[0]),true);

      System.err.println("Writing parameter info");
      post.writeStringParam("firstName","Paul");
      post.writeStringParam("lastName","Bl ankenbaker");
      post.writeFileParam("upload",new File("/home/pkb/.signature"));
      post.writeStringParam("age","37");

      System.err.println("Retrieving information");
      Copy.streamToStream(post.getInputStream(),System.out);

      System.err.println("Closing connection");
      post.close();

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

  }
}

 

Since:
1.0
Version:
$Revision: 1.2 $
Author:
$Author: pkb $
See Also:
URLPost

Constructor Summary
URLPostMultiPartForm()
           
 
Method Summary
 IOException close()
          Closes all of the open connections.
 InputStream getInputStream()
           
 URL getURL()
          Get the URL we communicate with
 void open(URL url, boolean doInput)
          Opens a URL connection.
protected  void writeEndParam()
           
 void writeFileParam(String name, File file)
          Write a entire FILE to the server.
protected  PrintWriter writeStartParam(String name)
           
 void writeStringParam(String name, String value)
          Write a single string parameter NAME/VALUE pair to the server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

URLPostMultiPartForm

public URLPostMultiPartForm()
Method Detail

open

public void open(URL url,
                 boolean doInput)
          throws IOException
Opens a URL connection.

This method creates a connection to the specified URL. After the connection has been opened your can write the parameters you need to send to the server and then optionally get the InputStream to read the server's response.

Parameters:
url - The URL of the server to communicate with.
doInput - Set to true if you will be reading data returned from the server, set to false if you are only putting data to the server.
Throws:
IOException - If unable to open a connection.
Since:
1.0
See Also:
writeStringParam(java.lang.String, java.lang.String), writeFileParam(java.lang.String, java.io.File), getInputStream()

close

public IOException close()
Closes all of the open connections.

After you are done communicating, you can use this method to make sure that any open connections are closed.

Returns:
IOException if there is a problem closing the connection.
Since:
1.0
See Also:
open(java.net.URL, boolean)

writeStringParam

public void writeStringParam(String name,
                             String value)
                      throws IOException
Write a single string parameter NAME/VALUE pair to the server.

Use this method after opening a connection to write a single NAME/VALUE pair to the server.

Parameters:
name - Name of the parameter to send.
value - Value of the parameter to send.
Throws:
IOException - If there is a problem sending the NAME/VALUE pair.
Since:
1.0
See Also:
writeFileParam(java.lang.String, java.io.File)

writeFileParam

public void writeFileParam(String name,
                           File file)
                    throws IOException
Write a entire FILE to the server.

Use this method after opening a connection to write the contents of an entire file to the server (with an associated NAME).

Parameters:
name - Name of the parameter that the server will see.
file - The local file to send.
Throws:
IOException - If there is a problem sending the NAME/VALUE pair.
Since:
1.0
See Also:
writeStringParam(java.lang.String, java.lang.String)

getInputStream

public InputStream getInputStream()
                           throws IOException
Throws:
IOException

writeEndParam

protected void writeEndParam()
                      throws IOException
Throws:
IOException

writeStartParam

protected PrintWriter writeStartParam(String name)
                               throws IOException
Throws:
IOException

getURL

public URL getURL()
Get the URL we communicate with

Returns:
Current URL value assigned.
See Also:
open(java.net.URL, boolean)


Copyright 1998-1998-2006 null. All Rights Reserved.