[英]Unzip a spring MultipartFile in java
I have a application that current allows a user to upload large sets of data to a web service. 我有一个应用程序,该应用程序当前允许用户将大量数据上传到Web服务。 The problem is that these files take some time to upload over the network and so I would like to allow the user to zip the file first and then upload it.
问题是这些文件需要花费一些时间才能通过网络上传,因此我想允许用户先压缩文件然后再上传。
@RequestMapping(value = "upload", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<?> uploadObjects(HttpServletRequest request,
@RequestParam("file") MultipartFile file) {
//Do stuff with it
}
I can currently unzip the MultipartFile into a Java IO file, but all the existing logic only works with a MultipartFile and would require some (potentially a lot of) reworking. 我目前可以将MultipartFile解压缩到Java IO文件中,但是所有现有逻辑仅适用于MultipartFile,并且需要进行一些(可能很多)重做。
private File unzip(MultipartFile file) throws IOException {
byte[] buffer = new byte[1024];
int bufferSize = 1024;
File tempFile = null;
ZipInputStream zis = new ZipInputStream(file.getInputStream());
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
tempFile = File.createTempFile(entry.getName(), "tmp");
tempFile.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempFile);
BufferedOutputStream bos = new BufferedOutputStream(fos, bufferSize);
int count;
while ((count = zis.read(buffer, 0, bufferSize)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
bos.close();
}
zis.close();
return tempFile;
}
Is there a way to unzip a MultipartFile back into a MultipartFile? 有没有一种方法可以将MultipartFile解压缩回MultipartFile? Or convert a File into a MultipartFile?
还是将文件转换成MultipartFile?
try org.springframework.mock.web.MockMultipartFile, it is intended to be used in tests, so it can be created locally. 尝试org.springframework.mock.web.MockMultipartFile,它打算在测试中使用,因此可以在本地创建。 There is a constructor
有一个构造函数
public MockMultipartFile(String name, InputStream contentStream)
which could fit your needs.... 可以满足您的需求...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.