2013-10-19 31 views
0

我有一个图像url(http://example.com/myimage.jpg),并希望将其转换为字节数组并将其保存在我的数据库中。请帮助我举一些例子。获取图像与给定的网址,并将其转换为字节数组

我做了以下内容,但得到这个消息URI scheme is not "file"

URI uri = new URI(profileImgUrl); 
File fnew = new File(uri); 
BufferedImage originalImage=ImageIO.read(fnew); 
ByteArrayOutputStream baos=new ByteArrayOutputStream(); 
ImageIO.write(originalImage, "jpg", baos); 
byte[] imageInByte=baos.toByteArray(); 

回答

2

JavadocFile(URI)构造函数指定URI必须是一个“文件” URI。换句话说,它应该以“文件:”开始

URI一个绝对的分层URI与方案等于“文件”,一个 非空路径组件,以及不确定的权限,查询,片段 组件

但是你可以达到你正在尝试做用,而不是一个文件/ URI的URL,:

URL imageURL = new URL(profileImgUrl); 
BufferedImage originalImage=ImageIO.read(imageURL); 
ByteArrayOutputStream baos=new ByteArrayOutputStream(); 
ImageIO.write(originalImage, "jpg", baos); 

//Persist - in this case to a file 

FileOutputStream fos = new FileOutputStream("outputImageName.jpg"); 
baos.writeTo(fos); 
fos.close(); 
+0

感谢!!!!!!!! – emilan

+1

为什么不直接保存字节,通过图像进行确实是一个很大的开销? – ThomasRS

相关问题