开发者

How to POST form-like image data from java

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();
  开发者_C百科       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!


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.



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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜