ServletOutputStream to Browser OK in NS, but not IE??

Discussion in 'Java' started by TomG, May 3, 2004.

  1. TomG

    TomG Guest

    Hi All,

    I am replacing a Netscape Application Server appLogic with a servlet.
    The servlet is supposed to take a URL and stream the file to the
    user's browser. I checked a number of examples from post in this news
    group to get examples of how to do this (thanks for the good info).
    The servelt works fine in NetScape 7.0 (jpg, pdf, html files open in
    the current browser session), but always pops up the file download box
    in IE 6. In IE the file is always downloaded and opened. This
    happens even for html files. This is a problem, since the appLogic
    opens most files in the browser, Word docs get opened in a separate
    instance of MS Word. However, the user never sees the download box.

    I tried modifiying the web.xml for this servelet to allow adding the
    filename to to be appended to the servlet name,
    GetContentItem/filename.jpg, but that didn't make any difference. The
    content type and filename appear appear in the download dialog.

    Here is the servelt code, any suggestions will be appreciated:

    public class GetContentItem extends HttpServlet {
    // initalise the servlet within init()
    private static Log log = LogFactory.getLog(GetContentItem.class);

    public void doGet(HttpServletRequest request, HttpServletResponse
    response)
    throws ServletException, IOException {
    if (log.isDebugEnabled())
    log.debug("IN " + GetContentItem.class + " doGet" );

    String sCRBaseURLRoot =
    "http://supporttest1.web.lucent.com:20101/cr/";
    String sDocPath = "Live/Product/5ESS/R16/Retrofit_Information/090094058001c628.jpg";
    String sDocPath2 =
    "Live/Product/5ESS/R16/Release_Information/0900940580026c41/french.pdf";
    String sDocPath3 =
    "/Live/Product/5ESS/R16/Release_Information/0900940580026cc1/spanish.doc";
    String sDocPath4 =
    "/Live/Product/5ESS/R16/Release_Information/090094058002791c/61402_Release_Deployment.html";
    String filename = "090094058001c628.jpg";
    String filename2= "french.pdf";
    String filename3= "spanish.doc";
    String filename4 = "61402_Release_Deployment.html";
    sDocPath=sDocPath4;
    filename = filename4;
    String sUrl = sCRBaseURLRoot + sDocPath;
    java.io.InputStream in=null;
    ServletOutputStream stream = null;

    try{
    URL u = new URL(sUrl);
    URLConnection c = u.openConnection();
    c.connect();

    in = new DataInputStream(c.getInputStream());

    String sHeaderField=null;
    String sHeaderFieldKey=null;
    int i = 1;
    stream = response.getOutputStream();
    response.setContentType(c.getContentType());
    response.setContentLength(c.getContentLength());
    response.setHeader("Content-disposition","attachment;filename=\"" +
    filename + "\"");

    int bytes_read;
    int BUFSIZE = 8192;
    byte[] buffer = new byte[BUFSIZE];
    while((bytes_read=in.read(buffer,0,BUFSIZE))!=-1){
    stream.write(buffer,0,bytes_read);
    }

    } catch(FileNotFoundException e){
    log.error("FileNotFoundException accessing document " + sDocPath +
    e );

    } catch(Exception e){
    log.error("Exception accessing document " + sDocPath + e );

    } finally{
    try{
    in.close();
    stream.flush();
    stream.close();
    } catch (Exception e){}
    }
    }
    }

    I hope the code display format is not too ugly in the post.

    Thanks,

    Tom
     
    TomG, May 3, 2004
    #1
    1. Advertising

  2. TomG wrote:

    > Hi All,
    >
    > I am replacing a Netscape Application Server appLogic with a servlet.
    > The servlet is supposed to take a URL and stream the file to the
    > user's browser. I checked a number of examples from post in this news
    > group to get examples of how to do this (thanks for the good info).
    > The servelt works fine in NetScape 7.0 (jpg, pdf, html files open in
    > the current browser session), but always pops up the file download box
    > in IE 6. In IE the file is always downloaded and opened. This
    > happens even for html files. This is a problem, since the appLogic
    > opens most files in the browser, Word docs get opened in a separate
    > instance of MS Word. However, the user never sees the download box.


    I don't think it's causing too many problems for you at the moment, but
    you may find it useful in the future to get your head around the roles
    of the server and the user agent (browser). In particular, the server
    doesn't work "in" NetScape, and the appLogic doesn't open anything in
    the client's browser. The client requests information from the server
    via his user agent; the server responds with a message or messages; the
    client accepts and processes the messages via his user agent.

    If you have this clear then it is immediately evident that differing
    user agent behavior must be in response to differing messages received
    from the server. A reasonable debugging step, then, would be to capture
    and examine the HTTP responses from the appLogic to the client and from
    the servlet to the client.

    > I tried modifiying the web.xml for this servelet to allow adding the
    > filename to to be appended to the servlet name,
    > GetContentItem/filename.jpg, but that didn't make any difference. The


    Not surprising.

    > content type and filename appear appear in the download dialog.


    [...]

    > public void doGet(HttpServletRequest request, HttpServletResponse
    > response)
    > throws ServletException, IOException {


    [...]

    > stream = response.getOutputStream();
    > response.setContentType(c.getContentType());
    > response.setContentLength(c.getContentLength());
    > response.setHeader("Content-disposition","attachment;filename=\"" +
    > filename + "\"");


    So what do you think you are accomplishing with that header? Is it
    there for some specific reason? Have you considered that it's awfully
    odd to declare content that you want displayed inline as an attachment
    -- with a specified file name, even? I'd bet on that being a major
    contributor to the problem. If you need a content disposition at all
    then its value should probably be "inline".

    >
    > int bytes_read;
    > int BUFSIZE = 8192;
    > byte[] buffer = new byte[BUFSIZE];
    > while((bytes_read=in.read(buffer,0,BUFSIZE))!=-1){
    > stream.write(buffer,0,bytes_read);
    > }


    [...]


    John Bollinger
     
    John C. Bollinger, May 4, 2004
    #2
    1. Advertising

  3. TomG

    TomG Guest

    "John C. Bollinger" <> wrote in message news:<c788gl$obe$>...
    > TomG wrote:
    >
    > > Hi All,
    > >
    > > I am replacing a Netscape Application Server appLogic with a servlet.
    > > The servlet is supposed to take a URL and stream the file to the
    > > user's browser. I checked a number of examples from post in this news
    > > group to get examples of how to do this (thanks for the good info).
    > > The servelt works fine in NetScape 7.0 (jpg, pdf, html files open in
    > > the current browser session), but always pops up the file download box
    > > in IE 6. In IE the file is always downloaded and opened. This
    > > happens even for html files. This is a problem, since the appLogic
    > > opens most files in the browser, Word docs get opened in a separate
    > > instance of MS Word. However, the user never sees the download box.

    >
    > I don't think it's causing too many problems for you at the moment, but
    > you may find it useful in the future to get your head around the roles
    > of the server and the user agent (browser). In particular, the server
    > doesn't work "in" NetScape, and the appLogic doesn't open anything in
    > the client's browser. The client requests information from the server
    > via his user agent; the server responds with a message or messages; the
    > client accepts and processes the messages via his user agent.
    >
    > If you have this clear then it is immediately evident that differing
    > user agent behavior must be in response to differing messages received
    > from the server. A reasonable debugging step, then, would be to capture
    > and examine the HTTP responses from the appLogic to the client and from
    > the servlet to the client.
    >
    > > I tried modifiying the web.xml for this servelet to allow adding the
    > > filename to to be appended to the servlet name,
    > > GetContentItem/filename.jpg, but that didn't make any difference. The

    >
    > Not surprising.
    >
    > > content type and filename appear appear in the download dialog.

    >
    > [...]
    >
    > > public void doGet(HttpServletRequest request, HttpServletResponse
    > > response)
    > > throws ServletException, IOException {

    >
    > [...]
    >
    > > stream = response.getOutputStream();
    > > response.setContentType(c.getContentType());
    > > response.setContentLength(c.getContentLength());
    > > response.setHeader("Content-disposition","attachment;filename=\"" +
    > > filename + "\"");

    >
    > So what do you think you are accomplishing with that header? Is it
    > there for some specific reason? Have you considered that it's awfully
    > odd to declare content that you want displayed inline as an attachment
    > -- with a specified file name, even? I'd bet on that being a major
    > contributor to the problem. If you need a content disposition at all
    > then its value should probably be "inline".
    >
    > >
    > > int bytes_read;
    > > int BUFSIZE = 8192;
    > > byte[] buffer = new byte[BUFSIZE];
    > > while((bytes_read=in.read(buffer,0,BUFSIZE))!=-1){
    > > stream.write(buffer,0,bytes_read);
    > > }

    >
    > [...]
    >
    >
    > John Bollinger
    >


    John,

    Thanks for the comments. I must admit that I am new to servlets. I
    pasted the code together from some examples I found in different
    posts. You nailed my problem; the content-dispostion. I changed the
    setHeader to
    ("Content-disposition", ":inline;filename=\"" + filename + "\"") and
    things started working in IE. I wasn't aware of the different content
    dispositon types.

    Thanks again,

    Tom
     
    TomG, May 4, 2004
    #3
    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. Mikito Harakiri

    Redirect ServletOutputStream into a file?

    Mikito Harakiri, Apr 3, 2004, in forum: Java
    Replies:
    1
    Views:
    1,026
    Mikito Harakiri
    Apr 3, 2004
  2. TomG
    Replies:
    1
    Views:
    680
    marcus
    Jul 19, 2004
  3. Timo Nentwig
    Replies:
    6
    Views:
    4,050
    Kerry Sanders
    Aug 22, 2004
  4. ap
    Replies:
    1
    Views:
    4,221
    Ryan Stewart
    Feb 15, 2005
  5. Edward
    Replies:
    1
    Views:
    2,228
    Edward
    Feb 1, 2006
Loading...

Share This Page