【问题标题】:Receive image file through rest Api通过rest Api接收图片文件
【发布时间】:2016-06-28 20:08:13
【问题描述】:

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

【问题讨论】:

  • 请看help page,了解如何提出一个好问题:你的问题太模糊,没有提供任何你尝试过的例子。
  • 使用base64格式
  • 感谢@CSK base64 原来是一个不错的选择。

标签: java rest multipartform-data jersey-2.0 form-data


【解决方案1】:

只需读取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 客户端使其更漂亮。

【讨论】:

  • 我想在服务器端接收图像。我打算使用@FormDataParam。有没有其他有效的方法。
  • 您只需使用 HTTP 客户端下载文件。阅读简单的inputstream 没有什么不同
  • @sunder,我以为你想调用一个 REST api 来上传图片@FormDataParam 没问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 2016-03-20
  • 2023-03-10
相关资源
最近更新 更多