Search code examples
javaangularjsspring-mvcmultipart

Spring MultipartFiles duplicates file in mixed multipart


I have UI code like this

  .factory('Service', function ($http, $resource, BASE_PATH) {
function sendResponse (code, responses) {
  return $http({
    method: 'POST',
    url: "/test",
    headers: {'Content-Type': undefined},
    transformRequest: function (data) {
      var formData = new FormData();
      formData.append("dec", new Blob([angular.toJson(data.dec)], {type: 'application/json'}));
      angular.forEach(data.files, file => {
        formData.append("file", file);
      });
      return formData;
    },
    data: {dec: responses.dec, files: responses.files}
  });`enter code here`
}

and here is controller part

 @RequestMapping(value = "/test", method = POST)
public ResponseEntity<List<Test>> save(@RequestPart(value = "file", required = false) MultipartFile[] multipartFiles,
    @RequestPart(value = "dec") List<Decision> dec) {
    return save(multipartFiles, dec);
}

The problem is, MultipartFile has duplicated files. For example I attach one "test.txt" but, I see two "test.txt" files in controller while debugging.

Is there any way to solve this issue?


Solution

  • I was using zuul server in between as API gatway. I found there is a bug in old version of zuul which duplicated one part of multipart object.

    The solution can be referred here github.com/spring-cloud/spring-cloud-netflix/issues/1171