aspose file tools*
The moose likes JSP and the fly likes Open a file in a jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Open a file in a jsp page" Watch "Open a file in a jsp page" New topic
Author

Open a file in a jsp page

Richard Elsberry
Ranch Hand

Joined: Mar 14, 2001
Posts: 40
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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18649
    
    8

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.
Richard Elsberry
Ranch Hand

Joined: Mar 14, 2001
Posts: 40
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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Richard Elsberry
Ranch Hand

Joined: Mar 14, 2001
Posts: 40
Ben,

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

Richard
Gail Mikels
Ranch Hand

Joined: May 07, 2001
Posts: 634
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.


Gail Mikels
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61413
    
  67

Originally posted by Elaine Micheals:


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


Link repaired.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Richard Elsberry
Ranch Hand

Joined: Mar 14, 2001
Posts: 40
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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12803
    
    5
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
Richard Elsberry
Ranch Hand

Joined: Mar 14, 2001
Posts: 40
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();
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
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.
Richard Elsberry
Ranch Hand

Joined: Mar 14, 2001
Posts: 40
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
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
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.
Jacob Fenwick
Ranch Hand

Joined: Apr 28, 2006
Posts: 55
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?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Open a file in a jsp page