避免在Spring Rest文件下载调用中使用CoyoteOutputStream对象签名

时间:2019-04-30 08:33:58

标签: java rest pdf spring-restcontroller

我拥有一个带有终点的模拟项目maven spring-rest

@RequestMapping(value = "/rest/{nid}/{fileName:.+}", method = RequestMethod.GET, produces = MediaType.APPLICATION_PDF_VALUE)
    public String getPjSae(@PathVariable String nid, @PathVariable String fileName, HttpServletResponse response) throws IOException {
        LOGGER.info("NID : " + nid);
        LOGGER.info("NOM FICHIER  : " + fileName);
        File file = new File(saePath+File.separatorChar + fileName);
        LOGGER.info("CHEMIN PJ  : " + file);
        if (file.exists()) {
            InputStream inputStream = new FileInputStream(file); //load the file
            // here I use Commons IO API to copy this file to the response output stream, I don't know which API you use.
            IOUtils.copy(inputStream, response.getOutputStream());
            // here we define the content of this file to tell the browser how to handle it
            response.setContentType("application/pdf");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".pdf");
            response.flushBuffer();
        }
        return response.getOutputStream().toString();
    }

我获得了下载的文件,并将其转换为base64以将其写入XML文件。我稍后必须进行比较,问题是在基数为64的字节流中写入对象的签名:CoyoteOutputStream

在pdf的每个base64的结尾,我都有一块:

CnN0YXJ0eHJlZgo0NjkyOAolJUVPRgpvcmcuYXBhY2hlLmNhdGFsaW5hLmNvbm5lY3Rvci5Db3lvdGVPdXRwdXRTdHJlYW1ANDA4YTViYzI=

每次都不同,原因是:

startxref
46928
%% EOF
org.apache.catalina.connector.CoyoteOutputStream@408a5bc2

这个坑,但是比较起来是因为: @ 408a5bc2 是唯一的

1 个答案:

答案 0 :(得分:1)

您最终将其返回:

return response.getOutputStream().toString();

这将返回相关输出流的默认toString输出,即您的 CoyoteOutputStream对象签名。如果您要避免,请不要将其退回。