본문 바로가기
Work/java

외부 사이트 이미지 로컬에 가져다 사용 하기

by 승수 2011. 12. 2.
new File(uri) 함수에서 uri 값에 타 사이트 주소를 넣어줘도 파일을 가져 올수 있는줄 알았는데...

그래서 처음엔 new File("http://~~~~~~~~~~~~~~.aaa.jpg") 이런 식으로 다른 싸이트 주소를 썼는데

찾아 보니 uri 값은 로컬 주소만 된다 한다.  

그래서 다른 방법을 찾다 보니 

InputStream inputStream = new URL("http://~~~~~~~~~~~~~~.aaa.jpg").openStream(); /// 가져올 파일 주소및 파일명
File file = new File(reportPath+"/img/monthly_bps.png");  /// 로컬에 저장될 경로및 파일명 
OutputStream out = new FileOutputStream(file);
writeFile(inputStream, out);
out.close();     

public void writeFile(InputStream is, OutputStream os) throws IOException
{
    int c = 0;
    while((c = is.read()) != -1)
        os.write(c);
    os.flush();
}  

요렇게 해서 가져다 쓰면 됩니다.