2016-09-20 1465 views
0

我有一个应用程序,当前允许用户上传大量数据到Web服务。问题是这些文件需要一些时间才能通过网络上传,所以我想让用户先压缩文件然后上传。在java中解压Spring MultipartFile

@RequestMapping(value = "upload", method = RequestMethod.POST) 
@ResponseBody 
public ResponseEntity<?> uploadObjects(HttpServletRequest request, 
             @RequestParam("file") MultipartFile file) { 
    //Do stuff with it 
} 

我目前可以解压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; 
} 

有没有办法将MultipartFile解压缩回MultipartFile?或者将文件转换成MultipartFile?

+0

[转换文件到MultiPartFile]的可能的复制(http://stackoverflow.com/questions/16648549/converting-file-到multipartfile) – MGorgon

回答

0

尝试org.springframework.mock.web.MockMultipartFile, 它打算在测试中使用,因此它可以在本地创建。 有一个构造函数

public MockMultipartFile(String name, InputStream contentStream) 

这可能满足您的需求....