1

I need to handle multipart/form-data request that contains a file to be uploaded to server. In case if the client disconnects before he could completely upload the file, server should save the partially uploaded file.

I am using Spring MVC Controller to handle this:

public class FileUploadController {

       @RequestMapping("/upload")
       public String handleUploadRequest(MultipartHttpServletRequest request) {
         MultipartFile file = request.getFile("File");
         InputStream stream = file.getInputStream();
         // read data from stream
  }
}

Have extended CommonsMultiPartResover to override

public class FileUploadMultipartResolver extends CommonsMultipartResolver {

@Autowired
ServletContext servletContext;

@Override
public void cleanupMultipart(MultipartHttpServletRequest request) {
    File tempDir = (File) servletContext.getAttribute(ServletContext.TEMPDIR);
    File[] files = tempDir.listFiles();
    for(int i=0; i< files.length; i++) {
        LOGGER.debug("filename: " + files[i].getName() + ", size: " + files[i].length());
    }
}    }

multi part properties are set to resolve lazily, maximum in memory size:

@Configuration
public class RcseFtcsConfiguration {

@Bean(name="multipartResolver")
public MultipartResolver getMultipartResolver() throws IOException {
    FileUploadMultipartResolver resolver = new FileUploadMultipartResolver();
    resolver.setResolveLazily(true);
    resolver.setMaxInMemorySize(1024);
    return resolver;
} }

However, I am unable to get a partially uploaded file when the client terminates before full upload.

I thought that MultiPartResolver should be writing the bytes to servlet container temporary directory for every MaxInMemorySize bytes but It is not happening.. I could see data in servlet container temporary directory only if the complete request data arrives at server.

I want to know if i am missing anything.

Siva Atta
  • 81
  • 7

1 Answers1

2

MultiPart upload and resumable uploads are quite different. How do you expect to resume uploads on the client side with this method?

There are several ways to solve this, one of them is to use the HTML5 File API to slice the file on the client side and upload it slice by slice. Once all slices are uploaded, you can assemble them on the server.

Libraries like resumable.js have their own "custom protocol" over HTTP to track upload progress, while others choose to use Content-Range requests to do this.

Now to answer your question, AFAIK there's no support for Content-Range uploads in Spring Framework right know; but you can achieve resumable uploads on the server side with one of those solutions.

Community
  • 1
  • 1
Brian Clozel
  • 56,583
  • 15
  • 167
  • 176