2016-03-07 93 views
1

我試圖打春RestController剛開:角春天文件上傳

osweb.servlet.PageNotFound:請求方法「POST」不支持

我相信的東西是缺少映射控制器。

<div class="col-sm-1" style="background-color:#cccccc;" align="center"><span class="file-input btn btn-primary btn-file">Import file<input type="file" onchange="angular.element(this).scope().uploadScriptFile(this.files)"></input></span></div> 


    $scope.uploadCtrFile = function(files) { 
     console.log(">>>>>>>>>>>>>>>>uploadCtrFile"); 
     var fd = new FormData(); 
     //Take the first selected file 
     fd.append("file", files[0]); 
     console.log(">>>>>>>>>>>>>>>>uploadCtrFile angular.toJson: " 
       + angular.toJson(fd, 2)); 

     $http.post('/rest/uploadCtrFile/', fd,{ 
      withCredentials: true, 
      headers: {'Content-Type': undefined }, 
      transformRequest: angular.identity 
     }).success(function(fd, status, headers, config) { 
      $scope.success = ">>>>>>>>>>>>>>>>uploadCtrFile Success: "+JSON.stringify({data: fd}); 
      console.log($scope.success); 
     }) 
     .error(function(fd, status, headers, config) { 
      $scope.success = ("failure message: " + JSON.stringify({data: fd})); 
      console.log($scope.success); 
     }); 

    }; 

控制器看起來像......

  @RequestMapping(value = "/uploadCtrFile/", headers = "'Content-Type': 'multipart/form-data'", method = RequestMethod.POST) 
      @ResponseBody 
      public void uploadCtrFile(MultipartHttpServletRequest request, HttpServletResponse response) { 

       Iterator<String> itr=request.getFileNames(); 

       MultipartFile file=request.getFile(itr.next()); 

       String fileName=file.getOriginalFilename(); 
       log.debug(">>>>>>>>>>>>>>>>>>>submitted uploadCtrFile: "+fileName); 
     } 

前端顯示這些消息......

">>>>>>>>>>>>>>>>uploadCtrFile angular.toJson: {}" tl1gen.js:607:0 
"failure message: {"data":  {"timestamp":1457380766467,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/rest/uploadCtrFile/"}}" 

我缺少什麼?

回答

1

您發送undefinedContent-Type值,在這裏:

headers: {'Content-Type': undefined } 

但你的控制器需要Content-Typemultipart/form-data值:

​​

您應該發出正確的Content-Type頭在你的要求,如:

headers: {'Content-Type': 'multipart/form-data'} 

或從您的控制器定義中刪除headers選項:

@RequestMapping(value = "/uploadCtrFile/", method = RequestMethod.POST)