上传文件的cURL命令不上传文件

所以我有一个 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是上传到服务器!我究竟做错了什么?我查过谷歌,但看起来我几乎什么都做了。


慕田峪9158850
浏览 123回答 1
1回答

弑天下

MultipartFile.getName返回多部分形式的参数名称。获取MultipartFile使用的字节file.getBytes()如果你MultipartFile是一个文本文件,你可以使用这个实用方法来检索该文本文件中的行列表:&nbsp;public List<String> read(MultipartFile file) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; InputStreamReader in = new InputStreamReader(file.getInputStream());&nbsp; &nbsp; &nbsp; &nbsp; try (BufferedReader reader = new BufferedReader(in)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return reader.lines().collect(Collectors.toList());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }GitHub 上的完整代码
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java