gpt4 book ai didi

java - 通过rest Api接收图像文件

转载 作者:行者123 更新时间:2023-12-01 09:45:29 24 4
gpt4 key购买 nike

如何通过Rest API接收图像文件。有一个 MULTIPART_FORM_DATA 选项,看起来它会像在多个请求中那样分部分发送文件。我想在服务器上非常快地接收图像。每秒大约 2 张图像。

最佳答案

只需读取File中的图像并使用Response类来构建响应。

Response.ok(new File("myimage.jpg"), "image/jpeg").build();

还有其他相同的变体。

使用以下内容读取图像。

URL url = new URL("http://localhost:8080/myimage/1");
URLConnection connection = url.openConnection();

input = connection.getInputStream();
byte[] buffer = new byte[1024];
int n = - 1;

OutputStream fos = new FileOutputStream("Output.jpg" );
while ( (n = input.read(buffer)) != -1)
{
fos.write(buffer, 0, n);
}
fos.close();

您可以使用 Apache HTTP 客户端使其更漂亮。

关于java - 通过rest Api接收图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38085373/

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