2012/12/03 10:51

url상 이미지, 읽어들여 내 시스템에서 처리하기. JAVA

오~

url상에 올려진 이미지 등의 파일을
읽어들여, 시스템에서 다운로드 등의 작업을 가능케 하는 코드. 

오, 역시, 안되는게 없어;; 

--------------------------------

public static void main(String[] args) {

            

 

             URL url = null;

             InputStream in = null;

             FileOutputStream fos = null;

            

             try {

                    url = new URL("http://www.google.ro/logos/2011/twain11-hp-bg.jpg");

 

                    URLConnection connection = url.openConnection();

                    in = connection.getInputStream();

                    fos = new FileOutputStream(new File(

                                 "c:\\downloaded.jpg"));

                    byte[] buf = new byte[512];

                    while (true) {

                           int len = in.read(buf);

                           if (len == -1) {

                                 break;

                           }

                           fos.write(buf, 0, len);

                    }

             } catch (Exception e) {

                    // TODO Auto-generated catch block

                    e.printStackTrace();

             }

             finally

             {

                    try {

                           in.close();

                    } catch (IOException e) {

                           // TODO Auto-generated catch block

                           e.printStackTrace();

                    }

                    try {

                           fos.flush();

                    } catch (IOException e) {

                           // TODO Auto-generated catch block

                           e.printStackTrace();

                    }

                    try {

                           fos.close();

                    } catch (IOException e) {

                           // TODO Auto-generated catch block

                           e.printStackTrace();

                    }

             }

 

       }

--------------------------------

urlConnection을 붙여서 파일을 읽어들일 줄이야..
정말,, 끝은.. 어디인가.. OTL...
ㅋㅋ
하나 배워간다~



덧글

댓글 입력 영역