在 spring 剩余文件下载调用中避免 CoyoteOutputStream 对象签名

Avoid CoyoteOutputStream object signature in spring rest file download call

我拥有一个mock项目mavenspring-rest with the end point

@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 文件。后面还要对比,问题是在base 64的字节流中写入对象的签名:CoyoteOutputStream

在pdf的每个base64结尾我有一段:

CnN0YXJ0eHJlZgo0NjkyOAolJUVPRgpvcmcuYXBhY2hlLmNhdGFsaW5hLmNvbm5lY3Rvci5Db3lvdGVPdXRwdXRTdHJlYW1ANDA4YTViYzI=

每次都不一样,因为:

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

所以,这个坑不过比较因为:@408a5bc2是独一无二的

你最终return这个:

return response.getOutputStream().toString();

这 return 是相关输出流的默认 toString 输出,即您的 CoyoteOutputStream 对象签名。如果你想避免这个,就不要return它。