[Zope] Posting a document to Zope via a Java Program
Gilles
gilles.lavaux@esrin.esa.it
Thu, 10 Oct 2002 17:27:24 -0500
Hello,
With you code I get :
000719: 3c 70 3e 0a 20 20 3c 73 74 72 6f 6e 67 3e 45 72 < p > . < s t
r o n g > E r
000735: 72 6f 72 20 54 79 70 65 3a 20 56 61 6c 75 65 45 r o r T y p e :
V a l u e E
000751: 72 72 6f 72 3c 2f 73 74 72 6f 6e 67 3e 3c 62 72 r r o r < / s t r
o n g > < b r
000767: 3e 0a 20 20 3c 73 74 72 6f 6e 67 3e 45 72 72 6f > . < s t r o
n g > E r r o
000783: 72 20 56 61 6c 75 65 3a 20 49 6e 76 61 6c 69 64 r V a l u e :
I n v a l i d
000799: 20 62 6f 75 6e 64 61 72 79 20 69 6e 20 6d 75 6c b o u n d a r y
i n m u l
000815: 74 69 70 61 72 74 20 66 6f 72 6d 3a 20 27 27 3c t i p a r t f o
r m : ' ' <
000831: 2f 73 74 72 6f 6e 67 3e 3c 62 72 3e 20 0a 20 20 / s t r o n g > <
b r > .
The boundary you are using look wrong. I was doing this in python and I
think I was getting the boundary from some library, I was not creating it
myself.
Gilles
----- Original Message -----
From: "D Bamud" <nougain@cad.delhi.nic.in>
To: "Zope Mailing List" <zope@zope.org>
Sent: Thursday, October 10, 2002 8:16 AM
Subject: [Zope] Posting a document to Zope via a Java Program
>
>
>
>
> My idea is to submit a document using a Java Client and not the browser. I
> could do this successfully using my *JAVA HTTP Client program* written by
> me. The server I used is Tomcat4.0.3 and a JSP program Upload.jsp (that
uses
> com.orelly.servlet.MultiPart). If it works fine here it should work fine
> with any HTTP server including Zope. But when I try "Zope" server (with
> program http://localhost:8080/Examples/FileLibrary/addFile) it gives me a
> 500 error. I am posting the error message below if you can give some
clue...
> (I used the specs from RFC1867)
>
> null: HTTP/1.1 500 Internal Server Error
> Server: Zope/(Zope 2.5.1 (binary release, python 2.1, win32-x86), python
> 2.1.3, win32) ZServer/1.1b1
> Date: Thu, 10 Oct 2002 12:57:34 GMT
> Bobo-Exception-File: C:\Zope\bin\lib\cgi.py
> Content-Type: text/html
> Bobo-Exception-Type: ValueError
> Bobo-Exception-Value: bobo exception
> Etag:
> Content-Length: 1704
> Bobo-Exception-Line: 603
>
> getRequestMethod : POST
> getResponseCode : 500
> getResponseMessage : Internal Server Error
>
>
> ---------------------------------------------------------
> Here is my entire working Java HTTP Client program
> ---------------------------------------------------------
> import java.io.*;
> import java.net.*;
>
> /**
> * RFC 1867
> * ========
> * Content-type: multipart/form-data, boundary=AaB03x
> *
> * --AaB03x
> * content-disposition: form-data; name="field1"
> *
> * Joe Blow
> * --AaB03x
> * content-disposition: form-data; name="pics"; filename="file1.txt"
> * Content-Type: text/plain
> *
> * ... contents of file1.txt ...
> * --AaB03x--
> */
> public class PostDocument {
> public static void main(String[] args) throws Exception {
> //
> // CONSTANTS
> //
> String url = "http://localhost:8080/Examples/FileLibrary/addFile";
> // <--- Zope/Python
> // String url =
> "http://localhost:8080/rcn/jsp/UploadFile.jsp?action=upload"; // <---
> Tomcat/JSP
> String docPath = "D:\\rcn\\java\\HttpURLConnection\\testdoc.txt";
> String bndry = "AaB03x";
> String paramName = "file";
> String fileName = "testdoc.txt";
>
> //
> // CREATE AN HTTP CONNECTION
> //
> HttpURLConnection httpcon = (HttpURLConnection) ((new
> URL(url).openConnection()));
> httpcon.setDoOutput(true);
> httpcon.setUseCaches(false); // ??? Not Required?
> httpcon.setRequestMethod("POST");
> httpcon.setRequestProperty("Content-type", "multipart/form-data,
> boundary=" +bndry); // this is new line
> httpcon.connect();
>
> //
> // OPEN THE READ AND WRITE STREAMS
> //
> System.out.println("Posting " +docPath +"...");
> File file = new File(docPath);
> FileInputStream is = new FileInputStream(file);
> OutputStream os = httpcon.getOutputStream();
>
> //
> // WRITE THE FIRST/START BOUNDARY
> //
> String disptn = "--" +bndry +"\r\ncontent-disposition: form-data;
> name=\"" +paramName +"\"; filename=\"" +fileName +"\"\r\nContent-Type:
> text/plain\r\n\r\n";
> System.out.print(disptn);
> os.write(disptn.getBytes());
>
> //
> // WRITE THE FILE CONTENT
> //
> byte[] buffer = new byte[4096];
> int bytes_read;
> while((bytes_read = is.read(buffer)) != -1) {
> os.write(buffer, 0, bytes_read);
> System.out.print(new String(buffer, 0, bytes_read));
> }
>
> //
> // WRITE THE CLOSING BOUNDARY
> //
> String boundar = "\r\n--" +bndry +"--";
> System.out.print(boundar);
> os.write(boundar.getBytes()); // another 2 new lines
>
> //
> // FLUSH / CLOSE THE STREAMS
> //
> os.flush();
> os.close();
> is.close();
>
> // DEBUG
> System.out.println("\n....Done!!!...\n\n");
> dump(httpcon);
> }
>
> public static void dump(HttpURLConnection httpcon) throws IOException {
> int n=0; // n=0 has no key, and the HTTP return status in the value
> field
> String headerKey;
> String headerVal;
>
> while (true){
> headerKey = httpcon.getHeaderFieldKey(n);
> headerVal = httpcon.getHeaderField(n);
>
> if (headerKey != null || headerVal != null) {
> System.out.println(headerKey +": " +headerVal);
> }
> else {
> break;
> }
>
> n++;
> }
>
> System.out.println();
> System.out.println("getRequestMethod : "
> +httpcon.getRequestMethod());
> System.out.println("getResponseCode : "
> +httpcon.getResponseCode());
> System.out.println("getResponseMessage : "
> +httpcon.getResponseMessage());
> }
> }
>
>
> _______________________________________________
> Zope maillist - Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> ** No cross posts or HTML encoding! **
> (Related lists -
> http://lists.zope.org/mailman/listinfo/zope-announce
> http://lists.zope.org/mailman/listinfo/zope-dev )
>
>
>