Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 角度5+;Spring启动映像上载_Java_Typescript_Spring Boot_Angular5 - Fatal编程技术网

Java 角度5+;Spring启动映像上载

Java 角度5+;Spring启动映像上载,java,typescript,spring-boot,angular5,Java,Typescript,Spring Boot,Angular5,我正在尝试上传一个从angular到spring boot rest的图像。但它会抛出 org.springframework.web.multipart.MultipartException: Current request is not a multipart request at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(Reques

我正在尝试上传一个从angular到spring boot rest的图像。但它会抛出

org.springframework.web.multipart.MultipartException: Current request is not a multipart request
    at org.springframework.web.method.annotation.RequestParamMethodArgumentResolver.handleMissingValue(RequestParamMethodArgumentResolver.java:190) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
我对这两种技术都是新手。帮我上传文件

这是代码

RestController

public ResponseMessage addUser(@RequestParam("profileImage") MultipartFile profileImage,
    @RequestParam("user") User user) {

try {
    if (userService.doesUserExist(user.getUsername())) {
    return new ResponseMessage("The username is not available; please choose a different username", 200);
    }
} catch (Exception e) {
    System.out.println(e.getMessage());
}
String extension = FilenameUtils.getExtension(profileImage.getOriginalFilename());
storageService.storeFile(profileImage, user.getUsername() + "." + extension, profilePicturePath);
user.setProfilePicturePath(profilePicturePath + "/" + user.getUsername() + "." + extension);
userService.addUser(user);

return new ResponseMessage(user.getUsername() + " was added successfully!", 200);
}
角度服务

addUser(user:User,profileImage:File){
      const formData:FormData = new FormData();

     formData.append('profileImage', profileImage);
     formData.append('user',JSON.parse(JSON.stringify(user)));

     return this.http.post<ResponseMessage>("/api/admin/user/new_user",formData,this.postHttpOptions);

    }
<input type='file' id="imgInp" (change)="setImage($event.target.files)" />
角度模板

addUser(user:User,profileImage:File){
      const formData:FormData = new FormData();

     formData.append('profileImage', profileImage);
     formData.append('user',JSON.parse(JSON.stringify(user)));

     return this.http.post<ResponseMessage>("/api/admin/user/new_user",formData,this.postHttpOptions);

    }
<input type='file' id="imgInp" (change)="setImage($event.target.files)" />

我明白了

我所做的改变

  • 我添加了@Requestpart而不是@RequestParam
  • 我添加了@RequestPart字符串用户,而不是@RequestParam用户用户;然后使用ObjectMapper将字符串转换为用户
  • 在客户端,我删除了'ContentType=application/Json',没有提到任何内容类型
  • 这是密码

    public String addUser(@RequestPart("profileImage") MultipartFile profileImage,
            @RequestPart("user") String userString) throws JsonParseException, JsonMappingException, IOException {
    
        User user = new ObjectMapper().readValue(userString, User.class);
    }
    

    plz共享完整的堆栈跟踪,以及从angular请求的
    curl