Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.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 如何接收请求正文数据以及多部分图像文件?_Java_Spring Boot_Multipartform Data - Fatal编程技术网

Java 如何接收请求正文数据以及多部分图像文件?

Java 如何接收请求正文数据以及多部分图像文件?,java,spring-boot,multipartform-data,Java,Spring Boot,Multipartform Data,我想接收包含请求正文数据的多部分图像文件,但无法找到它,为什么它会抛出org.springframework.web.HttpMediaTypeNotSupportedException:Content type“application/octet stream”不支持异常 下面是我的实现 public ResponseEntity<GlobalResponse> createUser(@Valid @RequestPart("json") UserDTO userDTO ,@Req

我想接收包含请求正文数据的多部分图像文件,但无法找到它,为什么它会抛出
org.springframework.web.HttpMediaTypeNotSupportedException:Content type“application/octet stream”
不支持异常

下面是我的实现

public ResponseEntity<GlobalResponse> createUser(@Valid @RequestPart("json") UserDTO userDTO ,@RequestPart(value ="file", required=false)MultipartFile file) throws IOException {

      //Calling some service

      return new ResponseEntity<>( HttpStatus.OK);
}
public ResponseEntity createUser(@Valid@RequestPart(“json”)UserDTO UserDTO,@RequestPart(value=“file”,required=false)MultipartFile文件)引发IOException{
//打电话找服务
返回新的响应状态(HttpStatus.OK);
}
编辑: 这是我的邮递员配置


根据您的异常:
org.springframework.web.HttpMediaTypeNotSupportedException:Content type“application/octet stream”
该方法不需要多部分数据,因此

@RequestMapping
配置中指定使用
多部分数据的请求:

@Postmapping("/api/v1/user", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) 
public ResponseEntity<GlobalResponse> createUser(@Valid @RequestPart("json") UserDTO userDTO ,@RequestPart(value ="file", required=false)MultipartFile file) throws IOException {

      //Calling some service

      return new ResponseEntity<>( HttpStatus.OK);
}
@Postmapping(“/api/v1/user”,consumes=MediaType.MULTIPART\u FORM\u DATA\u VALUE)
public ResponseEntity createUser(@Valid@RequestPart(“json”)UserDTO UserDTO,@RequestPart(value=“file”,required=false)MultipartFile文件)引发IOException{
//打电话找服务
返回新的响应状态(HttpStatus.OK);
}

因为您在表单数据中发送数据,表单数据可以以键值对的形式发送数据。不在
RequestBody
中,因此需要修改端点,如下所示:

@PostMapping(value = "/createUser")
public ResponseEntity createUser(@RequestParam("json") String json, @RequestParam("file") MultipartFile file) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();
    UserDTO userDTO = objectMapper.readValue(json, UserDTO.class);
    // Do something
    return new ResponseEntity<>(HttpStatus.OK);
}
@PostMapping(value=“/createUser”)
public ResponseEntity createUser(@RequestParam(“json”)字符串json,@RequestParam(“file”)多部分文件文件)引发IOException{
ObjectMapper ObjectMapper=新的ObjectMapper();
UserDTO UserDTO=objectMapper.readValue(json,UserDTO.class);
//做点什么
返回新的响应状态(HttpStatus.OK);
}

您需要在
String
表示中接收
UserDTO
对象,然后使用
ObjectMapper
将其映射到
UserDTO
。这将允许您使用表单数据接收
MultipartFile
UserDTO

不工作是什么意思?它会抛出错误吗?请详细说明并向我们展示您的客户端实现please@Mustahsan它正在抛出org.springframework.web.HttpMediaTypeNotSupportedException:内容类型“application/octet stream”不受支持例外您在该方法上面使用的u共享请求映射配置?@ValentinRiabukhin,我正在用PostmanCan测试API。您可以告诉我,ObjectMapper类的包名是什么,因为有多个ObjectMapperclass@camelCode
com.fasterxml.jackson.databind
@camelCode我很高兴知道这一点。你知道其他解决方案吗,实际上,我想用@Requestbody和图像发送数据file@camelCode使用
@RequestBody
您无法发送
多部分文件