从服务器文件系统中的文件加载pdf浏览器? [英] loading a pdf in-browser from a file in the server file system?

查看:108
本文介绍了从服务器文件系统中的文件加载pdf浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取服务器目录结构中文件中的pdf,以便为 Spring MVC 应用程序的用户加载到浏览器中?

How can I get a pdf located in a file in a server's directory structure to load in a browser for users of a Spring MVC application?

我已经在Google上进行了搜索,并发现了有关如何生成PDF的帖子,但是在这种情况下他们的答案不起作用.例如,这条其他帖子不相关,因为我的代码中是res.setContentType("application/pdf");下面不能解决问题.另外,另一篇文章描述了如何从数据库中执行此操作,但未显示完整的工作控制器代码.其他帖子也有类似的问题,导致它们在这种情况下无法工作.

I have googled this and found postings about how to generate PDFs, but their answers do not work in this situation. For example, this other posting is not relevant because res.setContentType("application/pdf"); in my code below does not solve the problem. Also, this other posting describes how to do it from a database but does not show full working controller code. Other postings had similar problems that caused them not to work in this case.

我只需要简单地提供一个文件(而不是从数据库中),并让用户在其浏览器中可以查看它.我想出的最好的方法是下面的代码,该代码要求用户下载PDF或在浏览器之外的单独应用程序中查看它. 我可以对下面的特定代码进行哪些具体更改,以使用户在单击链接而不提示您下载链接时自动在浏览器中看到PDF内容?

I need to simply serve up a file (not from a database) and have it been viewable by a user in their browser. The best I have come up with is the code below, which asks the user to download the PDF or to view it in a separate application outside the browser. What specific changes can I make to the specific code below so that the user automatically sees the PDF content inside their browser when they click on the link instead of being prompted to download it?

@RequestMapping(value = "/test-pdf")
public void generatePdf(HttpServletRequest req,HttpServletResponse res){
    res.setContentType("application/pdf");
    res.setHeader("Content-Disposition", "attachment;filename=report.pdf");
    ServletOutputStream outStream=null;
    try {
        BufferedInputStream bis = new BufferedInputStream(
                new FileInputStream(new File("/path/to", "nameOfThe.pdf")));
            /*ServletOutputStream*/ outStream = res.getOutputStream();
            //to make it easier to change to 8 or 16 KBs
            int FILE_CHUNK_SIZE = 1024 * 4;
            byte[] chunk = new byte[FILE_CHUNK_SIZE];
            int bytesRead = 0;
            while ((bytesRead = bis.read(chunk)) != -1) {outStream.write(chunk, 0, bytesRead);}
            bis.close();
            outStream.flush();
            outStream.close();
    } 
    catch (Exception e) {e.printStackTrace();}
}

推荐答案

更改

res.setHeader("Content-Disposition", "attachment;filename=report.pdf");

收件人

res.setHeader("Content-Disposition", "inline;filename=report.pdf");

您还应该设置内容长度

FileCopyUtils 是方便:

@Controller
public class FileController {

    @RequestMapping("/report")
    void getFile(HttpServletResponse response) throws IOException {

        String fileName = "report.pdf";
        String path = "/path/to/" + fileName;

        File file = new File(path);
        FileInputStream inputStream = new FileInputStream(file);

        response.setContentType("application/pdf");
        response.setContentLength((int) file.length());
        response.setHeader("Content-Disposition", "inline;filename=\"" + fileName + "\"");

        FileCopyUtils.copy(inputStream, response.getOutputStream());

    }
}

这篇关于从服务器文件系统中的文件加载pdf浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆