基本上,您需要做的是创建一个 HttpClient,然后使用此客户端执行图像上传命令,让事情变得更具体,看看这个测试场景
这里是 commercetools JVM SDK 的典型用途:
//create client
SphereClientConfig sphereClientConfig = SphereClientConfig.of( projectKey, clientId, clientSecret);
SphereClient client = SphereClient.of(sphereClientConfig, SphereClientFactory.of().createHttpClient(), SphereAccessTokenSupplier.ofConstantToken("accessToken"))
final ByIdVariantIdentifier identifier = product.getMasterData().getStaged().getMasterVariant().getIdentifier();
File imageFile = new File("Path to your image");
//create update commands
final ProductImageUploadCommand cmd1 = ProductImageUploadCommand
.ofVariantId(imageFile, identifier)
.withFilename("myProductImage1.gif")
.withStaged(true);
final ProductImageUploadCommand cmd2 = ProductImageUploadCommand
.ofVariantId(imageFile, identifier)
.withFilename("myProductImage2.gif")
.withStaged(true);
//update the product
final Product updatedProduct1 = client().executeBlocking(cmd1);
final Product updatedProduct = client().executeBlocking(cmd2);
//get the images
List<Image> images = updatedProduct.getMasterData().getStaged().getMasterVariant().getImages();
希望这可以帮助 :)