Reputation: 1782
I have a JSP page with some back end Java logic written to it. The page basically displays a PDF document on the browser when I paste the URL on the browser. The URL is in this format:
http://localhost:8080/repository/file/view/viewPDF.jsp?nodeID=27455
When I paste the URL, it's able to display the docoment properly but at the same time I want it to automatically download the document. By the browser's behaviour, it should have a download icon on the bottom left of the browser.
My only problem is it doesn't download at all but only shows the PDF document.
My viewPDF.JSP:
<%
///This section loads the PDF document on the browser
response.setContentType("application/pdf");
boolean debug = true;
try {
String snodeid = request.getParameter("nodeID");
long nodeid = Long.parseLong(snodeid);
Pdfinfo pdf = PPFacade.getPDFInfo(nodeid);
String pdfpath = pdf.getFfullpath();
if (debug) {
System.out.println("=============== PDF STREAM ================");
System.out.println("pdfpath = " + pdfpath);
}
//int len = (int)new File("D://test.pdf").length();
int len = (int) new File(pdfpath).length();
response.setContentLength(len);
byte[] buf = new byte[len];
FileInputStream pdfin = new FileInputStream(pdfpath);
pdfin.read(buf);
pdfin.close();
OutputStream pdfout = response.getOutputStream();
pdfout.write(buf, 0, len);
pdfout.flush();
if (debug) {
System.out.println("=============== END PDF STREAM ================");
}
///End of section
///////Automatically download file attachment
InitialContext ctx1 = new InitialContext();
FileFacadeLocal fileFacade1 = (FileFacadeLocal) ctx1.lookup("java:comp/env/file");
SettingsFacadeLocal settingsFacade1 = (SettingsFacadeLocal) ctx1.lookup("java:comp/env/settings");
Modlattr mod = settingsFacade1.get("ROOTFOLDER");
if (mod == null) {
throw new Exception("Unable to obtain system properties.");
}
String folder = mod.getAtval() + "/download/";
int count = 1;
if (count == 1) {
// long fileID = Long.parseLong(request.getParameter("f0"));
Fmedia fmedia = fileFacade1.get_file(nodeid);
if (fmedia == null) {
throw new Exception(fileFacade1.getMsg());
}
String OriginalName = fmedia.getFdesc();
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=\"" + OriginalName + "\"");
ServletOutputStream servletOutput = response.getOutputStream();
FileInputStream srcFile = new FileInputStream(fmedia.getFfulpath() + fmedia.getFgname());
byte[] buff = new byte[4096];
int bytesRead = 0;
while ((bytesRead = srcFile.read(buff)) != -1) {
servletOutput.write(buff, 0, bytesRead);
}
srcFile.close();
servletOutput.close();
} else {
long nodeID = Long.parseLong(request.getParameter("nodeID"));
Fmediainfo finfo = fileFacade.GetInfo(nodeID);
if (finfo == null) {
throw new Exception("Unable to locate file information.");
}
List Files = new ArrayList();
for (int i = 0; i < count; i++) {
long fileID = Long.parseLong(request.getParameter("f" + i));
Fmedia fmedia = fileFacade.get_file(fileID);
if (fmedia == null) {
throw new Exception(fileFacade.getMsg());
}
Files.add(fmedia);
}
byte[] buf1 = new byte[1024];
String zipFileUUID = folder + UUID.randomUUID().toString();
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFileUUID));
Iterator i = Files.iterator();
while (i.hasNext()) {
Fmedia fmedia = (Fmedia) i.next();
FileInputStream in = new FileInputStream(fmedia.getFfulpath() + fmedia.getFgname());
// Add ZIP entry to output stream.
zipOut.putNextEntry(new ZipEntry(fmedia.getFoname()));
int len1;
while ((len1 = in.read(buf1)) > 0) {
zipOut.write(buf1, 0, len1);
}
// Complete the entry
zipOut.closeEntry();
in.close();
}
zipOut.close();
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment; filename=\"" + finfo.getFmrefno() + ".zip\"");
ServletOutputStream servletOutput = response.getOutputStream();
FileInputStream srcFile = new FileInputStream(zipFileUUID);
byte[] buff = new byte[4096];
int bytesRead = 0;
while ((bytesRead = srcFile.read(buff)) != -1) {
servletOutput.write(buff, 0, bytesRead);
}
srcFile.close();
servletOutput.close();
File zFile = new File(zipFileUUID);
zFile.delete();
}
/////////////////
The code might be a little bit long but I just think it's better to include them.The only important section is the 2nd part of the code which is responsible for downloading the file.
I have tried debugging it and it does pass the correct values, so now I am not sure why it's not working.
Upvotes: 0
Views: 797
Reputation: 93
If you want to force the browser to download your file instead of opening the pdf, you could try setting the response type to application/force-download.
response.setContentType("application/force-download");
Upvotes: 1