MultipartHttpServletRequest-getFiles不返回文件

时间:2018-06-22 14:32:36

标签: angularjs spring-mvc http-post multipart

我正在尝试使用angularjs ad spring mvc将附件和一些信息从前端发送到服务器。 使用以下代码,当尝试在spring控制器中获取文件详细信息时,文件大小始终为零(req.getFiles(“ file”))。 我是否需要修改myService.js中的任何标头信息或在spring控制器中包括任何标头信息以获取附件以及其他信息(名称,sinNumber,状态,消息)?使用以下代码,问题仅在于它不返回文件信息,而是返回作为json传递的信息。

下面是示例代码:

html:

//html elements
<label style="color: #0099ff;">Attachment: </label>
         <input type = "file"  name="file" file-model="file" multiple/>
 //elements for name,sinNumber,status,message

js:

myApp.controller('passJSONTestController', function ($rootScope, $scope, MyService) {
     $scope.submitdata=function(){
        $scope.myJSONData = [
            {   'name':$scope.name,
                'sinNumber': $scope.sinNo,
                'status': $scope.status,
                'message':$scope.message,
            }
        ];
        var fd=new FormData();
        angular.forEach($scope.files,function(file){
             fd.append('file',file);
        });

        fd.append('json', JSON.stringify($scope.myJSONData);
        MyService.sendJSON(fd).then(
                function (response) {
                  },
                function (errResponse) {
     //error
                }
        );
    //service js
    myService.sendJSON = function (fd) {
             var deferred = $q.defer();
         var myUrl = myApplnURL + '/dataStack/getAllDataInfo.form';
         $http.post(repUrl, formData, {
                   transformRequest: function(data, headersGetterFunction) {
                       return data;
                   },
                   headers: {
                       'Content-Type': undefined,
                       'Content-Transfer-Encoding': 'utf-8'
                   }}).then(function (response) {
               console.log("response " + response);
             }, function (errResponse) {
                 console.error('Error in request' + errResponse);
                 deferred.reject(errResponse);
        });

弹簧控制器:

@Controller
@RequestMapping("/dataStack")
public class sendInfoController {
@RequestMapping(value = "/getAllDataInfo", method = RequestMethod.POST)
    @ResponseBody
    public String sendDataInfoTest(MultipartHttpServletRequest req){
        System.out.println("In spring controller");
        List<MultipartFile> multiPartFileList = req.getFiles("file"); //size is zero
        System.out.println("multiPartFileList size " + multiPartFileList.size());//size is zero
        //to read JSON, with below code i can able to read the JSON . Issue is with getFiles() which is not returning the files and always length is zero.
         MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
            // Jackson deserialization...but you could use any...Gson, etc
            ObjectMapper mapper = new ObjectMapper();
            String[] json = (String[]) req.getParameterMap().get("json");
          ....
          }
          }

spring-servlet.xml

....
   <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000"/>
    </bean>
....

PS:我需要从前端向服务器传递文件(根据用户选择是单个文件还是多个文件,或者没有文件)以及一些信息,例如名称,sinNumber,状态,消息。

0 个答案:

没有答案