java - 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.我使用上面的字符串来解析请求正文(即文件内容存储在字符串中)这是将文件内容存储在字符串中的正确方法吗?
解决方案
您实际上需要MultipartFile它将为您提供与上传文件相关的所有信息。
uploadfile(@RequestParam("file") MultipartFile file){
String fileName = file.getOriginalFilename()
}
推荐阅读
- java - 引起:java.lang.RuntimeException: Driver com.mysql.cj.jdbc.Driver 声称不接受 jdbcUrl
- go - 理解 Golang 并发代码的问题
- postgresql - 错误:没有找到与 Oid 2840 的 toast 关系的有效索引
- django - 当我输入 aws-acess-id 和 aws-secret-key /:“错误:NotAuthorizedError - 操作被拒绝。请求中包含的安全令牌无效:”
- python - app_context 的烧瓶变量
- azure - 从 yaml 管道中的变量组参数化 azureSubscription
- javascript - 如何在 HTML 视频播放器中根据时间更改视频源链接?
- node.js - Nodejs Xml 到 Json
- sql - SQL 循环遍历字符串,查找 ID,并从中创建视图
- laravel - 在 Laravel nova 中自定义 Partition Metrics 的值