feign通过byte[]上传图片,不使用multipartFile

因速卖通接口上传图片接口

AliexpressPhotobankRedefiningUploadimageforsdkResponse.AeopUploadImageResponse uploadImageForSdk(AliexpressPhotobankRedefiningUploadimageforsdkRequest req, String storeCode) throws ApiException {}

 其中AliexpressPhotobankRedefiningUploadimageforsdkRequest

public class AliexpressPhotobankRedefiningUploadimageforsdkRequest ... {
    private String fileName;
    private String groupId;
    private FileItem imageBytes;
...
}

检查FileItem发现多种构造方案

   public FileItem(File file) {
        this.contract = new FileItem.LocalContract(file);
    }

    public FileItem(String filePath) {
        this(new File(filePath));
    }

    public FileItem(String fileName, byte[] content) {
        this(fileName, (byte[])content, (String)null);
    }

    public FileItem(String fileName, byte[] content, String mimeType) {
        this.contract = new FileItem.ByteArrayContract(fileName, content, mimeType);
    }

    public FileItem(String fileName, InputStream stream) {
        this(fileName, (InputStream)stream, (String)null);
    }

    public FileItem(String fileName, InputStream stream, String mimeType) {
        this.contract = new FileItem.StreamContract(fileName, stream, mimeType);
    }

由于feign好像不支持文件传输,并且百度出来的各种添加MutipartFile或对象支持的方案太麻烦.

我尝试把InputStream转为byte[],传递到aliClient后,

        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[100]; //buff用于存放循环读取的临时数据
        int rc = 0;
        while ((rc = inputStream.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        byte[] in_b = swapStream.toByteArray(); //in_b为转换之后的结果

再组装请求对象AliexpressPhotobankRedefiningUploadimageforsdkRequest

public AliexpressPhotobankRedefiningUploadimageforsdkResponse.AeopUploadImageResponse uploadImageForSdk(
@RequestParam("fileName") String fileName,
@RequestBody byte[] in_b ,  
@RequestParam("storeCode")String storeCode) throws ApiException {}
     

需要注意@RequestBody byte[] in_b,用@RequestParam会导致Method has too many Body parameters.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值