AndServer
AndServer copied to clipboard
我想问一下怎么请求返回图片和Json字符串
我看了示例没发现返回图片的。 不是储存的file,我想直接返回屏幕截屏
使用了
@ResponseBody
@GetMapping(path = "/cap", produces = "image/png")
返回的head头依然是json的格式。
@ResponseBody
@GetMapping(path = "/cap", produces = "image/png")
byte[] image() throws IOException {
AssetManager am = App.getInstance().getAssets();
InputStream in = am.open("web/image/logo.png");
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[4096];
int count = -1;
while((count = in.read(data,0,4096)) != -1)
outStream.write(data, 0, count);
data = null;
return outStream.toByteArray();
}
详细代码
我也想知道怎么返回手机中的图片供其他客户端下载或查看
可以返回Body:
File file = ...;
return new FileBody(file);
文件也可以放成静态的链接,比如可以放在网站中。
可以返回Body:
File file = ...; return new FileBody(file);
文件也可以放成静态的链接,比如可以放在网站中。
您好,按照这种方式处理,提示这个
@yanzhenjie 能否直接下载sd卡下面的资源
可以返回Body:
File file = ...; return new FileBody(file);
文件也可以放成静态的链接,比如可以放在网站中。
您好,按照这种方式处理,提示这个
是支持的,body是能直接返回的,要看你接口代码和converter代码是怎么写的了。
@yanzhenjie 能否直接下载sd卡下面的资源
能,两种方式:
- sd卡的目录作为website,内容就是静态链接。
- 通过一个接口来提供访问能力,文件名称用path或者query表示都可以做到。
@RestController @RequestMapping(path = "/file") public class FileController {
@GetMapping("/img")
public FileBody getImage(HttpResponse response) {
String imgPath = Environment.getExternalStorageDirectory().getPath() + "pic.jpg";
File file = new File(imgPath);
return new FileBody(file);
}
}