1

I have a application that current allows a user to upload large sets of data to a web service. 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.

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? Or convert a File into a MultipartFile?

Lazyeye79
  • 11
  • 1
  • 3
  • Possible duplicate of [Converting File to MultiPartFile](http://stackoverflow.com/questions/16648549/converting-file-to-multipartfile) – MGorgon Sep 20 '16 at 16:56

1 Answers1

0

try org.springframework.mock.web.MockMultipartFile, it is intended to be used in tests, so it can be created locally. There is a constructor

public MockMultipartFile(String name, InputStream contentStream)

which could fit your needs....