Platform: SuSE linux 6.2 (w/kernel version 2.2.14), Blackdown JDK 1.1.7v3

I've been trying to write a small test program that writes a
multipart/form-data body to an apache Tomcat servlet, using HTTP
POST. 

What happens is that the servlet sees the headers, but not the body of 
the message I'm trying to write.  

What I'm wondering is if I've encountered a well known problem with
1.1.7v3?  Or if the problem is something in my code?

I've tried searching the net for this problem, but I haven't found
anything useful yet.

Basically I'm trying to do the same thing that happens when sending
the results of a form from a web browser.

To do some more testing I've written a small cgi-script, which just
writes back the body it receives:

        #!/usr/bin/perl
        print "Content-type: text/plain", "\n\n";
        while (<>) {
            print;
        }
        exit (0);

When I try POSTing a form from a browser, I see the
multipart/form-data it writes.  When I try using my Java application I 
just get the result codes.

Attached is the code I'm using.  Upload.java contains the program, and 
RepositoryCmd.java tries to encapsulate the construction and POST of
the multipart/form-data message.


- Steinar

import java.io.*;

class Upload {
    public static void main(String[] args)
    {
        if (args.length < 3) {
            System.out.println("usage: Upload server repository file"
                               + " [servlet]");
            return;
        }

        String server     = args[0];
        String repository = args[1];
        String filename   = args[2];
        RepositoryCmd rc = null;
        if (args.length > 3) {
            String servlet   = args[3];
            rc = new RepositoryCmd(server,
                                   repository,
                                   "put",
                                   filename,
                                   servlet);
        } else {
            rc = new RepositoryCmd(server,
                                   repository,
                                   "put",
                                   filename);
        }
        System.out.print("Starting upload...");
        BufferedReader in = rc.send();
        try {
            String inputLine;
            while ((inputLine = in.readLine()) != null) 
                System.out.println(inputLine);
        } catch (IOException e) {
            System.out.println(e);
        }
        System.out.println("finished!");
    }
}
import java.io.*;
import java.net.*;

class RepositoryCmd {
    protected String servlet_;
    protected String server_;
    protected String repository_;
    protected String command_;
    protected String filename_;

    public RepositoryCmd(String server,
                         String repository,
                         String command,
                         String filename)
    {
        server_ = server;
        repository_ = repository;
        command_ = command;
        filename_ = filename;
        servlet_ = "/metis/repository";
    }

    public RepositoryCmd(String server,
                         String repository,
                         String command,
                         String filename,
                         String servlet)
    {
        server_ = server;
        repository_ = repository;
        command_ = command;
        filename_ = filename;
        servlet_ = servlet;
    }

    public BufferedReader send()
    {
        BufferedReader retval = null;
        try {
            String boundary = "12345xyzzy";
            URL repository = new URL("http://" + server_ + servlet_);
            HttpURLConnection tc =
                (HttpURLConnection)repository.openConnection();
            tc.setDoOutput(true);
            tc.setDoInput(true);
            tc.setAllowUserInteraction(false);
            tc.setRequestMethod("POST");
            tc.setRequestProperty ("Content-Type",
                                   "multipart/form-data; "
                                   + "boundary=" + boundary);
            PrintWriter out = new PrintWriter(tc.getOutputStream());
            //PrintWriter out = new PrintWriter(System.out);
            retval = new BufferedReader(new InputStreamReader(tc.getInputStream()));
            FileInputStream fin = new FileInputStream(filename_);
            String inputLine;
            BufferedReader in =
                new BufferedReader(new InputStreamReader(fin));
            printPartHeader(out,boundary,"filename","filename_");
            while ((inputLine = in.readLine()) != null) {
                out.println(inputLine);
            }
            printPartHeader(out,boundary,"request","");
            out.println(command_);
            printPartHeader(out,boundary,"path","");
            out.println(repository_);
            printPartHeader(out,boundary,"overwrite","");
            out.println("true");
            printPartHeader(out,boundary,"create","");
            out.println("true");
            printEndBoundary(out,boundary);
            out.close();
            in.close();
            System.out.println("Response: "
                               + tc.getResponseCode() + ": "
                               + tc.getResponseMessage());
        } catch (MalformedURLException e) {
            System.out.println(e);
        } catch (IOException e) {
            System.out.println(e);
        }
        return retval;
    }

    protected void printPartHeader(PrintWriter out,
                                   String boundary,
                                   String field,
                                   String filename)
    {
        printBoundary(out,boundary);
        out.print("Content-Disposition: form-data" +
                  "; name=\"" + field + "\"");
        if (filename.length() > 0) {
            out.print("; filename=\"/" + filename + "\"\r\n");
            out.print("Content-Type: text/xml");
        }
        out.print("\r\n\r\n");
    }

    protected void printBoundary(PrintWriter out, String boundary)
    {
        out.print("--" + boundary + "\r\n");
    }

    protected void printEndBoundary(PrintWriter out, String boundary)
    {
        out.print("--" + boundary + "--\r\n\r\n");
    }
}

Reply via email to