You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-user@xml.apache.org by Stephan Wiesner <wi...@werum.de> on 2001/07/30 17:37:50 UTC

Binary encoded String to original format, displayed in Browser

I have a PDF on the server. I read it into a Base64 encoded String and send
it with SOAP to the client.
The client determines the MimeType and writes it to a file.
no problem.
I, however, I want to give the file to a client, via a servlet, it get
changed and will not display.
The problem is, that I have a encoded String and a PrintWriter from the
Servlet. Seems, this PrintWriter changes the byte[] in some way while
sending it.
The resulting PDF has the original size and displays fine, except that it is
empty.

Here the code:


Base64 bas = new Base64();
byte[] b = bas.decode(text);

// this works
  FileOutputStream fo = new FileOutputStream("test.pdf");
  fo.write(b);
  fo.close();

// this doesn't
String aus = new String(b);
out.print( aus ); // PrintWriter out
                        // prinltn() doesn't work either.


Thanks (Re: Binary encoded String to original format, displayed in Browser)

Posted by Stephan Wiesner <wi...@werum.de>.
Yeah, knew there was such a little trick.
Thanks a lot, works fine now.


Stephan

----- Original Message -----
From: "William Brogden" <wb...@bga.com>
To: <so...@xml.apache.org>
Sent: Monday, July 30, 2001 5:52 PM
Subject: Re: Binary encoded String to original format, displayed in Browser


>
>
> Stephan Wiesner wrote:
> >
> > I have a PDF on the server. I read it into a Base64 encoded String and
send
> > it with SOAP to the client.
> > The client determines the MimeType and writes it to a file.
> > no problem.
> > I, however, I want to give the file to a client, via a servlet, it get
> > changed and will not display.
> > The problem is, that I have a encoded String and a PrintWriter from the
> > Servlet. Seems, this PrintWriter changes the byte[] in some way while
> > sending it.
> > The resulting PDF has the original size and displays fine, except that
it is
> > empty.
> >
>
> You can't output binary data with a PrintWriter - it tries to do
> UNICODE translations. You have to use a ServletOutputStream from
> the response getOutputStream method.
>
> Be sure to set the Content-type and Content-length
>
> --
> WBB - wbrogden@lanw.com
> Java Cert mock exams http://www.lanw.com/java/javacert/
> Author of Java Developer's Guide to Servlets and JSP
> ISBN 0-7821-2809-2


Thanks (Re: Binary encoded String to original format, displayed in Browser)

Posted by Stephan Wiesner <wi...@werum.de>.
Yeah, knew there was such a little trick.
Thanks a lot, works fine now.


Stephan

----- Original Message -----
From: "William Brogden" <wb...@bga.com>
To: <so...@xml.apache.org>
Sent: Monday, July 30, 2001 5:52 PM
Subject: Re: Binary encoded String to original format, displayed in Browser


>
>
> Stephan Wiesner wrote:
> >
> > I have a PDF on the server. I read it into a Base64 encoded String and
send
> > it with SOAP to the client.
> > The client determines the MimeType and writes it to a file.
> > no problem.
> > I, however, I want to give the file to a client, via a servlet, it get
> > changed and will not display.
> > The problem is, that I have a encoded String and a PrintWriter from the
> > Servlet. Seems, this PrintWriter changes the byte[] in some way while
> > sending it.
> > The resulting PDF has the original size and displays fine, except that
it is
> > empty.
> >
>
> You can't output binary data with a PrintWriter - it tries to do
> UNICODE translations. You have to use a ServletOutputStream from
> the response getOutputStream method.
>
> Be sure to set the Content-type and Content-length
>
> --
> WBB - wbrogden@lanw.com
> Java Cert mock exams http://www.lanw.com/java/javacert/
> Author of Java Developer's Guide to Servlets and JSP
> ISBN 0-7821-2809-2


Re: Binary encoded String to original format, displayed in Browser

Posted by William Brogden <wb...@bga.com>.

Stephan Wiesner wrote:
> 
> I have a PDF on the server. I read it into a Base64 encoded String and send
> it with SOAP to the client.
> The client determines the MimeType and writes it to a file.
> no problem.
> I, however, I want to give the file to a client, via a servlet, it get
> changed and will not display.
> The problem is, that I have a encoded String and a PrintWriter from the
> Servlet. Seems, this PrintWriter changes the byte[] in some way while
> sending it.
> The resulting PDF has the original size and displays fine, except that it is
> empty.
> 

You can't output binary data with a PrintWriter - it tries to do 
UNICODE translations. You have to use a ServletOutputStream from
the response getOutputStream method.

Be sure to set the Content-type and Content-length 

-- 
WBB - wbrogden@lanw.com
Java Cert mock exams http://www.lanw.com/java/javacert/
Author of Java Developer's Guide to Servlets and JSP 
ISBN 0-7821-2809-2

Re: Binary encoded String to original format, displayed in Browser

Posted by William Brogden <wb...@bga.com>.

Stephan Wiesner wrote:
> 
> I have a PDF on the server. I read it into a Base64 encoded String and send
> it with SOAP to the client.
> The client determines the MimeType and writes it to a file.
> no problem.
> I, however, I want to give the file to a client, via a servlet, it get
> changed and will not display.
> The problem is, that I have a encoded String and a PrintWriter from the
> Servlet. Seems, this PrintWriter changes the byte[] in some way while
> sending it.
> The resulting PDF has the original size and displays fine, except that it is
> empty.
> 

You can't output binary data with a PrintWriter - it tries to do 
UNICODE translations. You have to use a ServletOutputStream from
the response getOutputStream method.

Be sure to set the Content-type and Content-length 

-- 
WBB - wbrogden@lanw.com
Java Cert mock exams http://www.lanw.com/java/javacert/
Author of Java Developer's Guide to Servlets and JSP 
ISBN 0-7821-2809-2

Re: Binary encoded String to original format, displayed in Browser

Posted by Tom Myers <to...@dreamscape.com>.
At 05:37 PM 7/30/2001 +0200, Stephan Wiesner wrote:
>I have a PDF on the server. I read it into a Base64 encoded String and send
>it with SOAP to the client....
>Base64 bas = new Base64();
>byte[] b = bas.decode(text);
>
>// this works
>   FileOutputStream fo = new FileOutputStream("test.pdf");
>   fo.write(b);
>   fo.close();
>
>// this doesn't
>String aus = new String(b);
>out.print( aus ); // PrintWriter out
>                         // prinltn() doesn't work either.

Why can't you use a ServletOutputStream, via getOutputStream() from your
ServletResponse object? The javadoc says it "Returns a ServletOutputStream 
suitable for writing binary data in the response." whereas the javadoc for
PrintWriter says "Print formatted representations of objects to a text-output 
stream. This class implements all of the print methods found in PrintStream. 
It does not contain methods for writing raw bytes, for which a program 
should use unencoded byte streams."

Tom Myers



Re: Binary encoded String to original format, displayed in Browser

Posted by Tom Myers <to...@dreamscape.com>.
At 05:37 PM 7/30/2001 +0200, Stephan Wiesner wrote:
>I have a PDF on the server. I read it into a Base64 encoded String and send
>it with SOAP to the client....
>Base64 bas = new Base64();
>byte[] b = bas.decode(text);
>
>// this works
>   FileOutputStream fo = new FileOutputStream("test.pdf");
>   fo.write(b);
>   fo.close();
>
>// this doesn't
>String aus = new String(b);
>out.print( aus ); // PrintWriter out
>                         // prinltn() doesn't work either.

Why can't you use a ServletOutputStream, via getOutputStream() from your
ServletResponse object? The javadoc says it "Returns a ServletOutputStream 
suitable for writing binary data in the response." whereas the javadoc for
PrintWriter says "Print formatted representations of objects to a text-output 
stream. This class implements all of the print methods found in PrintStream. 
It does not contain methods for writing raw bytes, for which a program 
should use unencoded byte streams."

Tom Myers