tags:

views:

68

answers:

2

I am working on an applet that interfaces with a signature pad. The signature pad API has a function that returns a BufferedImage (assume its called API_CALL_TO_RETURN_BUFFERED_IMAGE()). I can encode to jpeg and write this image to file just fine (Using FileOutputStream). However, instead of writing to the local disk, I need to upload the jpeg-encoded image to the server. I can POST data to the server just fine, and I can encode the image just fine; but I am struggling in having the two tasks meet in the middle.

The following is a condensed version of the code (try-catch, function, classes omitted):

ByteArrayOutputStream baos = new ByteArrayOutputStream();
JPEGImageEncode jie = JPEGCodec.createJPEGEncoder(baos);
jpeg.encode(API_CALL_TO_RETURN_BUFFERED_IMAGE());         // assume magic

// baos now contains jpeg data

URLConnection urlc = new URL(some_url).openConnection();
// set up urlc request headers and such
DataOutputStream dos = new DataOutputStream(urlc.getOutputStream());
dos.writeBytes(???);   // ??? should be image=[the data in baos above]

// close stuff

Originally, I thought:

String post_data = URLEncoder.encode("image=" + new String(baos.toByteArray()), some_charset);
dos.writeBytes(post_data);

But that clearly distorts the image.

This is what the proper (written locally) image looks like

I can only post one hyperlink, but the distorted image is here: imgur.com/mbmJL.jpg

How do I write a ByteArrayOutputStream to a DataOutputStream?

EDIT/UPDATE:

My solution was to do a mutlipart POST. The reason for setting the Content-Type header as multipart/form-data was, as this link, http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4, says:

multipart/form-data should be used for files, non-ascii values, or binary data

As far as writing a ByteArrayOutputStream to a DataOutputStream, it looks like:

dos.writeBytes(baos.toByteArray());

I am sure that is trivial to full-time Java programmers, but not I!

I did not use the library that was suggested because it offered way more then I needed.

+3  A: 

I would simplify some of the above by using HttpClient to manage the posting. Here's the Post tutorial and (if you need it) the Multipart Post tutorial. It's not clear which you need, but using HttpClient will solve a lot of the issues in creating the structure and content of an HTTP request.

Brian Agnew
HttpClient is very convenient and easy to use, thats true)
Roman
You know, originally, I thought the problem was my java (I dont write a lot of it), but now I am thinking the problem is decoding the image data on the server.
A: 

When you write one stream to another, there's a lot of overhead that might come into it; buffers, try/catch blocks, .close(), more try/catch blocks.

Apache Commons IO Library automates most of it for you.

If you're not in a hurry, figure out how to do it, but in the long run, just use Commons IO. For an example of how a buffered copy works, the first example from the Commons IO page shows the first half; instead of System.out.println, you'd want to write the line to another stream.

 InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
 try {
   InputStreamReader inR = new InputStreamReader( in );
   BufferedReader buf = new BufferedReader( inR );
   String line;
   while ( ( line = buf.readLine() ) != null ) {
     System.out.println( line );
   }
 } finally {
   in.close();
 }

If your file isn't string data, you'd want to read byte[] instead of String.

Dean J