单击视图将字节数组写入pdf文件

时间:2015-04-02 13:37:05

标签: 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();
    }   

1 个答案:

答案 0 :(得分:1)

您可以将inputstream写入春季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();
    }
}