I am writing an application where a user can download files. These files can be of type pdf, doc, xls, csv or ppt. The jsp which has the links to the files calls a DownloadServlet which fetches the file and writes it to the outout stream. These files are not located under the WEB-INF folder but on another directory (eg. C:\downloads).
When the user clicks on the link, it pops up a box which asks if they want to Save or Open the file. All files work fine when they are downloaded. When user tries to open a pdf file, it opens up Adobe Reader but then gives an error message-
"There was an error opening this document. This file cannot be found."
The doc & xls files open up fine.
Here is the code to my download servlet -
public class DownloadServlet extends HttpServlet {
//Initialize global variables
String fileName="";
private static Logger log = Logger.getLogger(DownloadServlet.class.getName());
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doPost(request,response);
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
log.debug("Entered DownloadServlet");
OutputStream outStream = response.getOutputStream();
fileName=request.getParameter("fileName");
log.debug("Filename:"+fileName);
String filePath = "C:\\test
reference-material";
File f=new File(filePath, fileName);
String fileType = fileName.substring(fileName.indexOf(".")+1,fileName.length());
log.debug("Filetype:"+fileType+";"+f.length());
if (fileType.trim().equalsIgnoreCase("txt")) {
response.setContentType( "text/plain" );
} else if (fileType.trim().equalsIgnoreCase("doc")) {
response.setContentType( "application/msword" );
} else if (fileType.trim().equalsIgnoreCase("xls")) {
response.setContentType( "application/vnd.ms-excel" );
} else if (fileType.trim().equalsIgnoreCase("pdf")) {
response.setContentType( "application/pdf" );
log.debug("content type set to pdf");
} else {
response.setContentType( "application/octet-stream" );
}
response.setContentLength((int)f.length());
response.setHeader("Content-Disposition","attachment; filename=\"SecurityPatterns.pdf\"");
response.setHeader("Cache-Control", "no-cache");
byte[] buf = new byte[8192];
FileInputStream inStream = new FileInputStream(f);
int sizeRead = 0;
while ((sizeRead = inStream.read(buf, 0, buf.length)) > 0) {
log.debug("size:"+sizeRead);
outStream.write(buf, 0, sizeRead);
}
inStream.close();
outStream.close();
}
//Get Servlet information
public String getServletInfo() {
return "DownloadServlet Information";
}
}
-
Opening pdf using jsp (3 messages)
- Posted by: Poorav Sheth
- Posted on: December 07 2005 16:19 EST
Threaded Messages (3)
- Download component by Marina Prikaschikova on December 08 2005 03:07 EST
- Download component by Poorav Sheth on December 08 2005 10:58 EST
- Re: Opening pdf using jsp by Jeff Koenke on October 17 2007 09:52 EDT
-
Download component[ Go to top ]
- Posted by: Marina Prikaschikova
- Posted on: December 08 2005 03:07 EST
- in response to Poorav Sheth
Check out Download servlet in JSOS:
http://www.servletsuite.com/servlets.htm -
Download component[ Go to top ]
- Posted by: Poorav Sheth
- Posted on: December 08 2005 10:58 EST
- in response to Marina Prikaschikova
Thanks, that works great.
Although, I am curious to find out what I was doing wrong. -
Re: Opening pdf using jsp[ Go to top ]
- Posted by: Jeff Koenke
- Posted on: October 17 2007 09:52 EDT
- in response to Poorav Sheth
The following fixed the problem for me. response.setHeader("Cache-Control", "max-age=60"); Another possible solution is to change the content type from "application/pdf" to "application/octet-stream".