JSP page to open/download a file

M

Matt

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!!
 
R

RobM

Matt said:
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
 
R

RobM

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,082
Members
48,773
Latest member
Kaybee

Latest Threads

Top