日期:2014-05-20  浏览次数:20643 次

求助,读取本地图片转换成byte数组
想通过java读取图片,转成byte格式存入数据库,在网上找了很久,都不太行。
求助各位大牛了,谢谢!



比如我想上传这张图片
File image = new File("./images/header.jpg");

那我之后该如何转换呢?



------解决方案--------------------
读取图片为流,然后用流得到byte数组,就这样 的
------解决方案--------------------
BufferedInputStream in = new BufferedInputStream(new FileInputStream("f:/2.jpg"));
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);

byte[] temp = new byte[1024];
int size = 0;
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
in.close();
byte[] content = out.toByteArray();