使用Feign上传文件-multipart/form-data [英] File Upload Using Feign - multipart/form-data

查看:3843
本文介绍了使用Feign上传文件-multipart/form-data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用伪装完成多部分文件上传,但是我似乎在任何地方都找不到很好的例子.我本质上希望HTTP请求类似于以下内容:

I'm trying to accomplish a multipart file upload using feign, but I can't seem to find a good example of it anywhere. I essentially want the HTTP request to turn out similar to this:

...
Content-Type: multipart/form-data; boundary=AaB03x

--AaB03x
Content-Disposition: form-data; name="name"

Larry
--AaB03x
   Content-Disposition: form-data; name="file"; filename="file1.txt"
   Content-Type: text/plain

... contents of file1.txt ...
--AaB03x--

甚至...

------fGsKo01aQ1qXn2C
Content-Disposition: form-data; name="file"; filename="file.doc"
Content-Type: application/octet-stream

... binary data ...

------fGsKo01aQ1qXn2C--

我是否需要手动构建请求主体,包括生成多部分边界?考虑到该客户可以做的其他事情,这似乎有点过分.

Do I need to manually build the request body, including generating the multipart boundaries? That seems a bit excessive considering everything else this client can do.

推荐答案

不,您不需要.您只需要定义一种代理接口方法,将content-type指定为:multipart/form-data和其他信息,例如远程API所需的参数.这是一个示例:

No, you don't. You just need to define a kind of proxy interface method, specify the content-type as: multipart/form-data and other info such as parameters required by the remote API. Here is an example:

public interface FileUploadResource {

    @RequestLine("POST /upload")
    @Headers("Content-Type: multipart/form-data")
    Response uploadFile(@Param("name") String name, @Param("file") File file);

} 

完整的示例可以在这里找到:打开文件上传假装

The completed example can be found here: File Uploading with Open Feign

这篇关于使用Feign上传文件-multipart/form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆