从存储桶Java下载图像列表

问题描述

我可以从下面的s3存储桶中检索图像列表吗?这是我的代码,但尚未编译。 images = imageResults.toArray(images);收到编译错误:Cannot resolve method 'toArray(byte[])'

 @Override
public byte[] downloadUserGalleryImages(String email) {

    UserProfile user = userRepo.findByEmail(email);
    if (Objects.isNull(user)){
        throw new UserServiceException(ErrorMessages.USER_NOT_FOUND.getErrorMessage());
    }

    String path = String.format("%s/%s/%s",BucketName.SPACE_NAME.getBucketName(),GALLERY_IMAGES,user.getUsername());
    byte[] images;
    List<byte[]> imageResults = new ArrayList<>();
    if (user.getImageGallery().size() > 0){
        user.getImageGallery().forEach(imageGallery -> {
            String imageUrl = imageGallery.getImageUrl();
            byte[] bytes = downloadUserImages(path,imageUrl);
            imageResults.add(bytes);

        });

    }
    images = new byte[imageResults.size()];
    images = imageResults.toArray(images);
    return images;
}

public byte[] downloadUserImages(String path,String key) {
    try {
        S3Object object = s3.getObject(path,key);
        return IOUtils.toByteArray(object.getObjectContent());
    } catch (AmazonServiceException | IOException e) {
        throw new IllegalStateException("Failed to download file to s3",e);
    }
}
 tried to map Bytes to list but this returns only one image I gues it's because of flatMap
images = Bytes.toArray(imageResults.stream()
            .map(Bytes::asList)
            .flatMap(Collection::stream)
            .collect(Collectors.toList()));
    return images;

老实说,我在处理字节方面遇到了麻烦,因此您的帮助得到了家人的高度赞赏。

解决方法

从Oracle的javaDoc中,ResourceOptions.MacroCreate的方法public T[] toArray?(T[] a)需要一个通用类型的数组。这不包括基元,因此ArrayList将给出错误:

byte[]

更改为byte[] myBytes1 = {0,1,2,3,4}; byte[] myBytes2 = {5,6,7,8,9}; List<byte[]> imageResults = new ArrayList<>(); imageResults.add(myBytes1); imageResults.add(myBytes2); byte[] images; images = new byte[imageResults.size()]; images = imageResults.toArray(images); // no suitable method found for toArray(byte[]) 以为该数组提供“适当的”类型将解决此问题,但是代码试图获取Byte[]数组的列表并将其放入一个数组中,无法做到:

Byte

但是,它可以获取Byte[] myBytes1 = {0,4}; Byte[] myBytes2 = {5,9}; List<Byte[]> imageResults = new ArrayList<>(); imageResults.add(myBytes1); imageResults.add(myBytes2); Byte[] images; // This must be a Byte[],not a byte[] images = new Byte[imageResults.size()]; images = imageResults.toArray(images); // arraycopy: element type mismatch 的列表并将其放入byte数组中:

Byte

它也可以采用byte myByte1 = 1; byte myByte2 = 2; List<Byte> imageResults = new ArrayList<>(); imageResults.add(myByte1); imageResults.add(myByte2); Byte[] images; // This must be a Byte[],not a byte[] images = new Byte[imageResults.size()]; images = imageResults.toArray(images); System.out.println(Arrays.toString(images)); // prints: // [1,2] 数组并将其放入二维byte数组中。请注意,它们是Byte数组,而不是Byte数组,因为byte

byte[] cannot be converted to Byte[]

如果您需要Byte[] myBytes1 = {0,9}; List<Byte[]> imageResults = new ArrayList<>(); imageResults.add(myBytes1); imageResults.add(myBytes2); Byte[][] images; images = new Byte[imageResults.size()][]; images = imageResults.toArray(images); System.out.println(Arrays.deepToString(images)); // prints: // [[0,4],[5,9]] 而不是byte[][],则不能使用Byte[][]方法。改为循环toArray()

ArrayList

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...