Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Streaming files to Firefox

Reply
Thread Tools

Streaming files to Firefox

 
 
WJ
Guest
Posts: n/a
 
      04-30-2005
I have some simple code that reads a file from disk and streams it to a web
browser.

The servlet container is Tomcat 5.0.28
IE 6.0.3790
Firefox 1.0.3

This works for IE, but not Firefox:

ServletOutputStream out = res.getOutputStream();
File testFile = new File("C:/temp/MyTestPDF.pdf");

res.setHeader("Content-Type","application/pdf");
res.setHeader("Content-Length","4230364");
res.setHeader("Content-Disposition", "inline;
filename=\"MyTestPDF.pdf");

int bufferSize;
byte[] buffer=new byte[2048];
FileInputStream fis=new FileInputStream(testFile);
while( (bufferSize=fis.read(buffer)) != -1)
out.write(buffer, 0, bufferSize);
out.close();


Now, if I change the Content-Disposition to this:
res.setHeader("Content-Disposition", "attachment;
filename=\"MyTestPDF.pdf");
Both browsers correctly prompt me to open or download the file.

I want the file to stream (eventually into a new window) without being
prompted.
Any help is greatly appreciated!






 
Reply With Quote
 
 
 
 
Adam P. Jenkins
Guest
Posts: n/a
 
      04-30-2005
WJ wrote:
> I have some simple code that reads a file from disk and streams it to a web
> browser.
>
> The servlet container is Tomcat 5.0.28
> IE 6.0.3790
> Firefox 1.0.3
>
> This works for IE, but not Firefox:
>
> ServletOutputStream out = res.getOutputStream();
> File testFile = new File("C:/temp/MyTestPDF.pdf");
>
> res.setHeader("Content-Type","application/pdf");
> res.setHeader("Content-Length","4230364");
> res.setHeader("Content-Disposition", "inline;
> filename=\"MyTestPDF.pdf");
>
> int bufferSize;
> byte[] buffer=new byte[2048];
> FileInputStream fis=new FileInputStream(testFile);
> while( (bufferSize=fis.read(buffer)) != -1)
> out.write(buffer, 0, bufferSize);
> out.close();
>
>
> Now, if I change the Content-Disposition to this:
> res.setHeader("Content-Disposition", "attachment;
> filename=\"MyTestPDF.pdf");
> Both browsers correctly prompt me to open or download the file.
>
> I want the file to stream (eventually into a new window) without being
> prompted.
> Any help is greatly appreciated!


You didn't mention what error you get from Firefox. Probably you just
need to enable the Acrobat Reader plugin under Firefox to make inline
PDFs work.
 
Reply With Quote
 
 
 
 
shakah
Guest
Posts: n/a
 
      04-30-2005
I'd expect the behavior to be client-dependent based on the browser
configuration. But it does look like you have an unbalanced
double-quote in your "Content-Disposition:" headers -- shouldn't the
value be "inline; filename=\"MyTestPDF.pdf\"" ?

WJ wrote:
> I have some simple code that reads a file from disk and streams it to

a web
> browser.
>
> The servlet container is Tomcat 5.0.28
> IE 6.0.3790
> Firefox 1.0.3
>
> This works for IE, but not Firefox:
>
> ServletOutputStream out = res.getOutputStream();
> File testFile = new File("C:/temp/MyTestPDF.pdf");
>
> res.setHeader("Content-Type","application/pdf");
> res.setHeader("Content-Length","4230364");
> res.setHeader("Content-Disposition", "inline;
> filename=\"MyTestPDF.pdf");
>
> int bufferSize;
> byte[] buffer=new byte[2048];
> FileInputStream fis=new FileInputStream(testFile);
> while( (bufferSize=fis.read(buffer)) != -1)
> out.write(buffer, 0, bufferSize);
> out.close();
>
>
> Now, if I change the Content-Disposition to this:
> res.setHeader("Content-Disposition", "attachment;
> filename=\"MyTestPDF.pdf");
> Both browsers correctly prompt me to open or download the file.
>
> I want the file to stream (eventually into a new window) without

being
> prompted.
> Any help is greatly appreciated!


 
Reply With Quote
 
WJ
Guest
Posts: n/a
 
      04-30-2005
Sorry.

The page just hangs when I try the download inline. If I do it as an
attachment,
it works fine in Firefox.

I thought of the plugin issue. Under tools -> Options ->Downloads -> I
have.pdf
associated with AcroExch, and under the Plug-ins button, it is enabled.





 
Reply With Quote
 
WJ
Guest
Posts: n/a
 
      04-30-2005
Great Catch! But that didn't fix the problem.



 
Reply With Quote
 
WJ
Guest
Posts: n/a
 
      04-30-2005
When I say the page hangs, I see the progress bar go across the bottom,
and on the Tomcat console, I see the pdf streaming down. It's as if
Firefox gets it, then does nothing with it. Afterward, Firefox is not
frozen,
but doesn't respond when I resize it, although the buttons work.



 
Reply With Quote
 
WJ
Guest
Posts: n/a
 
      04-30-2005
ok. I figured this out and am a bit surprised.

On my main dev box, I am running Windows 2003 Server.
This is where Firefox fails inline.

I went to another machine running Windows 2000 and
it ran just fine! So it seems to be an issue with Windows
Server 2003!

Thanks for all your help.


 
Reply With Quote
 
Abrasive Sponge
Guest
Posts: n/a
 
      05-01-2005
WJ wrote:
> I have some simple code that reads a file from disk and streams it to a web
> browser.
>
> The servlet container is Tomcat 5.0.28
> IE 6.0.3790
> Firefox 1.0.3
>
> This works for IE, but not Firefox:
>
> ServletOutputStream out = res.getOutputStream();
> File testFile = new File("C:/temp/MyTestPDF.pdf");
>
> res.setHeader("Content-Type","application/pdf");
> res.setHeader("Content-Length","4230364");
> res.setHeader("Content-Disposition", "inline;
> filename=\"MyTestPDF.pdf");
>
> int bufferSize;
> byte[] buffer=new byte[2048];
> FileInputStream fis=new FileInputStream(testFile);
> while( (bufferSize=fis.read(buffer)) != -1)
> out.write(buffer, 0, bufferSize);
> out.close();
>
>
> Now, if I change the Content-Disposition to this:
> res.setHeader("Content-Disposition", "attachment;
> filename=\"MyTestPDF.pdf");
> Both browsers correctly prompt me to open or download the file.
>
> I want the file to stream (eventually into a new window) without being
> prompted.
> Any help is greatly appreciated!
>
>
>
>
>
>

Well, my suggestions are

1. why don't you use...file.length() to get the file length correctly
2. why don't you use...response.setContentLength()?
3. why don't you use...response.setContentType()?
4. why don't you use...BufferedOutputStream to do your buffering work?
5. why don't you use the correct try..catch...finally structure with
your servlet?

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-01-2005
WJ <(E-Mail Removed)> wrote:
> On my main dev box, I am running Windows 2003 Server.
> This is where Firefox fails inline.
>
> I went to another machine running Windows 2000 and
> it ran just fine! So it seems to be an issue with Windows
> Server 2003!


Unless you've tried with several substantially different installations
of Windows Server 2003, it's more likely a configuration issue on your
specific system.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Reply With Quote
 
Adam P. Jenkins
Guest
Posts: n/a
 
      05-01-2005
Adam P. Jenkins wrote:
> WJ wrote:
>
>> I have some simple code that reads a file from disk and streams it to
>> a web
>> browser.
>>
>> The servlet container is Tomcat 5.0.28
>> IE 6.0.3790
>> Firefox 1.0.3
>>
>> This works for IE, but not Firefox:
>>
>> ServletOutputStream out = res.getOutputStream();
>> File testFile = new File("C:/temp/MyTestPDF.pdf");
>>
>> res.setHeader("Content-Type","application/pdf");
>> res.setHeader("Content-Length","4230364");


Actually, this may be your problem, if this is your actual code. You
have a hard-coded content-length. If your PDF file is actually shorter
than this, then Firefox may be waiting to receive the rest of it,
whereas maybe IE just handles this error more gracefully. I'd change
those lines to:

res.setContentType("application/pdf");
res.setContentLength((int)file.length());
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Issues with Firefox streaming audio.. Keith D. Lee Firefox 0 01-23-2012 07:33 PM
Streaming audio crashing Firefox elaich Firefox 4 11-08-2008 03:06 AM
firefox media streaming teabag Computer Support 4 06-06-2005 02:13 AM
dynamically generating files on web server and streaming back to client John Edwards ASP .Net 1 01-16-2004 11:35 PM



Advertisments