2013-10-19 2 views
0

나는 이미지 URL (http://example.com/myimage.jpg)을 가지고 있으며이를 바이트 배열로 변환하고 내 DB에 저장하려고합니다. Pls 몇 가지 예를 들어 도와주세요. 주어진 URL로 이미지 가져 오기 및 바이트 배열로 변환

은 내가 Javadoc File(URI)에 대한 생성자는 URI는 "파일"URI이어야 함을 지정 다음,하지만 점점이 메시지 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

했다. 즉, "파일"로 시작해야

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

관련 문제