如何通过Rest API接收镜像文件。有一个MULTIPART_FORM_DATA选项,看起来它将在多个请求中以部分发送文件。我想在服务器上接收图像非常快。大约每秒2张图片。
发布于 2016-06-29 04:33:26
只需读取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客户端来使其更漂亮。
https://stackoverflow.com/questions/38085373
复制相似问题