gpt4 book ai didi

java - 无法在浏览器上下载文件文档?

转载 作者:行者123 更新时间:2023-12-02 10:08:33 25 4
gpt4 key购买 nike

我有一个 JSP 页面,其中写入了一些后端 Java 逻辑。当我将 URL 粘贴到浏览器上时,该页面基本上会在浏览器上显示 PDF 文档。 URL 的格式如下:

http://localhost:8080/repository/file/view/viewPDF.jsp?nodeID=27455

当我粘贴 URL 时,它能够正确显示文档,但同时我希望它自动下载文档。根据浏览器的行为,浏览器左下角应该有一个下载图标。

我唯一的问题是它根本不下载,而只显示 PDF 文档。

我的 View PDF.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();

}







/////////////////

代码可能有点长,但我认为最好包含它们。唯一重要的部分是代码的第二部分,负责下载文件。

我尝试过调试它,它确实传递了正确的值,所以现在我不确定为什么它不起作用。

最佳答案

如果您想强制浏览器下载文件而不是打开 pdf,您可以尝试将响应类型设置为 application/force-download。

response.setContentType("application/force-download");

关于java - 无法在浏览器上下载文件文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55156911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com