java - 点击查看时将字节数组写入pdf文件

标签 java spring spring-mvc pdf

我已经完成了单击查看时将文件内容写入 pdf 文件的操作,但它没有自动打开。任何人都可以提供帮助吗?预先感谢。如果这里有任何错误,请纠正我。

@RequestMapping(value = { "/view" }, method = RequestMethod.GET)
        public static byte[] loadFile(@RequestParam("file_path") String file_path,@RequestParam("file_name") String fileName,HttpServletResponse response, HttpServletRequest request,HttpServletResponse resp) throws IOException, DocumentException {

        FileInputStream inputStream = null;
        try {
            String filePath = file_path.replace("/", "\\");
            File file = new File(filePath);
            inputStream = new FileInputStream(file);
            filePath.endsWith(".pdf");
            return readFully(inputStream, filePath, fileName, resp,   request,
                    resp);
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

这里我调用下面的方法并编写自动打开文件的代码。

@ResponseBody
    public static byte[] readFully(FileInputStream inputStream,
            @RequestParam("file_path") String file_path,
            @RequestParam("file_name") String fileName,
            HttpServletResponse response, HttpServletRequest request,
            HttpServletResponse resp) throws IOException, DocumentException {
        byte[] buffer = new byte[8192];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int bytesRead;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            baos.write(buffer, 0, bytesRead);
        }
        Document doc = new Document();
        PdfWriter.getInstance(doc, baos);
        doc.open();
        response.setContentType("application/pdf");
        response.setHeader("Content-disposition", "attachment; filename="
                + fileName + ".pdf");
        response.setHeader("Cache-Control",
                "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Expires", "0");
        response.setHeader("Pragma", "No-cache");
        response.setContentLength(baos.size());
        ServletOutputStream outn = response.getOutputStream();
        outn.flush();
        baos.writeTo(outn);
        File f = new File("C:\\Users\\Downloads\\" + fileName + ".pdf");
        if (f.exists()) {

            if (Desktop.isDesktopSupported()) {
                Desktop.getDesktop().open(f);
            } else {
                System.out.println("Awt Desktop is not supported!");
            }

        } else {
            System.out.println("File is not exists!");
        }
        System.out.println("Done");
        return baos.toByteArray();
    }   

最佳答案

您可以将您的inputstream写入spring fileInputStream。 您只需在 httpservletResponse 中写入输入流即可做到这一点。 请使用以下方法更改您的所有代码。您可以实现您的目标。

@RequestMapping(value = { "/view" }, method = RequestMethod.GET)
@ResponseBody
public void loadFile(@RequestParam("file_path") String file_path,@RequestParam("file_name") String fileName,HttpServletResponse response, HttpServletRequest request,HttpServletResponse resp) throws IOException, DocumentException {

    String filePath = file_path.replace("/", "\\");
    File file = new File(filePath);
    response.setHeader("Expires", "0");
    response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Pragma", "public");
    response.setHeader("Content-Type", "application/pdf");
    response.setHeader("Content-Disposition","attachment;filename="+fileName);
    response.setHeader("Content-Length", String.valueOf(file.length()));
    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        FileCopyUtils.copy(fileInputStream, httpServletResponse.getOutputStream());
        fileInputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

关于java - 点击查看时将字节数组写入pdf文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29414378/

相关文章:

Java-Spring-Hibernate-H2 : Disable auto-creating of database

java - 在 Jquery 中基于 KeyPair 循环 json 数据

java - 调用entityManager.flush - 调用两次、一次或根本不调用的效果

spring - 相当于非 Boot Spring 项目的 org.springframework.boot.context.embedded.FilterRegistrationBean?

java - 如何将 bcrypt 编码器引用到自定义身份验证提供程序?

java - 标准继承: join and search by child field

Java 8 流多线程

java - Android 在 getSupportFragmentManager() 处崩溃

java - 如何在Spring Data中进行Mongo聚合查询?

java - Thymeleaf + Spring Boot : Error resolving template