angularjs – Spring-boot:不存在所需的请求部分’file’
发布时间:2020-12-17 17:01:10  所属栏目:安全  来源:网络整理 
            导读:我收到错误当我想上传图片时,所需的请求部分’文件’不存在. 我打算上传视频和图片.视频上传效果很好,但图片不起作用. 这是我的代码. 春季启动 @ApiOperation(value = "Post new file") @RequestMapping(value = "/",method = RequestMethod.POST) public St
                
                
                
            | 
 我收到错误当我想上传图片时,所需的请求部分’文件’不存在. 
  我打算上传视频和图片.视频上传效果很好,但图片不起作用. 这是我的代码. 春季启动 @ApiOperation(value = "Post new file")
  @RequestMapping(value = "/",method = RequestMethod.POST)
  public String handleFileUpload(@RequestParam("file") MultipartFile file,@RequestParam (value = "accesstoken",required = true) String accessToken,RedirectAttributes redirectAttributes) throws Exception{
      tokenService.verifyAdmin(accessToken);
      storageService.store(file);
      return "You successfully uploaded " + file.getOriginalFilename() + "!";
  }StorageService.store @Override
  public void store(MultipartFile file) {
      try {
          if (file.isEmpty()) {
            throw new StorageException("Failed to store empty file " + file.getOriginalFilename());
           }
           Files.copy(file.getInputStream(),this.rootLocation.resolve(file.getOriginalFilename()));
      } catch (IOException e) {
  throw new StorageException("Failed to store file " + file.getOriginalFilename(),e);
     }
  }Html – 图片上传 <div class="control-group">
    <label class="control-label" for="date01">Cover Image</label>
    <div class="controls">
        <button class="btn-primary" name="file" ngf-pattern="'image/*'" ngf-accept="'image/*'" ngf-max-size="2MB" ngf-select="ctrl.uploadCoverImageFile($file)">Upload</button>
    </div>
</div>Html-视频上传 <div class="control-group">
    <label class="control-label" for="date01">Upload video</label>
    <div class="controls">
        <button class="btn-primary" ngf-select="ctrl.uploadVideoFile($file)">Upload</button>
    </div>
</div>调节器 self.uploadCoverImageFile = function(file){
     self.upload(file,-1);
};
self.uploadVideoFile = function(file){
    self.upload(file,1);
};
self.upload = function (file,x) {
    fileUploadService.uploadFile(file).then(function (resp) {
        console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
    },function (resp) {
        console.log('Error status: ' + resp);
    },function (evt) {
        if(x > 0)
            self.videoUploadProgress = parseInt(100.0 * evt.loaded / evt.total);
        else
            self.coverimageUploadProgress = parseInt(100.0 * evt.loaded / evt.total);
    });
};FileuploadService.uploadFile //ng-file-upload: [https://github.com/danialfarid/ng-file-upload]
uploadFile: function(file){
        return Upload.upload({
            url: baseEndPoint + '/',data: {file: file,accesstoken: userService.accesstoken}
        });
    },config.xml中 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="500000000000"/>
    </bean>
</beans>AppConfig.java @ImportResource("classpath:/vod/config/config.xml")
@Configuration
public class AppConfig {
}解决方法
 想要启用multipart:我遇到了同样的问题,我只是将以下属性添加到.properties文件中: 
  
  
  multipart.enabled=true (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
