package download;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
/**
* 从输入流中获取字节数组
* @param inputStream
* @return
* @throws IOException
*/
public class DownLoadMain {
/**
* 从网络Url中下载文件
* @param urlStr
* @param fileName
* @param savePath
* @throws IOException
*/
public static void downLoadFromUrl(String urlStr,String fileName,String savePath) throws IOException{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
//conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
byte[] getData = readInputStream(inputStream);
//文件保存位置
File saveDir = new File(savePath);
if(!saveDir.exists()){
saveDir.mkdir();
}
File file = new File(saveDir+File.separator+fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if(fos!=null){
fos.close();
}
if(inputStream!=null){
inputStream.close();
}
System.out.println("info:"+fileName+':'+url+" download success");
}
/**
* 从输入流中获取字节数组
* @param inputStream
* @return
* @throws IOException
*/
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//FabricCommunicationException
//驱动器的名称
String driver="com.mysql.jdbc.Driver";
//url 只想数据的的数据库名
String url="jdbc:mysql://127.0.0.1:3306/foodnutrifrombh";
//Mysql 的用户名,密码
String user="root";
String password="root";
//System.out.println("ajizai");
try{
//加载驱动程序
//System.out.println("ajizai");
Class.forName(driver);
//lianjie shju ku
Connection connection=DriverManager.getConnection(url,user,password);
if(!connection.isClosed())
System.out.println(" sucesss conect database!");
//只想Sql语句
Statement statement=(Statement) connection.createStatement();
//执行Sql语句
String sql="select chinesename,downloadurl from download1 where id >16";
//结果集
ResultSet resultSet=statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println(" 名称" + "\t" + " url");
System.out.println("-----------------");
String chinesename=null;
int i=1;
while(resultSet.next()){
//选择chinsesename这一列数据
chinesename=resultSet.getString("chinesename");
//解码
chinesename = new String(chinesename.getBytes("utf-8"),"utf-8");
System.out.println(resultSet.getString("downloadurl") + "\t" +chinesename +"\t"+i++);
downLoadFromUrl(resultSet.getString("downloadurl"),chinesename+".mp4","E:/shipin/");
}
resultSet.close();
connection.close();
}
catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
/**
* 从输入流中获取字节数组
* @param inputStream
* @return
* @throws IOException
*/
public class DownLoadMain {
/**
* 从网络Url中下载文件
* @param urlStr
* @param fileName
* @param savePath
* @throws IOException
*/
public static void downLoadFromUrl(String urlStr,String fileName,String savePath) throws IOException{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为3秒
//conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
//得到输入流
InputStream inputStream = conn.getInputStream();
//获取自己数组
byte[] getData = readInputStream(inputStream);
//文件保存位置
File saveDir = new File(savePath);
if(!saveDir.exists()){
saveDir.mkdir();
}
File file = new File(saveDir+File.separator+fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if(fos!=null){
fos.close();
}
if(inputStream!=null){
inputStream.close();
}
System.out.println("info:"+fileName+':'+url+" download success");
}
/**
* 从输入流中获取字节数组
* @param inputStream
* @return
* @throws IOException
*/
public static byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//FabricCommunicationException
//驱动器的名称
String driver="com.mysql.jdbc.Driver";
//url 只想数据的的数据库名
String url="jdbc:mysql://127.0.0.1:3306/foodnutrifrombh";
//Mysql 的用户名,密码
String user="root";
String password="root";
//System.out.println("ajizai");
try{
//加载驱动程序
//System.out.println("ajizai");
Class.forName(driver);
//lianjie shju ku
Connection connection=DriverManager.getConnection(url,user,password);
if(!connection.isClosed())
System.out.println(" sucesss conect database!");
//只想Sql语句
Statement statement=(Statement) connection.createStatement();
//执行Sql语句
String sql="select chinesename,downloadurl from download1 where id >16";
//结果集
ResultSet resultSet=statement.executeQuery(sql);
System.out.println("-----------------");
System.out.println("执行结果如下所示:");
System.out.println("-----------------");
System.out.println(" 名称" + "\t" + " url");
System.out.println("-----------------");
String chinesename=null;
int i=1;
while(resultSet.next()){
//选择chinsesename这一列数据
chinesename=resultSet.getString("chinesename");
//解码
chinesename = new String(chinesename.getBytes("utf-8"),"utf-8");
System.out.println(resultSet.getString("downloadurl") + "\t" +chinesename +"\t"+i++);
downLoadFromUrl(resultSet.getString("downloadurl"),chinesename+".mp4","E:/shipin/");
}
resultSet.close();
connection.close();
}
catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}