728x90
반응형
URL예제
웹 상의 이미지 파일 다운로드
public class UrlTest {
public static void main(String[] args) {
UrlTest u = new UrlTest();
u.test();
}
public void test() {
String imgUrl = "https://cdn.pixabay.com/photo/2014/04/13/20/49/cat-323262_960_720.jpg";
URL url = null;
URLConnection conn = null;
try {
url = new URL(imgUrl);
conn = url.openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try(
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("cat-323262_960_720.jpg"));
){
int data = -1;
while((data = bis.read()) != -1) {
System.out.println(data);
bos.write(data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
반응형
'프로그래밍 > JAVA' 카테고리의 다른 글
04.22(예외 처리/ IO/ Collection/ Generics) (0) | 2020.04.22 |
---|---|
04.21(IO 예제로 정리하기) (0) | 2020.04.21 |
04.19(SimpleDateFormat 패턴) (0) | 2020.04.19 |
04.17(API - Calendar 예제) (0) | 2020.04.17 |
04.16(자바 IO 예제) (0) | 2020.04.16 |