泽西岛HTTP POST方法损坏非文本文件



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

我的应用程序正在运行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;
}

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

最新更新