简体   繁体   English

resttemplate等效/映射curl -T选项

[英]resttemplate equivalent/mapping of curl -T option

What is the correct way to map curl -T flag if one were to use Spring resttemplate? 如果要使用Spring resttemplate,映射curl -T标志的正确方法是什么? From manpage for -T - "This transfers the specified local file to the remote URL" 从-T的联机帮助页-“这会将指定的本地文件传输到远程URL”

This is a related curl format:- 这是一个相关的curl格式:

curl -X PUT -T /tmp/sampleFileToUpload -v 'http://testHost/URI'

This is what I have - does not seem to be working though 这就是我所拥有的-虽然似乎没有工作

MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
map.add("file",new FileSystemResource(new File("/tmp/sampleFileToUpload")));

I have set content-type to multipart/form-data in the headers and then invoke exchange method. 我已经在标题中将content-type设置为multipart / form-data,然后调用了exchange方法。 The destination gets written to - but not with the expected contents - but rather with the request parameters. 目标被写入-但不写入预期的内容-而是写入请求参数。 From what I have read - curl -F option is straightforward to map to - example "file" as key in the map but I am not sure as to how to map -T while using resttemplate. 据我了解,curl -F选项很容易映射到-例如“ file”作为映射中的键,但是我不确定在使用resttemplate时如何映射-T。

This worked for me:- 这对我有用:

httpHeaders.add("content-type","multipart/form-data");
...
...
httpEntity = new HttpEntity<String>(fileContentsAsString, httpHeaders)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM