Angularjs 不带RequestBody的多部分抛出415

Angularjs 不带RequestBody的多部分抛出415,angularjs,spring,spring-boot,multipart,Angularjs,Spring,Spring Boot,Multipart,我已经创建了简单的rest方法,在SpringBoot中通过multipart上传和添加文件。 我没有任何@RequestBody,所以我很惊讶为什么浏览器抛出415->“不受支持的媒体类型”内容类型“null”不受支持 控制器看起来像: @RestController @RequestMapping(value = "/api/file") public class FileController { @Autowired private FileServiceImpl fil

我已经创建了简单的rest方法,在SpringBoot中通过multipart上传和添加文件。 我没有任何
@RequestBody
,所以我很惊讶为什么浏览器抛出
415->“不受支持的媒体类型”内容类型“null”不受支持
控制器看起来像:

@RestController
@RequestMapping(value = "/api/file")
public class FileController {

    @Autowired
    private FileServiceImpl fileService;
    @Autowired
    private UserRepository userRepository;

    @PreAuthorize("hasAnyAuthority('CLIENT')")
    @RequestMapping(value = "", method = RequestMethod.POST, headers = "content-type=multipart/*", produces = "application/json", consumes = MediaType.APPLICATION_JSON_VALUE)
    public File uploadFile(@RequestParam("uploadedFile") MultipartFile file, HttpServletRequest httpRequest) {
        Principal name = httpRequest.getUserPrincipal();
        if (name.getName() == null) {
            throw new RuntimeException("Brak sesji");
        }
        User userByLogin = userRepository.findUserByLogin(name.getName());
        File f = null;
        if (!file.isEmpty()) {
            try {
                f = new File(file.getOriginalFilename(), file.getBytes(), file.getName(), file.getContentType(), new Date(), userByLogin);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (f != null) {
            fileService.uploadFile(f);
        }
        return f;
    }
}
{"timestamp":1491988354597,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'null' not supported","path":"/api/file"}
前端看起来像

<div>
<label>Dodaj załącznik</label>
<input type="file" files-model="file" >
<button ng-click="addFile()">Dodaj</button>
</div>

 $scope.addFile = function () {
          FileService.save($scope.file);
 }

您必须将文件附加到formData中

<div>
<label></label>
<input type="file" files-model="file" id="file" >
<button ng-click="addFile()">Dodaj</button>
</div>

$scope.addFile = function () {
      var file = document.getElementById("file").files[0];
      var formData = new FormData();
      formData.append("file", file);
      FileService.save(formData);
 };

多达伊
$scope.addFile=函数(){
var file=document.getElementById(“文件”).files[0];
var formData=new formData();
formData.append(“文件”,file);
FileService.save(formData);
};
在FileService服务中,您必须设置标题:{Content Type:undefined}


这应该管用

尝试删除consumes=MediaType.APPLICATION\u JSON\u值。您正在上载文件,但映射需要JSON。尝试将多部分/表单数据用作consumes@StanislavL同样的错误仍然添加了
angular.module('sbAdminApp').factory('FileService',function($resource){var service=$resource('api/file',{id:'@id'},{post:{method:'post',headers:{'content-Type':undefined}});回程服务;})和您调用的服务方法“FileService.save”仍然存在相同的故障,但它应该是“FileService.post”,因为您的服务中有“post”方法。但是$resource共享一个“save”方法来保存对象,那么,如何向该方法添加标题,而不是创建完全相同的新方法呢?但您必须使用您在服务中创建的方法$资源提供了“$save”,我认为它不包含头设置。好的,重新定义了一个新方法,但现在有“`”必需的请求部分“uploadedFile”不存在“`->但在我的java控制器方法中,我有
@RequestParam(“uploadedFile”)多部分文件uploadedFile
,在HTML中也有该名称