JSP View Problem: send the content of the file to the client

M

Matt

I posted similar problem before, the problem is when the user selects
a file, and submit,
it will show the content to the user, or pop up a file download dialog
to save the file
in the local machine.

The approach I am using is to send the content of the file (character
text)
to the client.

It will pop up a file download dialog box, which is fine,
but the problem is the file name is the JSP file name, not the
filename of
the file I want to open. For example, I want to open myfile.doc, in
the file
download dialog box, it still shows fileview2.doc, and prompts the
user
to save the file as fileview2.doc instead of myfile.doc.

any ideas?? In my problem, what is the best approach to this problem?
Should I let
the web browser should take care of the file, or I should read the
file byte by
byte, and write it to the client??


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

//fileview2.jsp

<%@ page import="java.io.*"%>
<%
try
{
String fileName = request.getParameter("filename");
File file = new File(fileName);
response.addHeader("content-type", "application/msword"); //OPEN
MS-WORD DOCUMENT
BufferedInputStream bis = new BufferedInputStream(new
FileInputStream(file));
long count = file.length();
PrintWriter rsp = response.getWriter();
for (long index = 0; index < count; index ++)
{
int ch = bis.read();
rsp.write(ch);
}
bis.close();
}
catch(Exception e)
{ out.println(e);
}
%>
 
M

Murray

Matt said:
I posted similar problem before, the problem is when the user selects
a file, and submit,
it will show the content to the user, or pop up a file download dialog
to save the file
in the local machine.

The approach I am using is to send the content of the file (character
text)
to the client.

It will pop up a file download dialog box, which is fine,
but the problem is the file name is the JSP file name, not the
filename of
the file I want to open. For example, I want to open myfile.doc, in
the file
download dialog box, it still shows fileview2.doc, and prompts the
user
to save the file as fileview2.doc instead of myfile.doc.

any ideas?? In my problem, what is the best approach to this problem?
Should I let
the web browser should take care of the file, or I should read the
file byte by
byte, and write it to the client??


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

//fileview2.jsp

<%@ page import="java.io.*"%>
<%
try
{
String fileName = request.getParameter("filename");
File file = new File(fileName);
response.addHeader("content-type", "application/msword"); //OPEN
MS-WORD DOCUMENT
BufferedInputStream bis = new BufferedInputStream(new
FileInputStream(file));
long count = file.length();
PrintWriter rsp = response.getWriter();
for (long index = 0; index < count; index ++)
{
int ch = bis.read();
rsp.write(ch);
}
bis.close();
}
catch(Exception e)
{ out.println(e);
}
%>

Try adding the following header

response.setHeader("Content-Disposition", "attachment; filename=\"" +
shortFilename + "\"");

where shortFilename is just the name of the file itself without any
directory prefix.

I'd also advise against using response.getWriter() unless you're confident
you won't be using binary files. Use response.getOutputStream() instead
 

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

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,093
Latest member
EmiliaAlfo

Latest Threads

Top