爪哇国新游记之二十八----从url指定的地址下载文件到本地
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
}
}
}
}
爪哇国新游记之二十八----从url指定的地址下载文件到本地的更多相关文章
- 爪哇国新游记之二十九----访问URL获取输入流
代码: import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import ...
- 爪哇国新游记之二十二----排序判断重复时间复杂度为2n的位图法
import java.util.ArrayList; import java.util.List; /** * 位图法 * 用于整型数组判重复,得到无重复列表 * */ public class B ...
- 爪哇国新游记之二----用于计算三角形面积的Point类和TAngle类
这次尝试用两个类完成一个面积计算任务: Point类代表平面上的点: public class Point { private float x; private float y; public Poi ...
- 爪哇国新游记之三十四----Dom4j的XPath操作
Dom4j是Java访问XML的利器之一,另一个是JDom.记得当年因为粗掌握点JDomAPI但项目要求使用Dom4j还闹一阵情绪,现在看来真是没必要,只花一些时间成本就进去一个新世界绝对是值得做的一 ...
- 爪哇国新游记之十四----初试JDBC
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...
- 爪哇国新游记之十九----使用Stack检查数字表达式中括号的匹配性
/** * 辅助类 * 用于记载字符和位置 * */ class CharPos{ char c; int pos; public CharPos(char c,int pos){ this.c=c; ...
- 爪哇国新游记之十三----XML文件读写
/** * XML读写示例 * @author hx * */ public class XmlReaderWriter{ /** * 读取一个XML文件,返回一个雇员链表 * @param file ...
- 爪哇国新游记之七----使用ArrayList统计水果出现次数
之前学习制作了DArray,了解ArrayList就容易了. /** * 用于存储水果名及数量 * */ public class Fruit{ private String name; public ...
- 爪哇国新游记之一----第一个类Cube
将这个类作为Java学习的第一个类,简单易懂易上手. /** * 正方体类 */ public class Cube { private int length;// 正方体边长 private sta ...
随机推荐
- mybatis insert oracle 返回主键
mybtis返回oracle主键 只需要加一点代码(红色处的代码)就可以了 <!-- 添加记录到临时表 --> <insert id="insertPlaneStateme ...
- V-Hyper安装ubuntu-13.10-server-amd64
1.在windws8上的V_Hyper虚拟机上安装Ubuntu虚拟机服务器版.遇到的问题和解决方案 2.正确的在V-Hyper配置方法参考文章:在Hyper-V中安装和配置Ubuntu Server ...
- Selenium2+python自动化25-js处理日历控件(修改readonly属性)【转载】
前言 日历控件是web网站上经常会遇到的一个场景,有些输入框是可以直接输入日期的,有些不能,以我们经常抢票的12306网站为例,详细讲解如何解决日历控件为readonly属性的问题. 基本思路:先用j ...
- seneca的一段代码(原创)
var seneca=require('seneca')() seneca.add({cmd:'wordcount'},function(msg,respond){ var length=0; if( ...
- python算法:LinkedList(双向线性链表)的实现
LinkedList是一个双向线性链表,但是并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针(Pointer).由于不必须按顺序存储,链表在插入的时候可以达到O(1)的复杂度,比另一 ...
- processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(“ 132501”).active().singleResult();
JAVA: processEngine.getRuntimeService().createProcessInstanceQuery().processInstanceId(“ 132501”).ac ...
- *****git pull总结
当git clone之后,直接git pull它会自动匹配一个正确的remote url 是因为在config文件中配置了以下内容: 1 [branch "master"] 2 r ...
- Tomcat 服务器基本知识
Tomcat下载安装和配置 下载 下载地址: http://tomcat.apache.org tomcat服务器分为很多版本, 其中包括windows版和linux版 ...
- magento 开启模板路径提示
1.进入后台system->configuration->,选择main Website 2.advanced->developer中,将Debug中的Template Path H ...
- Unity防破解 —— 重新编译mono
Unity4.x版本导出android包时,只能选择mono,无法使用il2cpp,这就造成了我们的程序集很容易被修改--很多朋友在发布项目时觉得即使代码暴露出去也没什么关系,只有项目火了才有 ...