问题描述
我可以从下面的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