package download;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.zip.GZIPInputStream;/*** 从url指定的地址下载一个文件到本地* 2014-8-5 17:28:50**/
public class Downloader{private URL url;// 网络文件的地址URLConnection con;// 到URL的连接InputStream is;// 输入流public Downloader(String urlStr,String path,String fileName) throws Exception{url = new URL(urlStr);con = url.openConnection();is = con.getInputStream();// 未取到文件int length=con.getContentLength();if(length==71495){throw new Exception(urlStr+"指向的文件不存在.");}File folder=new File(path);if(folder.exists()==false || folder.isFile()){folder.mkdir();}String filename=path+File.separator+fileName;String code = con.getHeaderField("Content-Encoding");if ((null != code) && code.equals("gzip")) {GZIPInputStream gis = new GZIPInputStream(is);// 1K的数据缓冲byte[] bs = new byte[1024];// 读取到的数据长度int len;// 输出的文件流OutputStream os = new FileOutputStream(filename);// 开始读取while ((len = gis.read(bs)) != -1) {os.write(bs, 0, len);}// 完毕,关闭所有链接
            gis.close();os.close();is.close();} else {// 1K的数据缓冲byte[] bs = new byte[1024];// 读取到的数据长度int len;// 输出的文件流OutputStream os = new FileOutputStream(filename);// 开始读取while ((len = is.read(bs)) != -1) {os.write(bs, 0, len);}// 完毕,关闭所有链接
            os.close();is.close();}System.out.println(filename+"已保存");}public static void main(String[] args) throws Exception{String picUrl="http://img.1234.com/Upload2010/Sabfdra/SabrafdsfsfCOVERGIRLgtz/01.jpg";String[] arr=picUrl.split("/");int n=arr.length;String folder="C://reedf//wallpaper//"+arr[n-2];String[] arr2=arr[n-1].split("[.]");String ext=arr2[arr2.length-1];picUrl=picUrl.replace(arr[n-1], "");String url;String filename;String index;for(int i=0;i<150;i++){if(i<10){index="0"+i;}else{index=String.valueOf(i);}url=picUrl+index+"."+ext;filename=index+"."+ext;try{new Downloader(url,folder,filename);}catch(Exception ex){// do nothing
            }}}
}

本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/xiandedanteng/p/3892771.html,如需转载请自行联系原作者

爪哇国新游记之二十八----从url指定的地址下载文件到本地相关推荐

  1. 爪哇国新游记之二十九----访问URL获取输入流

    代码: import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import ...

  2. 爪哇国新游记之二十六----迷宫寻路

    代码: class Position{int x;int y;public Position(int x,int y){this.x=x;this.y=y;} } // 迷宫寻路 public cla ...

  3. 爪哇国新游记之二十四----二叉树

    /*** 二叉树节点类* */ class Node<T extends Comparable> {public Node(T data){this.data=data;}T data;N ...

  4. 爪哇国新游记之二十五----图及其遍历查找

    代码: import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java. ...

  5. 爪哇国新游记之二十一----快算24

    四张牌,通过四则运算得到24,一个数限用一次,快者为胜. 代码: import java.util.ArrayList; import java.util.HashSet; import java.u ...

  6. 爪哇国新游记之十八----泛型栈类

    import java.lang.reflect.Array;/*** 泛型栈** @param <T>*/ public class Stack<T>{private Cla ...

  7. 爪哇国新游记之三十二----邮件发送

    由三个类完成任务,第一个为主,main中是用法示例. 纯邮件发送和带附件发送邮件皆可,大家请参照main函数中用法. package com.ufo.util.mail;import java.uti ...

  8. 爪哇国新游记之十五----泛型动态数组类

    import java.lang.reflect.Array;/*** 泛型动态数组类**/ public class DynamicArray<T extends Object>{pri ...

  9. 爪哇国新游记之十六----泛型单链表类

    /*** 单链表节点类* @param <T>*/ class Node<T extends Object>{protected T value;protected Node ...

最新文章

  1. linux script $,linux – 这在shell脚本中是什么意思SCRIPTNAME =“${0 ## * /}”?
  2. Micropython 如何用Turnipbit做一个自动浇水装置
  3. 【转】01Teams的前世今生
  4. Xtrabackup备份与恢复
  5. gettimeofday_PHP gettimeofday()函数与示例
  6. 九龙擒庄指标源码破译_擒庄系列:庄家难逃该指标,散户屡试不爽的秘籍!(附公式)...
  7. ODrive踩坑(三)AS5047P磁编码器的ABI接口
  8. 计算机指令与运算基础原理笔记
  9. 关于终止多个for select循环嵌套的问题
  10. android跑马灯效果横向,Android自定义View实现纵向跑马灯效果详解
  11. Hive_HQL_复杂SQL_连续发单天数
  12. 人生若只如初见 何事秋风悲画扇
  13. PXC高可用集群(MySQL)
  14. H265(HEVC)视频下载 测试专用
  15. D2C-Net: A Dual-branch, Dual-guidance and Cross-refine Network for Camouflaged Object Detection阅读笔记
  16. JavaScript中linux时间戳与日期的转换
  17. char (*)[]无法传给参数char **
  18. Android display 显示流程
  19. 常见笔顺错误的字_那些我们易错笔画笔顺的常见字
  20. 网页html web直接打开cad文件dwg格式等的方法

热门文章

  1. 不插电的计算机科学百度云,【精品】不插电的计算机科学.pdf
  2. 简约好看的响应式app下载页面源码
  3. python在windows平台的多版本配置
  4. Win 7英文系统显示中文乱码的解决
  5. 教你怎么获得ICM会员ID解析.xxx域名(membership id icm)
  6. Spoonwep破解wep加密无线路由密码
  7. wordpress主题安装
  8. 苹果编程语言Swift中文教程:Swift简介
  9. 如何在终端下截取一个完整长度的网页截图
  10. magento 增加一个layout template