所以我有一个 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是上传到服务器!我究竟做错了什么?我查过谷歌,但看起来我几乎什么都做了。
1 回答
弑天下
TA贡献1818条经验 获得超7个赞
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());
}
}
添加回答
举报
0/150
提交
取消