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

时间:2019-03-14 07:19:28

标签: java jsp

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

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

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

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

我的观点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();

        }







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

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

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

1 个答案:

答案 0 :(得分:1)

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

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