关于给出url自动下载图片的demo
2015-12-21 11:46
387 查看
背景:最近在做一个数据的导出,需要根据url地址从服务器上下载图片打包交付别人,这里我写了一个程序:
一:自动下载图片的方法:
public static void DownPic(String imgUrl,String imgName) throws IOException{
String imageUrl = imgUrl;
URL url = new URL(imageUrl);
//打开网络输入流
DataInputStream dis = new DataInputStream(url.openStream());
String newImageName="G:/workspace/backfsn_旧版/old_fsn_pic/"+imgName;
//建立一个新的文件
FileOutputStream fos = new FileOutputStream(new File(newImageName));
byte[] buffer = new byte[1024];
int length;
//开始填充数据
while( (length = dis.read(buffer))>0){
fos.write(buffer,0,length);
}
dis.close();
fos.close();
}
二:为对象设计原型:
public class ImgInfo {
private String imgName;
private String imgUrl;
public String getImgName() {
return imgName;
}
public void setImgName(String imgName) {
this.imgName = imgName;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public ImgInfo(Map row) {
this.imgName = row.get("FILE_NAME")==null?" ": row.get("FILE_NAME").toString();
this.imgUrl =row.get("URL")==null?" ": row.get("URL").toString();
}
}
三:业务逻辑设计:
public static Map<String,String> getImgUrl(int errNum) throws IOException{
String sql = "SELECT tt.FILE_NAME ,tt.URL FROM t_test_resource tt INNER JOIN t_test_product_to_resource tp on tp.RESOURCE_ID=tt.RESOURCE_ID LIMIT 5000 ";
Result rs = DBConnection.getSelected(sql);
if(rs!=null && rs.getRowCount()>0){
for(int i=errNum;i<rs.getRowCount();i++){
ImgInfo imgInfo=new ImgInfo(rs.getRows()[i]);
circulationNum=i;
//处理带"/"的文件名
if(imgInfo.getImgName().contains("/")){
String[] ss = imgInfo.getImgName().split("/");
imgInfo.setImgName(ss[ss.length-1]);
}
if(imgInfo.getImgName().contains("?")){
String s = imgInfo.getImgName().replace("?","-");
imgInfo.setImgName(s);
}
DownPic(imgInfo.getImgUrl(), imgInfo.getImgName());
}
}
return null;
}
四:main 函数执行:
public static void main(String[] args) throws IOException {
int errorNum=0;
long startTime=System.currentTimeMillis();
try {
getImgUrl(0);
} catch (IOException e) {
//拋異常讓程序繼續
e.printStackTrace();
System.out.println("-——错误文件--"+circulationNum);
getImgUrl(circulationNum+1);
}
long endTime=System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
System.out.println("出錯文件數量: "+errorNum);
}
总结:写这个小demo时出现一些错误:比如文件名称含非法字符,一开始是采用抛出异常后继续执行程序的方式来解决,执行到后面出现越来越多的错误,就选在了业务逻辑里对这些异常处理。
一:自动下载图片的方法:
public static void DownPic(String imgUrl,String imgName) throws IOException{
String imageUrl = imgUrl;
URL url = new URL(imageUrl);
//打开网络输入流
DataInputStream dis = new DataInputStream(url.openStream());
String newImageName="G:/workspace/backfsn_旧版/old_fsn_pic/"+imgName;
//建立一个新的文件
FileOutputStream fos = new FileOutputStream(new File(newImageName));
byte[] buffer = new byte[1024];
int length;
//开始填充数据
while( (length = dis.read(buffer))>0){
fos.write(buffer,0,length);
}
dis.close();
fos.close();
}
二:为对象设计原型:
public class ImgInfo {
private String imgName;
private String imgUrl;
public String getImgName() {
return imgName;
}
public void setImgName(String imgName) {
this.imgName = imgName;
}
public String getImgUrl() {
return imgUrl;
}
public void setImgUrl(String imgUrl) {
this.imgUrl = imgUrl;
}
public ImgInfo(Map row) {
this.imgName = row.get("FILE_NAME")==null?" ": row.get("FILE_NAME").toString();
this.imgUrl =row.get("URL")==null?" ": row.get("URL").toString();
}
}
三:业务逻辑设计:
public static Map<String,String> getImgUrl(int errNum) throws IOException{
String sql = "SELECT tt.FILE_NAME ,tt.URL FROM t_test_resource tt INNER JOIN t_test_product_to_resource tp on tp.RESOURCE_ID=tt.RESOURCE_ID LIMIT 5000 ";
Result rs = DBConnection.getSelected(sql);
if(rs!=null && rs.getRowCount()>0){
for(int i=errNum;i<rs.getRowCount();i++){
ImgInfo imgInfo=new ImgInfo(rs.getRows()[i]);
circulationNum=i;
//处理带"/"的文件名
if(imgInfo.getImgName().contains("/")){
String[] ss = imgInfo.getImgName().split("/");
imgInfo.setImgName(ss[ss.length-1]);
}
if(imgInfo.getImgName().contains("?")){
String s = imgInfo.getImgName().replace("?","-");
imgInfo.setImgName(s);
}
DownPic(imgInfo.getImgUrl(), imgInfo.getImgName());
}
}
return null;
}
四:main 函数执行:
public static void main(String[] args) throws IOException {
int errorNum=0;
long startTime=System.currentTimeMillis();
try {
getImgUrl(0);
} catch (IOException e) {
//拋異常讓程序繼續
e.printStackTrace();
System.out.println("-——错误文件--"+circulationNum);
getImgUrl(circulationNum+1);
}
long endTime=System.currentTimeMillis(); //获取结束时间
System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
System.out.println("出錯文件數量: "+errorNum);
}
总结:写这个小demo时出现一些错误:比如文件名称含非法字符,一开始是采用抛出异常后继续执行程序的方式来解决,执行到后面出现越来越多的错误,就选在了业务逻辑里对这些异常处理。
相关文章推荐
- 贝叶斯网络的应用实例一
- Android API之Drawable资源
- 控件的android:layout_alignParentTop属性找不到的原因
- 变量在 PHP7 内部的实现(一)
- java编写的简单移动方块小游戏代码
- jQuery焦点图插件SaySlide
- Android多国语言缩写(全)
- 子图同构算法:VF2算法(含代码)
- Health Kit教程(二)
- 快速创建maven 工程:simple java工程,webapp
- iOS 宏(define)与常量(const)的正确使用
- 状态保持与恢复onSaveInstanceState
- JavaScript Array: slice vs splice
- Android之ContentProvider
- AFnetworking post 添加请求头
- 旋转打印n阶矩阵(第0届第4题)
- 关于app退出后子线程停止的问题
- Spring中数据源配置
- 常见HTTP错误代码大全
- 渐变背景 css3渐变效果及代码