简体   繁体   English

在Java中解压缩spring MultipartFile

[英]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.

相关问题 Java spring:将文件转换为 MultipartFile 的最佳方法 - Java spring: best way to convert a File to a MultipartFile 使用jQuery将图像发布到Spring Java作为MultipartFile - Post image with jQuery to Spring java as MultipartFile Spring MVC Java 中不存在所需的 MultipartFile 参数“文件” - Required MultipartFile parameter 'file' is not present in Spring MVC Java 必需的MultipartFile参数&#39;file&#39;不存在Java Spring MVC - Required MultipartFile parameter 'file' is not present java spring mvc 在 Java 8 中逐行读取 Spring Multipartfile 的最佳方法 - Best way to read Spring Multipartfile line by line in Java 8 使用Spring 4 restTemplate(Java Client和RestController)上传MultipartFile列表 - Uploading a List of MultipartFile with Spring 4 restTemplate (Java Client & RestController) Java Spring boot 2.0.5 MultipartFile上传“内容类型不受支持” - Java Spring boot 2.0.5 MultipartFile upload “Content type not supported” java - 如何使用spring boot在java中将multipartfile参数设置为“不需要”? - How to set a multipartfile parameter as "not required" in java with spring boot? Spring multipartFile PUT请求 - Spring multipartFile PUT request 使用spring将File转换为MultiPartFile - Converting File to MultiPartFile with spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM