java 从网络下载视屏简短小程序
来源:互联网 发布:集合知とは何か 编辑:程序博客网 时间:2024/05/01 16:29
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();
}
}
}
0 0
- java 从网络下载视屏简短小程序
- java网络下载小程序
- 两个简短的python小程序
- Java从网络下载图片
- java从网络下载图片
- java小程序下载地址
- 从网络加载的小程序
- java/android从网络下载zip资源
- java 使用IO从网络下载图片-
- java ----URL 从网络下载图片
- JAVA从网络下载文件到本地
- 第二章 从一个最简短的C++程序讲起
- Android显示从网络下载图片偏小的问题
- Java小程序之简易网络画板
- Javascript简短小代码
- 简短的程序
- java学习篇(一)---从网络下载图片
- NSURLSession从网络上下载资源,此程序下载的是视频
- javascript中this的用法详解
- (转)控件EditText的setOnEditorActionListener方法的理解
- 集合
- 双11数据大屏直播 / 双11作战大屏
- C#---生成table的行序号的多种方法
- java 从网络下载视屏简短小程序
- Hashtable 和 HashMap的区别
- Gradle sync failed: failed to find target android-24
- 变而不变:我看分布式系统发展和阿里实践
- 线性回归、逻辑回归区别
- 基本的android 动画
- 轻松理解JavaScript之AJAX
- mfc 定时器
- 软件注册DLL思路