想每個人都有這種需要,有時候就是想自己下載圖片,

這也是我自學JAVA的動力啊 ^O^

參考 JAVA World 的 paochih  的文章

以我自己的圖片的下載範例

參考參考...


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;


public class DLpic {
   public static void main(String[] args) {
       
       URL url =null ;
    try {
            
        String[] pic = {"http://pic.pimg.tw/robertvmp/1171610314.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610321.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610323.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610328.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610331.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610338.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610340.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610342.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610344.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610346.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610348.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610351.jpg",
                        "http://pic.pimg.tw/robertvmp/1171610353.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610356.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610358.jpg",    
                        "http://pic.pimg.tw/robertvmp/1171610367.jpg",    
                        "http://pic.pimg.tw/robertvmp/1198075299.jpg",    
                        "http://pic.pimg.tw/robertvmp/1198075300.jpg"};

        for (int k=0; k< pic.length ; k++){
            url = new URL(pic[k]);
            
            FileOutputStream fos = new FileOutputStream(pic[k].substring(pic[k].length() - 14), false);
            InputStream is = url.openStream();
            int r = 0;
            System.out.println("Downloading - " + (k+1));

            /* //==Solution 1.直接讀入
            while ( ( r = is.read() ) != -1){
                fos.write(r);

             } */

           /* //==Solution 2.讀到 buffer , <<速度較快>>
            byte[] buf = new byte[100000]; // 100kb buffer
            while ( ( r = is.read(buf) ) != -1){
                fos.write(buf,0,r);
            }*/

            //==Solution 3.讀到 buffer , <<較快一點 & 精準的寫法>>
            //因為不能保證連線的品質,或是檔案較大的時候,也許會有lose
            int chunkSize = 1024 * 8;
            byte[] buf = new byte[chunkSize];
            int readLen;
            while ((readLen = is.read( buf, 0, buf.length)) != -1) {
               fos.write(buf, 0, readLen);  
            }

                is.close();
                fos.close();
        }
        
        System.out.println("Download Finished. ");
        
        
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
   }       

}

arrow
arrow
    全站熱搜

    羅伯特 發表在 痞客邦 留言(0) 人氣()