根據url自動下載圖片的demo

背景:最近在做一個工作,根據url 自動從服務器上下載圖片,以下是寫的demo:

一:自動下載文件的程序:

public static void DownPic(String imgUrl,String imgName) throws IOException{
        
        
        
        String imageUrl = imgUrl;
           
        URL url = new URL(imageUrl);
       
        //打開網絡輸入流
       
        DataInputStream dis = new DataInputStream(url.openStream());
       
        String newImageName="G:/workspace/backfsn_舊版/old_fsn_pic/"+imgName;
       
        //建立一個新的文件
       
        FileOutputStream fos = new FileOutputStream(new File(newImageName));
       
        byte[] buffer = new byte[1024];
       
        int length;
       
        //開始填充數據
       
        while( (length = dis.read(buffer))>0){
       
        fos.write(buffer,0,length);
       
        }
       
        dis.close();
       
        fos.close();
        
    }



二:建一個對象模型:

   public class ImgInfo {
    private String imgName;
    private String imgUrl;
    
    
    
    
    public String getImgName() {
        return imgName;
    }
    public void setImgName(String imgName) {
        this.imgName = imgName;
    }
    public String getImgUrl() {
        return imgUrl;
    }
    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }
    
    
    public ImgInfo(Map row) {
        this.imgName = row.get("FILE_NAME")==null?" ": row.get("FILE_NAME").toString();
        this.imgUrl =row.get("URL")==null?" ": row.get("URL").toString();
    }

}

三:從數據庫獲取數據及處理:

public static Map<String,String> getImgUrl(int errNum) throws IOException{
        String sql = "SELECT tt.FILE_NAME ,tt.URL FROM t_test_resource tt INNER JOIN  t_test_product_to_resource  tp on tp.RESOURCE_ID=tt.RESOURCE_ID LIMIT 5000 ";
        Result rs = DBConnection.getSelected(sql);
            if(rs!=null && rs.getRowCount()>0){
                for(int i=errNum;i<rs.getRowCount();i++){
                    ImgInfo imgInfo=new ImgInfo(rs.getRows()[i]);
//                    System.out.println("----"+imgInfo.getImgUrl());
                    circulationNum=i;
                    //處理帶"/"的文件名
                    if(imgInfo.getImgName().contains("/")){
                         String[] ss = imgInfo.getImgName().split("/");  
                         imgInfo.setImgName(ss[ss.length-1]);
                    }
                    if(imgInfo.getImgName().contains("?")){
                         String s = imgInfo.getImgName().replace("?","-");  
                         imgInfo.setImgName(s);
                    }
                    DownPic(imgInfo.getImgUrl(), imgInfo.getImgName());
                    
                }
                
            }
            
        
        
        return null;
        
    }




四:main函數調用:

    public static void main(String[] args) throws IOException  {
        int errorNum=0;
        long startTime=System.currentTimeMillis();
        try {
            getImgUrl(0);
        } catch (IOException e) {
            //拋異常讓程序繼續
            e.printStackTrace();
            System.out.println("-——錯誤文件--"+circulationNum);
            getImgUrl(circulationNum+1);
        }
        long endTime=System.currentTimeMillis(); //獲取結束時間
        System.out.println("程序運行時間: "+(endTime-startTime)+"ms");
        System.out.println("出錯文件數量: "+errorNum);
    
    }

    



需要注意的是:main函數會拋一些異常:導致異常的原因是文件格式不對,或者文件名含有非法字符,在進行數據封裝成對象時可做相應的處理

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章