根据数据库里的url批量下载

来源:互联网 发布:countdown.js 使用 编辑:程序博客网 时间:2024/05/16 07:33
import java.io.*;
import java.net.*;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

 class DBHelper {  
public static final String url = "jdbc:mysql://ip:3306/db";  
    public static final String name = "com.mysql.jdbc.Driver";  
    public static final String user = "root";  
    public static final String password = "root";  

    public Connection conn = null;  
    public PreparedStatement pst = null;  
  
    public DBHelper(String sql) {  
        try {  
            Class.forName(name);//指定连接类型  
            conn = (Connection) DriverManager.getConnection(url, user, password);//获取连接  
            pst = (PreparedStatement) conn.prepareStatement(sql);//准备执行语句  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
  
    public void close() {  
        try {  
            this.conn.close();  
            this.pst.close();  
       } catch (SQLException e) {  
    e.printStackTrace();  
        }  
    }  
}  

public class Main {  
 
    static String sql = null;  
    static DBHelper db1 = null;  
    static ResultSet ret = null;  
    public static void downloadFile(String remoteFilePath, String localFilePath) throws IOException
    {
        URL urlfile = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        File f = new File(localFilePath);
        if(!f.exists())
        f.createNewFile();
        else{
        int i=1;
        do{
        i++;
        f=new File(localFilePath+"-"+i+".pdf");
        }while(f.exists());
        f.createNewFile();
        }
        try
        {
            urlfile = new URL(remoteFilePath);
            httpUrl = (HttpURLConnection)urlfile.openConnection();
            httpUrl.connect();
            bis = new BufferedInputStream(httpUrl.getInputStream());
            bos = new BufferedOutputStream(new FileOutputStream(f));
            int len = 2048;
            byte[] b = new byte[len];
            while ((len = bis.read(b)) != -1)
            {
                bos.write(b, 0, len);
            }
            bos.flush();
            bis.close();
            httpUrl.disconnect();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                bis.close();
                bos.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws IOException {  
        sql = "select attach_url,attach_name from nstr_attachment where attach_url like '%.pdf' ";//SQL语句  
        db1 = new DBHelper(sql);//创建DBHelper对象  
  
        try {  
            ret = db1.pst.executeQuery();//执行语句,得到结果集  
            while (ret.next()) {  
                String attach_url = ret.getString(1);  
                String attach_name = ret.getString(2);  
                downloadFile(attach_url,"F:/pdf/"+attach_name);//从远程下载文件
                System.out.println(attach_url + "\t" + attach_name );
                
            }//显示数据  
            ret.close();  
            db1.close();//关闭连接  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }  
  
}  
0 0
原创粉丝点击