Hi,

> -----Original Message-----
> From: Paritosh Patel [mailto:xygnu...@gmail.com]
> Sent: Friday, July 15, 2011 5:24 PM
> To: users@tomcat.apache.org
> Subject: How to send binary data in a form field via Java
> 
> I apologize in advance if this is not the correct mail list for such a
> question, but this is the closest one I could find.
> 
> (I am using Tomcat 6.0.26 but my question is generic in nature)
> 
> I have a Java client that talks to a servlet using several text fields.
> I now wanted to add a field that includes binary data (in this case a
> protocol buffer byte array). The data gets to the servlet, but the
> bytes are changed. Specifically, it appears that the encoding/decoding
> of bytes > 127 are not the same as the original bytes.

That's expected, because ASCII is only defined to byte 127. From byte 128,
the related characters differ by the used Encoding (UTF-8, ISO-8859-1,...)


> 
> The client specified content-type to be "application/x-www-form-
> urlencoded" right now, but I have tried several others. Then, for
> encoding, I have tried encoding the bytes using URLEncoder
> 
>       ByteArrayOutputStream osBytes = new ByteArrayOutputStream();
>       byte[] val = "..."  // My binary data
>       String valStr = new String((byte[]) val);
>       osBytes.write(URLEncoder.encode(valStr, "UTF-8").getBytes());

That is unreliable, because the bytes are converted to chars using the
platform's default charset. If that differs from the server's one, the bytes
will be decoded to other characters.


Have you tried to just Hex-encode the bytes when sending it to the server,
and decoding it on the server side? For example, if your byte[] array is {1,
99, 0, 255}, you could encode it to the hex string "016300FF", and then on
the server side, decode it to bytes.

However, a more efficient form would be to encode the bytes via Base64,
which will mean a increase in size of only 33% instead 100% (when
hex-encoding it).


Regards,
Konstantin Preißer


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to