首页 > 解决方案 > java Spring boot在通过curl发送post请求时获取控制器中的文件名

问题描述

我正在尝试将文件从客户端上传到服务器客户端使用 curl 命令将文件上传到服务器

客户端命令:

curl -X POST -T pom.xml http://localhost:8070/put --header "origmd5":"7AB4E6F0A4A2D3CBB200DB1677D99AD75"

现在在控制器中,即在服务器端,代码如下

服务器端:

@PostMapping(value="readFile")
public ResponseEntity<?> uploadfile(@RequestBody String filecontent) throws IllegalStateException, IOException {
System.out.println(filecontent);//prints the content which is inside the file uploaded by client
  return null;
  }

1.现在的问题是我们如何获取客户端已经发送的文件名文件的内容可以在请求体中解析但是如何获取文件名?

 @RequestBody String filecontent

2.我使用上面的字符串来解析请求正文(即文件内容存储在字符串中)这是将文件内容存储在字符串中的正确方法吗?

标签: javaspringweb-servicesspring-mvcspring-boot

解决方案


您实际上需要MultipartFile它将为您提供与上传文件相关的所有信息。

uploadfile(@RequestParam("file") MultipartFile file){
String fileName = file.getOriginalFilename()
}

推荐阅读