简体   繁体   English

Spring MultipartException:当前请求不是多部分请求

[英]Spring MultipartException: The current request is not a multipart request

I'm trying to upload a file with AngularJS and Spring controller.我正在尝试使用 AngularJS 和 Spring 控制器上传文件。 The Angular controller looks like this: Angular 控制器如下所示:

 $scope.uploadFile=function(){
 var formData=new FormData();
 formData.append("file",file.files[0]);

 $http.post('/content-files/upload /',  file.files[0], {

     transformRequest: function(data, headersGetterFunction) {
            return data; // do nothing! FormData is very good!
        },
     headers: {'Content-Type': undefined }

 })
 .success(function(){
     console.log('Post Succeded !');
 })
 .error(function(){
     console.log('Post Failed .');
 });
}

I also try this:我也试试这个:

var formData = new FormData();
formData.append('file',file.files[0]);

$http.post('/content-files/upload /',  formData, {         
  headers: { 'Content-Type': undefined },
  transformRequest: angular.identity
})

and the Spring controller looks like this: Spring 控制器如下所示:

    @RequestMapping(value = "/content-files/upload/", method =   RequestMethod.POST  )           
public @ResponseBody String handleFileUpload(  @RequestParam("file")      MultipartFile file) {
  System.out.println("BrandController.uploadMultipart()");

String name=file.getName();
if (!file.isEmpty()) {
    try {
        byte[] bytes = file.getBytes();
        BufferedOutputStream stream =
                      new BufferedOutputStream(new FileOutputStream(new File(name)));
        stream.write(bytes);
        stream.close();
        return "You successfully uploaded " + name + "!";
    } catch (Exception e) {
        return "You failed to upload " + name + " => " + e.getMessage();
    }
} else {
    return "You failed to upload " + name + " because the file was empty.";
 }
 }

My html Page is :我的 html 页面是:

 <form enctype="multipart/form-data">
   <input id="file-0a" class="file" type="file" file-model="myFile" name="myFile" />
                 <button ng-click="uploadFile()">upload me</button></form>

i have 2 jars in web-inf/lib:commons-upload.jar and commons-io.jar I add this in my spring configuration file:我在 web-inf/lib:commons-upload.jar 和 commons-io.jar 中有 2 个 jar 我将它添加到我的 spring 配置文件中:

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

When I'm trying to upload a file, I get the error:当我尝试上传文件时,出现错误:

   org.springframework.web.multipart.MultipartException: The current request is   not a multipart request

When I'm trying to upload a file using MultipartHttpServletRequest in my spring function instead of Multipart i get this error:当我尝试在我的 spring 函数而不是 Multipart 中使用 MultipartHttpServletRequest 上传文件时,我收到此错误:

    java.lang.IllegalStateException: Current request is not of type   [org.springframework.web.multipart.MultipartHttpServletRequest]: org.apache.catalina.connector.RequestFacade@196f5636

When i use HttpServletRequest request and i try to cast it i got a ClassCastException.当我使用 HttpServletRequest 请求并尝试投射它时,我得到了 ClassCastException。 Nothing change when i cancel enctype="multipart/form-data" from my form Tag当我从我的表单标签中取消 enctype="multipart/form-data" 时没有任何变化

需要做的两件事: 1. headers: {'Content-Type': 'application/x-www-form-urlencoded'} 2. 传递的数据应转换为 URL 编码的字符串参考: 在此处输入链接描述

I stuck into the same problem.我陷入了同样的问题。 Below is angular code下面是角度代码

things you should do:你应该做的事情:

  1. Remove the Content-Type from headers.从标题中删除 Content-Type。
  2. For multi file upload use below code对于多文件上传使用下面的代码
handleImageUpload = async (event) => {
    if (event.target.files && event.target.files) {
      const files = event.target.files;
      _.map(files, (file) => {
        //array for multiple files upload
        this.imageData.push(file);
        const reader = new FileReader();
        //base64 encoded image for preview
        reader.onload = async (event: any) => {
          this.album.push(event.target.result);
        };
        reader.readAsDataURL(file);
      });
      this.uploadedImage = Promise.resolve(this.album);
    }
  }

In you submit handler use this code在您提交处理程序时使用此代码

const formData: any = new FormData();
 // adding multiple files
    this.imageData.map((i) => {
      formData.append("files", i);
    });
    // add objects
    _.map(body,(value,key) => {
      formData.append(key, value);
    });
    // delete headers it will be set by browser
    headers = headers.delete("Content-Type");
    // make a call to api
    this.httpClient
      .post(url, formData, { headers })
      .subscribe((res) => console.log(res));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “当前请求不是多部分请求” angularjs和spring boot - “the current request is not a multipart request” angularjs and spring boot 当前请求不是angularJS和Spring Rest中的多部分请求 - The current request is not a multipart request in angularJS and Spring Rest 上传文件时出错:Spring 4和angular js中当前请求不是多部分请求 - Getting error while uploading file : The current request is not a multipart request in spring 4 and angular js Spring MVC中包含多部分数据的POST请求 - POST request with multipart data in spring mvc angularJs 上传文件错误:当前请求不是多部分请求 - angularJs upload file error: The current request is not a multipart request 向Spring MVC Controller发送多部分请求时出现400错误的请求 - 400 Bad Request When Send Multipart Request To Spring MVC Controller 当前请求的类型不是[org.springframework.web.multipart.MultipartHttpServletRequest] - Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest] 使用AngularJS的多部分请求 - Multipart request with AngularJS 请求被拒绝,因为没有找到angularjs和spring mvc的多部分边界 - the request was rejected because no multipart boundary was found with angularjs and spring mvc 使用AngularJS的Java Spring Boot多部分POST请求 - Java Spring Boot multipart POST request using AngularJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM