tags:

views:

55

answers:

2

Hey, I've tried researching how to POST data from java, and nothing seems to do what I want to do. Basically, theres a form for uploading an image to a server, and what I want to do is post an image to the same server - but from java. It also needs to have the right parameter name (whatever the form input's name is). I would also want to return the response from this method.

It baffles me as to why this is so difficult to find, since this seems like something so basic.

EDIT ---- Added code

Based on some of the stuff BalusC showed me, I created the following method. It still doesn't work, but its the most successful thing I've gotten yet (seems to post something to the other server, and returns some kind of response - I'm not sure I got the response correctly though):

EDIT2 ---- added to code based on BalusC's feedback

EDIT3 ---- posting code that pretty much works, but seems to have an issue:

 ....

 FileItemFactory factory = new DiskFileItemFactory();

 // Create a new file upload handler
 ServletFileUpload upload = new ServletFileUpload(factory);

 // Parse the request
 List<FileItem> items = upload.parseRequest(req);

 // Process the uploaded items
 for(FileItem item : items) {
     if( ! item.isFormField()) {
         String fieldName = item.getFieldName();
         String fileName = item.getName();
         String itemContentType = item.getContentType();
         boolean isInMemory = item.isInMemory();
         long sizeInBytes = item.getSize();

         // POST the file to the cdn uploader
         postDataRequestToUrl("<the host im uploading too>", "uploadedfile", fileName, item.get());

     } else {
         throw new RuntimeException("Not expecting any form fields");
     }
 }

....

// Post a request to specified URL. Get response as a string.
public static void postDataRequestToUrl(String url, String paramName, String fileName, byte[] requestFileData) throws IOException {
 URLConnection connection=null;

 try{
     String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
     String charset = "utf-8";

     connection = new URL(url).openConnection();
     connection.setDoOutput(true);
     connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
     PrintWriter writer = null;
     OutputStream output = null;
     try {
         output = connection.getOutputStream();
         writer = new PrintWriter(new OutputStreamWriter(output, charset), true); // true = autoFlush, important!

         // Send binary file.
         writer.println("--" + boundary);
         writer.println("Content-Disposition: form-data; name=\""+paramName+"\"; filename=\"" + fileName + "\"");
         writer.println("Content-Type: " + URLConnection.guessContentTypeFromName(fileName));
         writer.println("Content-Transfer-Encoding: binary");
         writer.println();

         output.write(requestFileData, 0, requestFileData.length);
         output.flush(); // Important! Output cannot be closed. Close of writer will close output as well.

         writer.println(); // Important! Indicates end of binary boundary.

         // End of multipart/form-data.
         writer.println("--" + boundary + "--");
     } finally {
         if (writer != null) writer.close();
         if (output != null) output.close();
     }

     //*  screw the response

     int status = ((HttpURLConnection) connection).getResponseCode();
     logger.info("Status: "+status);
     for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
         logger.info(header.getKey() + "=" + header.getValue());
     }

 } catch(Throwable e) {
     logger.info("Problem",e);
 } 

}

I can see this code uploading the file, but only after I shutdown the tomcat. This leads me to believe that I'm leaving some sort of connection open.

This worked!

A: 

What have you tried? If you google for Http Post Java, dozens of pages appear - what's wrong with them? This one, http://www.devx.com/Java/Article/17679/1954 for example, appears pretty decent.

Nicolas78
A combination of URL, URLConnection, DataOutputStream, and DataInputStream always gives me back a FileNotFoundException. And using PostMethod adding a parameter with setParameter, the server I'm uploading to says I didn't pass it a file (presumably I didn't pass it as the right parameter name or some kind of information is missing).
B T
you seem to be on the right trak, sounds like you should definitively post some code! :)
Nicolas78
+3  A: 

The core API you'd like to use is java.net.URLConnection. This is however pretty low level and verbose. You'd like to learn about the HTTP specifics in detail and take them into account (headers, etcetera). You can find here a related question with lot of examples.

A more convenient HTTP client API is the Apache Commons HttpComponents Client. You can find an example here.


Update: as per your update: you should read the response as a character stream, not as a binary stream and attempt to cast a byte to a char. This ain't going to work. Head to the Gathering HTTP response information part in the linked question with examples. Here's how it should look like:

BufferedReader reader = null;
StringBuilder builder = new StringBuilder();

try {
    reader = new BufferedReader(new InputStreamReader(response, charset));
    for (String line; (line = reader.readLine()) != null;) {
        builder.append(line);
    }
} finally {
    if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
}

return builder.toString();

Update 2: as per your second update. Seeing the way how you continue to attampt reading/writing streams, I think it's high time to learn the basic Java IO :) Well, this part is also answered in the linked question. You would like to use Apache Commons FileUpload to parse a multipart/form-data request in a servlet. How to use it is also described/linked in the linked question. Look at the bottom of the Uploading files chapter. By the way, the content length header would return zero since you are not explicitly setting it (and also cannot do without buffering the entire request in memory).


Update 3:

I can see this code uploading the file, but only after I shutdown the tomcat. This leads me to believe that I'm leaving some sort of connection open.

You need to close the OutputStream with which you wrote the file to the disk. Once again, read the above linked basic Java IO tutorial.


BalusC
It would be much more helpful if there were any examples of posting something image data.
B T
The related `URLConnection` question contains a full example (head to *uploading files* part somewhere at bottom). The HttpClient example is fullworthy as well, you may only want to change the content/mime type.
BalusC
Thanks, I tried that out and edited my question.
B T
@B T: See the update of my answer.
BalusC
@BalusC Thanks for the FileUpload suggestion - first thing I've seen that has an API that makes sense. It definitely gets the data through - it uploads the file and I can see it on the other end. The problem is its seems to be taking a while to register - it kinda seems like my method isn't closing a connection or something. I'll update my question with the code I'm using now
B T
I am closing the OutputStream:if (output != null) output.close();
B T
Are you closing the outermost one? When you wrap an `OutputStream` in another `OutputStream` like `new BufferedOutputStream(output)`, then you should declare and close the `BufferedOutputStream` one.
BalusC
I'm closing both actually. You can see what i'm closing in the finally block.
B T
I didn't mean in your client, but in the server. After you've retrieved the uploaded file in some servlet in Tomcat.
BalusC
So it looks like the image was actually uploaded, but took a minute or so to propagate through the cdn. I just have to be patient. Thanks so much for your help!
B T
You're welcome.
BalusC