Forums Register Login

Open a file in a jsp page

+Pie Number of slices to send: Send
Hello,

I am writing an application that allows users to upload a file and save it outside of the web application; c:\path to file\file.pdf.
The files will most likely be .doc, .pdf, .txt.

The users also need to be able to click on a hyperlink and display the file in a jsp page. I have not been able to find an answer to this in the archives. Anybody know of a solution?

Thanks,
Richard
+Pie Number of slices to send: Send
You probably couldn't find an answer in the archives because the answer is mostly nothing to do with JSPs.

To upload a file you use HTML like this:And to handle that upload on the server I would recommend you use Jakarta's Commons FileUpload instead of trying to deal with the request yourself. This belongs in a servlet rather than a JSP because it has nothing to do with arranging the HTML in the response.
+Pie Number of slices to send: Send
I appologize as I wasn't clear enough in my original post.

I already have the upload portion of the application working. My problem is I don't know how to read a file stored on the local file system, not the applciation's context, and display it in a jsp page.

This was easy when the files where stored inside the application because I only needed to provide a URL to the file. However, these files needed to be moved to a location outside the app and now I'm stuck.
+Pie Number of slices to send: Send
From outside the app, you would need to stream the file to the user from a servlet.

I don't have an example of this exact procedure but I have one that is close.
http://simple.souther.us/SimpleStream.war.

In this example, I stream images from under the WEB-INF directory using ServletContext.getResourceAsStream to read the files. You could do the same thing with your files but, instead of using getResourceAsStream, use java.io.FileReader to read the file's contents.

Writing to the ServletOutputStream would be the same.
[ September 18, 2006: Message edited by: Bear Bibeault ]
+Pie Number of slices to send: Send
Ben,

Thanks for the reply. The link you provided is broken. Do you have an alternative?

Richard
+Pie Number of slices to send: Send
 

Originally posted by Richard Elsberry:
Ben,

Thanks for the reply. The link you provided is broken. Do you have an alternative?

Richard



Just remove the "." from the end of the link.
+Pie Number of slices to send: Send
 

Originally posted by Elaine Micheals:


Just remove the "." from the end of the link.



Link repaired.
+Pie Number of slices to send: Send
I'm still stumped here. I must be missing something obvious. I've attached my code below.

I am attempting to open a pdf or doc file from the local file system. When the link is clicked on I get the dialogue box asking my to save or open the application. However, when it opens I see what appears to be raw data. Something like this:

�� ��� � > ��

for word docs and a "damaged file" messagee for pdf files.






HttpSession session = req.getSession();
Upload upload = (Upload)session.getAttribute("uploadRef");
//resp.setContentType ("application/doc");
//resp.setContentType ("application/pdf");
resp.setContentType ("application/x-download");
resp.addHeader ("Content-Disposition", "inline;filename=\""+upload.getFileName()+"\"");


try{

BufferedReader bufferedReader = new BufferedReader(new FileReader(upload.getLocalFilePath()));
String line;
PrintWriter out = resp.getWriter();
while ((line = bufferedReader.readLine()) != null)
{
out.println(line);
}
bufferedReader.close();
}
catch(Exception e){

}


I also tried this but get the same result:

//InputStream isStream = null;
//ServletOutputStream sosStream = null;
// try
// {
// //response.flushBuffer();
//isStream = new FileInputStream(new File(upload.getLocalFilePath()));
//sosStream = resp.getOutputStream();
//int ibit = 256;
// while ((ibit) >= 0)
// {
// ibit = isStream.read();
// sosStream.write(ibit);
// }
//
//}
//catch (IOException ioeException)
//{
// }
// sosStream.flush();
// sosStream.close();
// isStream.close();

any help would be greatly appreciated.

Richard
+Pie Number of slices to send: Send
 

a "damaged file" messagee for pdf files.


Thats because you are using a Reader and Writer - Readers and Writers apply a character conversion that is bound to cause trouble with binary data files.

Use a FileInputStream and byte methods. Read into a byte[] instead of one byte at a time.

Bill
+Pie Number of slices to send: Send
Bill,

Thanks for your assistance. I rewrote to include the FileInputStream as you suggested but still get the same result. I've made sure that this new code is being tested as the result is exactly the same. I'm missing something obvious but can't put my finger on it.

New code:

HttpSession session = req.getSession();
Upload upload = (Upload)session.getAttribute("uploadRef");
//resp.setContentType ("application/doc");
//resp.setContentType ("application/pdf");
resp.setContentType ("application/x-download");
resp.addHeader ("Content-Disposition", "inline;filename=\""+upload.getFileName()+"\"");

BufferedInputStream bis = null;
ServletOutputStream out = resp.getOutputStream();

try {
File file = new File(upload.getLocalFilePath());
FileInputStream fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
byte[] data = new byte[2048];
int readed = bis.read(data);
while (readed != -1)
{
out.write(data,0,readed);
readed = bis.read(data);
}
}
catch(Exception e){}
if (out != null) out.close();
if (bis != null) bis.close();
+Pie Number of slices to send: Send
Don't forget to "flush" your data (it should be done implicitly by close, but it is better to do it yourself.

out.flush()

This code is in a servlet right? Not in a JSP?
JSPs can add extra carriage return characters into your outputstream/writer which will corrupt the download.
+Pie Number of slices to send: Send
Hi Stefan,

I added the out.flush() to my code. Yes, this code is in a doGet() method of a servlet. It's being called by a struts action:

<a href="/pages/download/download.do">upload</a>

While composing this repsonse I figured out that I could get the download to work if I called the servlet directly and not via the struts action. So it had something to do with how Struts processes request/responses.

I was using the Struts action because I wanted to open the download in a new window. However, I now know that:

resp.setHeader ("Content-Disposition", "attachment;filename=\""+upload.getFileName()+"\"");

the "attachment" parameter of setHeader() accomplishes the same thing. This would be a good thing for folks in the Struts list to know about.

Thanks for the assistance...once again!
Richard
+Pie Number of slices to send: Send
Thats a step forward then.
If it is struts, make sure you return null from your action to indicate that you handled the response, and struts doesn't need to send one.
+Pie Number of slices to send: Send
I found this thread helpful. I have a related question.
I've built a fileupload jsp and I want to get the original filename of a file. I put the file into a FileItem object and use the getName function. When I'm in Firefox it works perfectly, as it gets just the name and not the path. However when I'm in IE it uses the entire path of the file as the name. Has anyone encountered this problem and how did you get around it?
+Pie Number of slices to send: Send
Jacob, you'd probably be better off starting a new thread with this question.
It's not really that close to the original in this thread.
Yup, yup, yup. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 3965 times.
Similar Threads
Is J2EE appropriate?
Excel files-Weekly,Monthly,Yearly
Hiding a JSP from public access
As JSP File Size grows?
clearing address bar
Create Bookmark Over 40 Characters & Suppressed Endnotes in Fixed Page File in Java/.NET Apps
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 06:39:10.