You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Keith Campbell <KC...@paperloop.com> on 2005/09/15 00:33:16 UTC

Excel download question

Hi all,

I'm trying to use HttpClient to download an excel file from a web  
application.

Everything works but in my final excel document I get a series of  
meaningless integers printed out to the document.
Am I missing something obvious?


xml = method.getResponseBodyAsStream();

PrintWriter out = response.getWriter();

response.setHeader( "content-disposition","attachment;  
filename=projectList.xls" );
response.setContentType("application/vnd.ms-excel");

int excel;
while((excel=xml.read())!=-1){
     out.print(excel);
}

Thanks in advance,
Keith

Re: Excel download question

Posted by Wade Chandler <hw...@yahoo.com>.
--- Michael Becke <mb...@gmail.com> wrote:

> Hi Keith,
> 
> This is because you are reading binary bytes from
> the HTTP response
> and writing them as text integers to a PrintWriter. 
> You'll want to
> use getOutputStream() instead of getWriter().  I
> also suggest
> reading/writing more than one byte at a time.
> 
=============================
Or wrap your input and output streams with a
BufferedOutputStream....this is just one way to write
more bytes at the same time.
=============================

> Mike
> 
> On 9/14/05, Keith Campbell <KC...@paperloop.com>
> wrote:
> > Hi all,
> > 
> > I'm trying to use HttpClient to download an excel
> file from a web
> > application.
> > 
> > Everything works but in my final excel document I
> get a series of
> > meaningless integers printed out to the document.
> > Am I missing something obvious?
> > 
> > 
> > xml = method.getResponseBodyAsStream();
> > 
> > PrintWriter out = response.getWriter();
> > 
> > response.setHeader(
> "content-disposition","attachment;
> > filename=projectList.xls" );
> >
> response.setContentType("application/vnd.ms-excel");
> > 
> > int excel;
> > while((excel=xml.read())!=-1){
> >      out.print(excel);
> > }
> > 
> > Thanks in advance,
> > Keith


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: Excel download question

Posted by Roland Weber <RO...@de.ibm.com>.
Hi Keith,

don't forget to cast the 'excel' int down to a byte
before writing it to the output stream. I second
Mike's suggestion to read a bunch of bytes into
an array and write them out all at once.

cheers,
  Roland




Michael Becke <mb...@gmail.com> 
15.09.2005 04:13
Please respond to
"HttpClient User Discussion"


To
HttpClient User Discussion <ht...@jakarta.apache.org>
cc

Subject
Re: Excel download question






Hi Keith,

This is because you are reading binary bytes from the HTTP response
and writing them as text integers to a PrintWriter.  You'll want to
use getOutputStream() instead of getWriter().  I also suggest
reading/writing more than one byte at a time.

Mike

On 9/14/05, Keith Campbell <KC...@paperloop.com> wrote:
> Hi all,
> 
> I'm trying to use HttpClient to download an excel file from a web
> application.
> 
> Everything works but in my final excel document I get a series of
> meaningless integers printed out to the document.
> Am I missing something obvious?
> 
> 
> xml = method.getResponseBodyAsStream();
> 
> PrintWriter out = response.getWriter();
> 
> response.setHeader( "content-disposition","attachment;
> filename=projectList.xls" );
> response.setContentType("application/vnd.ms-excel");
> 
> int excel;
> while((excel=xml.read())!=-1){
>      out.print(excel);
> }
> 
> Thanks in advance,
> Keith
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Re: Excel download question

Posted by Michael Becke <mb...@gmail.com>.
Hi Keith,

This is because you are reading binary bytes from the HTTP response
and writing them as text integers to a PrintWriter.  You'll want to
use getOutputStream() instead of getWriter().  I also suggest
reading/writing more than one byte at a time.

Mike

On 9/14/05, Keith Campbell <KC...@paperloop.com> wrote:
> Hi all,
> 
> I'm trying to use HttpClient to download an excel file from a web
> application.
> 
> Everything works but in my final excel document I get a series of
> meaningless integers printed out to the document.
> Am I missing something obvious?
> 
> 
> xml = method.getResponseBodyAsStream();
> 
> PrintWriter out = response.getWriter();
> 
> response.setHeader( "content-disposition","attachment;
> filename=projectList.xls" );
> response.setContentType("application/vnd.ms-excel");
> 
> int excel;
> while((excel=xml.read())!=-1){
>      out.print(excel);
> }
> 
> Thanks in advance,
> Keith
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org