users@jersey.java.net

[Jersey] Re: how to preserve HTML formatting for a byte[] by Jersey client

From: Pavel Bucek <pavel.bucek_at_oracle.com>
Date: Tue, 19 Jul 2011 22:17:05 +0200

this is not related to formatting, my bet is on encoding issues. Are you
ensuring proper encoding somewhere?

I have created testcase

server side:

     @PUT
     @Path("echo")
     @Produces("text/plain; charset=utf-8")
     public byte[] echo(byte[] s) {
         System.out.println("Service received: " + new String(s,
Charset.forName("UTF-8")));
         return s;
     }

client side:

     @Test
     public void testEcho() {
         WebResource webResource = resource();
         byte[] bytes =
webResource.path("helloworld/echo").put(byte[].class, new
String("Pr(ílis( z(lut(ouc(ký ku*n( úpe(l d(ábelské ódy".getBytes(),
Charset.forName("UTF-8")));

         System.out.println("Client received: " + new String(bytes,
Charset.forName("UTF-8")));
     }

and output is:

Service received: Pr(ílis( z(lut(ouc(ký ku*n( úpe(l d(ábelské ódy
Client received: Pr(ílis( z(lut(ouc(ký ku*n( úpe(l d(ábelské ódy

which is correct.

So my recommendation: investigate about encoding used on both sides,
tools like ethereal/wireshark should help you.

Regards,
Pavel

On 7/19/11 6:09 PM, Jitesh Sinha -X (jisinha - Siliconweb Inc. at Cisco)
wrote:
>
> Can someone please provide help about this?
>
> *From:*Jitesh Sinha -X (jisinha - Siliconweb Inc. at Cisco)
> *Sent:* Monday, July 18, 2011 3:21 PM
> *To:* users_at_jersey.java.net
> *Subject:* [Jersey] how to preserve HTML formatting for a byte[] by
> Jersey client
>
> Hi All,
>
> I need to send a byte[] to a web service.I am writing both client and
> web service side code. Somehow when I get byte[] on server side I see
> garbled characters in place of bullet points. These bullet points are
> absolutely fine at client side.
>
> I get byte[] from an HTML form that has an upload field and Users can
> upload a file in UTF-8 encoding.
>
> Here is the code at client side --
>
> byte[] uploadFile = testCommand.getUploadFile() ;
>
> FormDataMultiPart formData = new FormDataMultiPart() ;
>
> formData.field("uploadFile", uploadFile,
> MediaType.MULTIPART_FORM_DATA_TYPE);
>
> Client client = Client.create();
>
> WebResource service =
> client.resource(AltCsoUtil.loadProperties().getProperty("requestUrl"));
>
> ClientResponse response =
> service.type(MediaType.MULTIPART_FORM_DATA).header("appKey",appkey).post(ClientResponse.class,formData);
>
> Code at server side is pretty simple -- I just get this field from
> Spring framework's Command class --
>
> Server side code -
>
> byte[] uploadFile = clientCommand.getUploadFile() ;
>
> FileOutputStream fos = new FileOutputStream("input.html") ;
>
> fos.write(clientCommand.getUploadFile()) ;
>
> fos.flush() ;
>
> fos.close() ;
>
> I have put same FileOutputStream statements on client side also -- it
> all comes fine. On server side ,as I said bullet points become garbled.
>
> Thanks
>
> -Jitesh
>