windows系统实现自动定时到网络下载图片

首先使用java语言实现到指定URL下下载指定的img图片

package com.local;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) throws IOException {
        String spec = "http://www.hzfc.gov.cn/scxx/";
        URL url = new URL(spec);
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        InputStream in = conn.getInputStream();
        InputStreamReader ir = new InputStreamReader(in, "gbk");
        BufferedReader reader = new BufferedReader(ir);
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("
");
        }
        String str = sb.toString();

        // 获得 商品房累计成交信息 图片
        String SPF_PIC = null;
        String regex = "(<h1>.*商品房累计成交信息.*区域.*</h1>[\s\S]+?<img.+/>)";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        while (m.find()) {
            String res = m.group(1);
            regex = "('.+')";
            p = Pattern.compile(regex);
            m = p.matcher(res);
            while (m.find()) {
                res = m.group(1);
                res = res.replaceAll("'", "");
                res = spec + "/" + res;
                SPF_PIC = res;
            }
        }

        // 获得 商品房累计成交信息 图片
        String RSF_PIC = null;
        regex = "(<h1>.*二手房累计成交信息.*</h1>[\s\S]+?<img.+/>)";
        p = Pattern.compile(regex);
        m = p.matcher(str);
        while (m.find()) {
            String res = m.group(1);
            regex = "('.+')";
            p = Pattern.compile(regex);
            m = p.matcher(res);
            while (m.find()) {
                res = m.group(1);
                res = res.replaceAll("'", "");
                res = spec + "/" + res;
                RSF_PIC = res;
            }
        }

        // 下载商品房图片
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH_mm_");//设置日期格式
        url = new URL(SPF_PIC);
        DataInputStream dataInputStream = new DataInputStream(url.openStream());
        String imageName = "G:\家庭\房产\房产成交记录-每日总\"+df.format(new Date())+"商品房累计成交信息.jpg";
        FileOutputStream fileOutputStream = new FileOutputStream(new File(imageName));

        byte[] buffer = new byte[1024];
        int length;

        while ((length = dataInputStream.read(buffer)) > 0) {
            fileOutputStream.write(buffer, 0, length);
        }
        dataInputStream.close();
        fileOutputStream.close();

        // 下载二手房房图片
        url = new URL(RSF_PIC);
        dataInputStream = new DataInputStream(url.openStream());
        imageName = "G:\家庭\房产\房产成交记录-每日总\"+df.format(new Date())+"-二手房累计成交信息.jpg";
        fileOutputStream = new FileOutputStream(new File(imageName));

        buffer = new byte[1024];
        length = 0;

        while ((length = dataInputStream.read(buffer)) > 0) {
            fileOutputStream.write(buffer, 0, length);
        }
        
        dataInputStream.close();
        fileOutputStream.close();
        reader.close();
        ir.close();
        in.close();
        
    }
}

然后,编写bat文件,运行class文件

::定时下载图片
h:
cd H:eclipse-oxygen-workspaceTESTHousesrcmainjavacomlocal
javac -encoding utf-8 -d . Test.java
java com/local/Test

最后,设置windows的计划任务

     右键计算机--管理--系统工具--任务计划程序--创建任务--设置任务名称,触发器,操作--点击确定,完成

原文地址:https://www.cnblogs.com/caer/p/7909299.html