作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有一个 Java API,我使用 cURL 命令将 GET/POST 请求发送到我的 API。我在 Java API 中使用 Springboot。我的 cURL 命令如下所示:
curl -X POST \
https://localhost:8443/abc/code/v \
-H 'Content-Type: multipart/form-data' \
-F file=@/path/to/file.txt
接收此请求的 Java Springboot 方法如下所示:
@RequestMapping(method = RequestMethod.POST, value = "/{code}/{v}")
public ResponseEntity<Object> uploadTemplate( //
@ApiParam(value = "code desc") //
@PathVariable final String code, //
@ApiParam(value = "v desc") //
@PathVariable final String v, //
@RequestParam("file") MultipartFile file
) throws IOException {
//log.info("received [url: /tabc/" + code + "/" + v + ", method: POST");
System.out.println("file: "+ file.getName());
}
打印出“file: file”
看起来file.txt
根本没有上传到服务器!我究竟做错了什么?我在 Google 上查过,但看起来我几乎做了所有事情。
最佳答案
MultipartFile.getName
返回多部分形式的参数名称。
要获取MultipartFile
的字节,请使用 file.getBytes()
如果您的 MultipartFile
是一个文本文件,您可以使用此实用程序方法来检索该文本文件中的行列表:
public List<String> read(MultipartFile file) throws IOException {
InputStreamReader in = new InputStreamReader(file.getInputStream());
try (BufferedReader reader = new BufferedReader(in)) {
return reader.lines().collect(Collectors.toList());
}
}
关于java - cURL命令上传文件不上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56176734/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!