You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by David Kerber <dc...@verizon.net> on 2006/10/14 04:13:06 UTC
Writing a text file back to browser from a servlet
I'm trying to do something that seems like it should be very easy, but
can't get it to work: sending a .txt file back to the user's browser so
they can save it to their local hard disk. I am having no trouble
creating the file and writing it to a temporary place on the server, but
can't figure out what I need to do so the users can save the result.
Right now, I'm using the following code excerpt:
fReader = new FileReader( fullFileName );
bReader = new BufferedReader( fReader );
oStream = response.getOutputStream();
while (( logString = bReader.readLine()) != null ) {
// I'm sure a println would also work, but this is what I happen
to have right now
oStream.write(( logString + EddRcvr.newLineStr ).getBytes());
}
oStream.close();
bReader.close();
fReader.close();
And it *displays* just fine in the browser window, but when I try to
save it (using firefox 1.5.x), the download windows says it completed
successfully, but the file is never there when I try to look at it.
Any suggestions as to what I'm missing?
TIA!
Dave
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
RE: Writing a text file back to browser from a servlet
Posted by Tim Lucia <ti...@yahoo.com>.
Leave out the Content-Disposition header and you'll get a plain text
response. You originally said "so they can save it to their local hard
disk." FireFox uses the name of the request as the file to save as when one
is not specified in the response -- so that may explain why using File->Save
As gives unpredictable results.
Tim
-----Original Message-----
From: David Kerber [mailto:dckerber@verizon.net]
Sent: Monday, October 16, 2006 8:23 PM
To: Tomcat Users List
Subject: Re: Writing a text file back to browser from a servlet
This worked, though not exactly how I expected. The "save file as..."
box comes up and works, but is there a way of showing the file in the
browser screen as well? Right now, it keeps the previous page displayed
while prompting to save the download. I can live with that if I have
to, but it would be nice if I could show my users the file content in
the browser as well.
Thanks!
Dave
David Kerber wrote:
> Thanks, Tim - I'll give that a try later today.
>
> Dave
>
>
> Tim Lucia wrote:
>
>> You could stream it directly to the user, if practical (why write to
>> a temp
>> file only to stream that back to the user?) In order to be
>> recognized by
>> the browser as text and an attachment, you should:
>>
>> response.setContentType("text/plain; charset=UTF-8");
>> response.setHeader("Content-disposition", "attachment;
>> filename=xxxx.txt");
>>
>> charset is optional...
>>
>>
>>
>> Tim
>>
>>
>> -----Original Message-----
>> From: David Kerber [mailto:dckerber@verizon.net] Sent: Friday,
>> October 13, 2006 10:13 PM
>> To: Tomcat Users List
>> Subject: Writing a text file back to browser from a servlet
>>
>> I'm trying to do something that seems like it should be very easy,
>> but can't get it to work: sending a .txt file back to the user's
>> browser so they can save it to their local hard disk. I am having no
>> trouble creating the file and writing it to a temporary place on the
>> server, but can't figure out what I need to do so the users can save
>> the result.
>>
>> Right now, I'm using the following code excerpt:
>>
>> fReader = new FileReader( fullFileName );
>> bReader = new BufferedReader( fReader );
>> oStream = response.getOutputStream();
>> while (( logString = bReader.readLine()) != null ) {
>> // I'm sure a println would also work, but this is what I happen
>> to have right now
>> oStream.write(( logString + EddRcvr.newLineStr ).getBytes());
>> }
>> oStream.close();
>> bReader.close();
>> fReader.close();
>>
>>
>> And it *displays* just fine in the browser window, but when I try to
>> save it (using firefox 1.5.x), the download windows says it completed
>> successfully, but the file is never there when I try to look at it.
>>
>> Any suggestions as to what I'm missing?
>>
>> TIA!
>> Dave
>
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Writing a text file back to browser from a servlet
Posted by David Kerber <dc...@verizon.net>.
This worked, though not exactly how I expected. The "save file as..."
box comes up and works, but is there a way of showing the file in the
browser screen as well? Right now, it keeps the previous page displayed
while prompting to save the download. I can live with that if I have
to, but it would be nice if I could show my users the file content in
the browser as well.
Thanks!
Dave
David Kerber wrote:
> Thanks, Tim - I'll give that a try later today.
>
> Dave
>
>
> Tim Lucia wrote:
>
>> You could stream it directly to the user, if practical (why write to
>> a temp
>> file only to stream that back to the user?) In order to be
>> recognized by
>> the browser as text and an attachment, you should:
>>
>> response.setContentType("text/plain; charset=UTF-8");
>> response.setHeader("Content-disposition", "attachment;
>> filename=xxxx.txt");
>>
>> charset is optional...
>>
>>
>>
>> Tim
>>
>>
>> -----Original Message-----
>> From: David Kerber [mailto:dckerber@verizon.net] Sent: Friday,
>> October 13, 2006 10:13 PM
>> To: Tomcat Users List
>> Subject: Writing a text file back to browser from a servlet
>>
>> I'm trying to do something that seems like it should be very easy,
>> but can't get it to work: sending a .txt file back to the user's
>> browser so they can save it to their local hard disk. I am having no
>> trouble creating the file and writing it to a temporary place on the
>> server, but can't figure out what I need to do so the users can save
>> the result.
>>
>> Right now, I'm using the following code excerpt:
>>
>> fReader = new FileReader( fullFileName );
>> bReader = new BufferedReader( fReader );
>> oStream = response.getOutputStream();
>> while (( logString = bReader.readLine()) != null ) {
>> // I'm sure a println would also work, but this is what I happen
>> to have right now
>> oStream.write(( logString + EddRcvr.newLineStr ).getBytes());
>> }
>> oStream.close();
>> bReader.close();
>> fReader.close();
>>
>>
>> And it *displays* just fine in the browser window, but when I try to
>> save it (using firefox 1.5.x), the download windows says it completed
>> successfully, but the file is never there when I try to look at it.
>>
>> Any suggestions as to what I'm missing?
>>
>> TIA!
>> Dave
>
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Writing a text file back to browser from a servlet
Posted by David Kerber <dc...@verizon.net>.
Thanks, Tim - I'll give that a try later today.
Dave
Tim Lucia wrote:
>You could stream it directly to the user, if practical (why write to a temp
>file only to stream that back to the user?) In order to be recognized by
>the browser as text and an attachment, you should:
>
>response.setContentType("text/plain; charset=UTF-8");
>response.setHeader("Content-disposition", "attachment; filename=xxxx.txt");
>
>
>charset is optional...
>
>
>
>Tim
>
>
>-----Original Message-----
>From: David Kerber [mailto:dckerber@verizon.net]
>Sent: Friday, October 13, 2006 10:13 PM
>To: Tomcat Users List
>Subject: Writing a text file back to browser from a servlet
>
>I'm trying to do something that seems like it should be very easy, but
>can't get it to work: sending a .txt file back to the user's browser so
>they can save it to their local hard disk. I am having no trouble
>creating the file and writing it to a temporary place on the server, but
>can't figure out what I need to do so the users can save the result.
>
>Right now, I'm using the following code excerpt:
>
> fReader = new FileReader( fullFileName );
> bReader = new BufferedReader( fReader );
> oStream = response.getOutputStream();
> while (( logString = bReader.readLine()) != null ) {
> // I'm sure a println would also work, but this is what I happen
>to have right now
> oStream.write(( logString + EddRcvr.newLineStr ).getBytes());
> }
> oStream.close();
> bReader.close();
> fReader.close();
>
>
>And it *displays* just fine in the browser window, but when I try to
>save it (using firefox 1.5.x), the download windows says it completed
>successfully, but the file is never there when I try to look at it.
>
>Any suggestions as to what I'm missing?
>
>TIA!
>Dave
>
>
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
RE: Writing a text file back to browser from a servlet
Posted by Tim Lucia <ti...@yahoo.com>.
You could stream it directly to the user, if practical (why write to a temp
file only to stream that back to the user?) In order to be recognized by
the browser as text and an attachment, you should:
response.setContentType("text/plain; charset=UTF-8");
response.setHeader("Content-disposition", "attachment; filename=xxxx.txt");
charset is optional...
Tim
-----Original Message-----
From: David Kerber [mailto:dckerber@verizon.net]
Sent: Friday, October 13, 2006 10:13 PM
To: Tomcat Users List
Subject: Writing a text file back to browser from a servlet
I'm trying to do something that seems like it should be very easy, but
can't get it to work: sending a .txt file back to the user's browser so
they can save it to their local hard disk. I am having no trouble
creating the file and writing it to a temporary place on the server, but
can't figure out what I need to do so the users can save the result.
Right now, I'm using the following code excerpt:
fReader = new FileReader( fullFileName );
bReader = new BufferedReader( fReader );
oStream = response.getOutputStream();
while (( logString = bReader.readLine()) != null ) {
// I'm sure a println would also work, but this is what I happen
to have right now
oStream.write(( logString + EddRcvr.newLineStr ).getBytes());
}
oStream.close();
bReader.close();
fReader.close();
And it *displays* just fine in the browser window, but when I try to
save it (using firefox 1.5.x), the download windows says it completed
successfully, but the file is never there when I try to look at it.
Any suggestions as to what I'm missing?
TIA!
Dave
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org