Streaming files to Firefox

Discussion in 'Java' started by WJ, Apr 30, 2005.

  1. WJ

    WJ Guest

    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!
     
    WJ, Apr 30, 2005
    #1
    1. Advertising

  2. 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.
     
    Adam P. Jenkins, Apr 30, 2005
    #2
    1. Advertising

  3. WJ

    shakah Guest

    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!
     
    shakah, Apr 30, 2005
    #3
  4. WJ

    WJ Guest

    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.
     
    WJ, Apr 30, 2005
    #4
  5. WJ

    WJ Guest

    Great Catch! But that didn't fix the problem. :-(
     
    WJ, Apr 30, 2005
    #5
  6. WJ

    WJ Guest

    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.
     
    WJ, Apr 30, 2005
    #6
  7. WJ

    WJ Guest

    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.
     
    WJ, Apr 30, 2005
    #7
  8. 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?
     
    Abrasive Sponge, May 1, 2005
    #8
  9. WJ

    Chris Smith Guest

    WJ <> 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
     
    Chris Smith, May 1, 2005
    #9
  10. 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());
     
    Adam P. Jenkins, May 1, 2005
    #10
  11. WJ

    WJ Guest

    The real problem I was having was streaming a PDF from jasper reports.

    In my code, I am using res.setHeader("Content-Type","application/pdf");

    When I couldn't get that to work, I wrote a scratch servlet to stream a pdf
    from my own file system, setting the length as
    res.setContentLength((int)testFile.length());

    That didn't work either, so I went back to basics, hard coding value I knew.

    I've wanted to download the jasper source to see if it actually sets the
    contentLength.
    I never did because it's worked great thus far, until I ran into this, which
    probably
    isn't a jasper issue anyway.

    Thanks for all the feedback!
     
    WJ, May 1, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Edwards
    Replies:
    1
    Views:
    316
    Hermit Dave
    Jan 16, 2004
  2. Troy

    streaming text (csv) files

    Troy, Feb 11, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    5,871
  3. Mark Kelly

    Streaming ZIPPED files

    Mark Kelly, May 11, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    544
    Mark Kelly
    May 12, 2004
  4. MattC
    Replies:
    1
    Views:
    4,277
    Steve C. Orr [MVP, MCSD]
    Jun 14, 2005
  5. A.M-SG

    Streaming large files over network

    A.M-SG, Oct 28, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    650
    Steven Cheng[MSFT]
    Nov 7, 2005
Loading...

Share This Page