文档中心 > AliExpress开放平台

Image uploading mechanism

更新时间:2022/01/28 访问次数:350

Upload images to photobank

image

Background:Developers/sellers may meet some errors when trying to upload/edit products using external url. For example: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target...

Further more, conditions of sellers' servers might vary in different countries, thus causing unstability when using external url. Currently,it is recommend to use the images' photobank_url to post or edit the products.

Solution:Recommended process is the following flow chart.

How to upload images to photobank:

Relative API:aliexpress.photobank.redefining.uploadimageforsdk

While invoking the API,the object FileItem for parameter "imagebytes" are important:

image

1.When the image you want to upload to the photobank is a local one,you can just use its local filepath:

TaobaoClient client = new DefaultTaobaoClient(url, appkey, appsecret);
AliexpressPhotobankRedefiningUploadimageforsdkRequest req = new AliexpressPhotobankRedefiningUploadimageforsdkRequest();
//req.setGroupId("0");
req.setImageBytes(new FileItem("/Users/zy/Desktop/zhaoyao/test.jpg"));  
req.setFileName("test.jpg"); 
AliexpressPhotobankRedefiningUploadimageforsdkResponse rsp = client.execute(req, sessionKey);
System.out.println(rsp.getBody());

2.When you trying to upload external images, it needs to be transformed into byte[] for processing.(One point to be noticed is that the external image URL could be access successfully). The following code is an example.

public static void main(String[] args) throws Exception {
        // 将图片url转换成byte数组
        URL url = new URL("https://ecommerce.grupotoysmaniatic.es/ArticuloImagen/119_50DMWNI-410882-1200-1200-E.jpg");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setReadTimeout(5 * 1000);
        InputStream stream = connection.getInputStream();
        byte[] image = readInputStream(stream);
        TaobaoClient client = new DefaultTaobaoClient(url, appkey, appsecret);
        AliexpressPhotobankRedefiningUploadimageforsdkRequest req = new AliexpressPhotobankRedefiningUploadimageforsdkRequest();
        //req.setGroupId("0");
        req.setImageBytes(new FileItem("aaa.jpg", image));
        req.setFileName("test.jpg");
        AliexpressPhotobankRedefiningUploadimageforsdkResponse rsp = client.execute(req, sessionKey);
        System.out.println(rsp.getBody());
    }
    private static byte[] readInputStream(InputStream stream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = stream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        stream.close();
        return outStream.toByteArray();
    }

Uploaded successfully:

After you upload the images to photobank successfully,the photobank_url will be returned in response(within "alicdn.com"), for example:"https://ae01.alicdn.com/kf/Hf34573381bfc48e98a86f718261f5049z.jpg".The developers now needs to store the mapping between the origin url and the photobank_url and using the photobank_url to post/edit products subsequently.

FAQ

关于此文档暂时还没有FAQ
返回
顶部