根据数据库里的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
- 根据数据库里的url批量下载
- Android里根据下载文件的url获取文件名
- java根据图片的URL下载图片
- 根据url自动下载图片的demo
- 根据url下载音乐
- 根据URL下载文件
- 根据url下载apk
- 根据URL下载文件
- 【android】简单的根据url下载图片的一个类
- 根据URL链接下载文件
- Java根据Url下载图片
- android根据url下载图片
- HttpURLConnection根据URL下载图片
- 根据url下载网络图片
- HttpURLConnection根据URL下载图片
- 根据 url地址 下载图片
- java根据url下载图片
- iOS网络编程---根据URL下载网络文件的方法
- Haskell 2
- javascript中apply()和call()方法的区别
- Linux 时代的来临
- C++11 并发指南六( <atomic> 类型详解二 std::atomic )
- BZOJ 1012: [JSOI2008]最大数maxnumber
- 根据数据库里的url批量下载
- 一篇关于java泛型的详细讲解
- 为你解惑:css中单位px,em,rem的区别。。。
- 网络安全-加密验签方案
- hdu1421_搬寝室
- 大个酷论Classpath&&Path
- Hadoop-2.6.4集群(三个节点)安装(详细图文)
- 【web】上机实验六
- Vue.js入门环境搭建