gpt4 book ai didi

java - 从 RESTful api 传递图像的正确方法

转载 作者:行者123 更新时间:2023-12-02 01:57:10 24 4
gpt4 key购买 nike

我使用一个 API,该 API 应该读取存储在 Amazon S3 存储桶中的所有图像并将其传送到前端进行显示(以及进一步的操作)。我可以使用代码访问存储的所有图像,但是将它们传送到 FE 的正确格式是什么?

这里提供了MT代码,

@GetMapping(value = "/findAllImages")
public ResponseEntity<List<String>> findNamesOfAllImages() {

List<String> names = new ArrayList<>();

String password = ProcBuilder.run(
"security",
"-i", "find-generic-password",
"-l", Parameters.getAppName(), "-w"
);

AWSCredentials credentials = new BasicAWSCredentials(
Parameters.getAccessKey(),
password
);

AmazonS3 s3client = AmazonS3ClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(Regions.US_EAST_1)
.build();

ObjectListing objectListing = s3client.listObjects(Parameters.getBucketName());

for (S3ObjectSummary s3ObjectSummary : objectListing.getObjectSummaries()) {
names.add(s3ObjectSummary.getKey());
}

return ResponseEntity.status(HttpStatus.CREATED).body(names);
}

ATM 我只读取图像的名称并将它们存储在 ArrayList

最佳答案

我使用了OutputStream来将数据发送到FE。我不完全确定这是要走的路,但是,这是提供的代码。

@GetMapping(value = "/findAllImages")
public ResponseEntity<List<OutputStream>> findNamesOfAllImages() {

List<String> names = new ArrayList<>();

String password = ProcBuilder.run(
"security",
"-i", "find-generic-password",
"-l", Parameters.getAppName(), "-w"
);

AWSCredentials credentials = new BasicAWSCredentials(
Parameters.getAccessKey(),
password
);

AmazonS3 s3client = AmazonS3ClientBuilder
.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(Regions.US_EAST_1)
.build();

ObjectListing objectListing = s3client.listObjects(Parameters.getBucketName());

for (S3ObjectSummary s3ObjectSummary : objectListing.getObjectSummaries()) {
names.add(s3ObjectSummary.getKey());
}

List<OutputStream> outputStreams = new ArrayList<>();
S3Object object = null;

File file;
int count = 0;

for (String name : names) {

object = s3client.getObject(new GetObjectRequest(Parameters.getBucketName(), name));;

InputStream reader = new BufferedInputStream(object.getObjectContent());
file = new File(name + (++count));

OutputStream writer;

try {

writer = new BufferedOutputStream(new FileOutputStream(file));
int read = -1;

while (true) {

if (!((read = reader.read()) != -1))
break;

writer.write(read);
outputStreams.add(writer);
}

writer.flush();
writer.close();
reader.close();

} catch (IOException e) {
e.printStackTrace();
}
}

return ResponseEntity.status(HttpStatus.CREATED).body(outputStreams);
}

关于java - 从 RESTful api 传递图像的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57393403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com