JSP page to open/download a file

Discussion in 'Java' started by Matt, Sep 4, 2004.

  1. Matt

    Matt Guest

    I want to write a JSP page that allows to open/download a file

    In fileview.jsp, I have the file browse, and when user click submit button,
    fileview2.jsp should open the file. I am using response.sendRedirect(), but
    I don't know if this is correct. It has the error:
    Error 404: File not found: fileview/C:/app.log


    fileview.jsp
    ============
    <FORM ACTION="fileview2.jsp" method="POST">
    <P><input type="FILE" name="filename">
    <P><input type="submit">

    fileview2.jsp
    ============
    <%
    String filename = request.getParameter("filename");
    response.addHeader("content-type", "text/plain");
    response.sendRedirect(filename);
    %>

    any ideas? please advise what I am missing. thanks!!
     
    Matt, Sep 4, 2004
    #1
    1. Advertising

  2. Matt

    RobM Guest

    "Matt" <> wrote in message
    news:...
    > I want to write a JSP page that allows to open/download a file
    >
    > In fileview.jsp, I have the file browse, and when user click submit

    button,
    > fileview2.jsp should open the file. I am using response.sendRedirect(),

    but
    > I don't know if this is correct. It has the error:
    > Error 404: File not found: fileview/C:/app.log
    >
    >
    > fileview.jsp
    > ============
    > <FORM ACTION="fileview2.jsp" method="POST">
    > <P><input type="FILE" name="filename">
    > <P><input type="submit">
    >
    > fileview2.jsp
    > ============
    > <%
    > String filename = request.getParameter("filename");
    > response.addHeader("content-type", "text/plain");
    > response.sendRedirect(filename);
    > %>
    >
    > any ideas? please advise what I am missing. thanks!!


    Hi Matt

    You have to remember that files accessed in this manner from a web page
    (either by redirect or by an anchor/link) have to be accessible to that web
    server, which normally means within the webapps/yourWebApplication directory
    on Tomcat for example. To show this example, copy your fileview2.jsp and
    rename it as fileview2.txt then enter that name into your first web page
    (without any directory path). It appears. This shows that the paths are
    relative to your web app (fileview2.txt being in the same place as
    fileview2.jsp which I assume is the root directory of your web app).

    You could add a subdirectory e.g. webapps/yourWebApplication/download in
    which case you would enter download/fileview2.txt

    The only way to access files such as C:/app.log is to physically read them
    in your JSP in java code (fileinputstream etc etc) and send the contents
    back; I would use a servlet for this though, beware of the newlines in JSP.
    I would also be aware of your header text/plain - are the files always that?

    Cheers
    Rob
     
    RobM, Sep 4, 2004
    #2
    1. Advertising

  3. Matt

    RobM Guest

    > Hi Matt
    >
    > You have to remember that files accessed in this manner from a web page
    > (either by redirect or by an anchor/link) have to be accessible to that

    web
    > server, which normally means within the webapps/yourWebApplication

    directory
    > on Tomcat for example. To show this example, copy your fileview2.jsp and
    > rename it as fileview2.txt then enter that name into your first web page
    > (without any directory path). It appears. This shows that the paths are
    > relative to your web app (fileview2.txt being in the same place as
    > fileview2.jsp which I assume is the root directory of your web app).
    >
    > You could add a subdirectory e.g. webapps/yourWebApplication/download in
    > which case you would enter download/fileview2.txt
    >
    > The only way to access files such as C:/app.log is to physically read them
    > in your JSP in java code (fileinputstream etc etc) and send the contents
    > back; I would use a servlet for this though, beware of the newlines in

    JSP.
    > I would also be aware of your header text/plain - are the files always

    that?
    >
    > Cheers
    > Rob
    >

    PS - I think you can also set alias on your webserver to go to the directory
    where your files are. I prefer the programmed method; more secure.
     
    RobM, Sep 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. Ryan Taylor
    Replies:
    2
    Views:
    1,028
    Ryan Taylor
    Nov 10, 2004
  2. Replies:
    0
    Views:
    4,598
  3. Brett  Kelly
    Replies:
    1
    Views:
    728
    Steve C. Orr [MVP, MCSD]
    Jun 16, 2006
  4. chandan
    Replies:
    0
    Views:
    998
    chandan
    Nov 17, 2008
  5. chandan
    Replies:
    0
    Views:
    527
    chandan
    Nov 17, 2008
Loading...

Share This Page