根据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函数会抛一些异常:导致异常的原因是文件格式不对,或者文件名含有非法字符,在进行数据封装成对象时可做相应的处理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值