2008年5月软考之网络工程师考试上午试题
2008-05-26 01:01
531 查看
Java根据web网站Url下载图片
新窗口打开复制代码
package com.ronniewang.downloadpicture;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import com.ronniewang.utilities.JdbcUtil;
public class DownloadPicture {
public static void main(String[] args) {
测试下载图片:
http://img.iitu.net/img_lt/58pic/12/50/47/10U58PICMxQ.jpg
http://img.iitu.net/img_lt/58pic/12/50/47/02C58PIC5hd.jpg
http://img.iitu.net/img_lt/58pic/12/50/46/92E58PICZM4.jpg
DownloadPicture downloadPicture = new DownloadPicture();
ArrayList<String> urlList = downloadPicture.readUrlList();
downloadPicture.downloadPicture(urlList);
}
/**
* 传入要下载的图片的url列表,将url所对应的图片下载到本地
* @param urlList
*/
private void downloadPicture(ArrayList<String> urlList) {
URL url = null;
int imageNumber = 0;
for (String urlString : urlList) {
try {
url = new URL(urlString);
DataInputStream dataInputStream = new DataInputStream(url.openStream());
String imageName = imageNumber + ".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();
imageNumber++;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 连接mysql数据库,通过查询数据库读取要下载的图片的url列表
* @return
*/
private ArrayList<String> readUrlList() {
ArrayList<String> urlList = new ArrayList<String>();
try {
Connection connection = (Connection) JdbcUtil.getConnection();
Statement statement = (Statement) connection.createStatement();
String sql = "select url from url"; //查询语句换位相应select语句
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
String url = resultSet.getString("url");
urlList.add(url);
System.out.println(url);
}
JdbcUtil.free(resultSet, statement, connection);
} catch (SQLException e) {
e.printStackTrace();
}
return urlList;
}
}
相关文章推荐
- 2016年下半年软考网络工程师考试上午题试题答案发布
- 2009年上半年软考网络工程师考试试题(上午)
- 2009年上半年软考网络工程师考试试题(上午答案)
- 2007年下半年软考:网络工程师考试试题分析
- 网络工程师考试2006年下半年上午试题解析(三)
- 2008年5月网络工程师考试试题答案
- 网络工程师考试2007年下半年上午试题解析(一)
- 2008年5月网络工程师考试上午试卷参考答案
- 2009年5月全国计算机软考网络工程师考试试题答案
- 2007下半年软考:网络管理员考试试题(上午)无需下载
- 2009年上半年网络工程师考试上午试卷答案与解析(三)
- 网络工程师考试经验谈(软考)
- 2017跟着小虎玩着去软考--项目管理师上午考试试题之二(解析2016下半年项目管理师上午试题5-7题)
- 2007下半年软考:网络管理员考试试题(下午)无需下载
- 2008年5月网络工程师上午考试试题
- 06上半年网络工程师试题分类精讲.第一部分:计算机与网络知识(上午考试科目).第一章:计算机系统知识
- 2004年网络工程师考试模拟试题一
- 2017跟着小虎玩着去软考--项目管理师上午考试试题之六(解析2016下半年项目管理师上午试题20-23题)
- 2017年上半年软考网络工程师考试真题参考答案