因此,我有一个Java API,并使用cURL命令向我的API发送GET/POST请求。我在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
根本没有上传到服务器上!我做错了什么?我已经在谷歌上查过了,但看起来我几乎什么都做了。
发布于 2019-05-17 05:49:48
MultipartFile.getName
以多部分形式返回参数的名称。
使用file.getBytes()
获取MultipartFile
的字节数
如果您的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());
}
}
https://stackoverflow.com/questions/56176734
复制相似问题