Tuesday, April 25, 2006

Posting a document using HttpURLConnection?

Java Programming: Posting a document using HttpURLConnection?: "Posting a document using HttpURLConnection"


----------------------------------------
-----------------
Here is my entire working 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());
}
}

Wednesday, February 15, 2006

Seagate ST1.3 1-Inch Drive: Smaller Yet Holds More Data - Gizmodo

These are nano-mechanics on steroids...boy wat is this world coming to. 1 inch drive is almost as small as a regular sdcard..kewl stuff
Seagate ST1.3 1-Inch Drive: Smaller Yet Holds More Data - Gizmodo