Jersey HTTP POST 方法损坏 non-text 个文件

Jersey HTTP POST method corrupting non-text files

我有一个 HTTP POST 方法,如果我上传文本文件,它可以正常工作。但是,如果我尝试上传 word 文档、pdf、zip、gzip 等...上传的文件会在此过程中损坏。我正在使用 Postman 发送请求。我做了一个 "POST" 方法,输入 url,添加 headers(尝试了各种 headers,它确实没有改变任何东西,所以现在我没有输入任何内容),然后在 body I select "formdata" 和 select 文件上。我真的只需要解决这个问题就可以支持以 .csv.gz 和 .csv 结尾的文件。目前,csv 没问题,但 .csv.gz 是损坏的类型。我也尝试了其他 non-text 文件,只是为了看看会发生什么,它们也损坏了。我无法弄清楚是否有某些编码、过滤器等...导致我可以删除或需要应用某些设置的情况发生。或者,如果有其他方法可以用 jersey 处理这个问题,那么 non-text 文件与原始文件保持一致。

我的应用程序是 运行 Spring v1.5.3 和 Jersey 2.25。

    @Override
public Response uploadTopicFile(String topic, FormDataMultiPart formDataMultipart) throws Exception {
    List<BodyPart> bodyParts = formDataMultipart.getBodyParts();

    // Getting the body of the request (should be a file)
    for (BodyPart bodyPart : bodyParts) {
        String fileName = bodyPart.getContentDisposition().getFileName();
        InputStream fileInputStream = bodyPart.getEntityAs(InputStream.class);
        String uploadedFileLocation = env.getProperty("temp.upload.path") + File.separator + fileName;
        this.saveFile(fileInputStream, uploadedFileLocation);
        String output = "File uploaded to : " + uploadedFileLocation;
        log.debug(output);
    }
    return Response.status(201).build();
}

    private void saveFile(InputStream uploadedInputStream, String serverLocation) {
    try {

        // Create the output directory
        Files.createDirectories(Paths.get(serverLocation).getParent());

        // Get the output stream
        OutputStream outputStream = new FileOutputStream(new File(serverLocation));
        int read = 0;
        byte[] bytes = new byte[1024];

        // Loop through the stream
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            // Output to file
            outputStream.write(bytes, 0, read);
        }

        // Flush and close
        outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return;
}

有一个过滤器导致损坏。过滤器已更新,问题已解决。