因速卖通接口上传图片接口
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.