前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java url抓取文件到本地

java url抓取文件到本地

作者头像
葫芦
发布2019-05-09 11:43:30
1.6K0
发布2019-05-09 11:43:30
举报
文章被收录于专栏:葫芦葫芦
代码语言:javascript
复制
package socket;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

public class Url {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
URL url=new URL("http://www.liezi.net/wp-content/uploads/2014/02/bailai1.mp3");
System.out.println(url.getHost());
System.out.println(url.getPath());
System.out.println(url.getPort());
System.out.println(url.getProtocol());
Date date=new Date();
System.out.println(date);

String filename=url.getPath().substring(url.getPath().lastIndexOf("/"));
URLConnection conn=url.openConnection();
BufferedInputStream bis=new BufferedInputStream(conn.getInputStream());
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("c:\\test"+filename));
byte[] bytes=new byte[1024*100];
int len=-1;
int i=1;
while((len=bis.read(bytes))!=-1){
bos.write(bytes,0,len);
bos.flush();

i++;
System.out.println(i);
}
bos.close();
bis.close();
System.out.println("downloaded");
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014/03/02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档