为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕田峪9158850 2022-11-30 16:54:41
所以我有一个 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());

        }

    }

GitHub 上的完整代码


查看完整回答
反对 回复 2022-11-30
  • 1 回答
  • 0 关注
  • 117 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信