Java下载url文件方法
2009-09-06 01:15
232 查看
import java.net.*;
import java.io.*;
public class Download{
private URL u = null;
private String file_name ;
BufferedInputStream bis= null;
FileOutputStream fos = null;
public Download(){}
public Download(String p){
try{
u = new URL(p);
}
catch(MalformedURLException mue){
}
getName(p);
}
private void getName(String path){
this.file_name = "./"+path.substring(path.lastIndexOf('/'));
if(this.file_name.length() == 0){
System.out.println("no file name!");
System.exit(0);
}
}
public void down(){
try{
bis = new BufferedInputStream(u.openStream());
fos = new FileOutputStream(this.file_name);
byte [] content = new byte [1000];
int size = 0;
while((size = bis.read(content)) != -1){
fos.write(content,0,size);
}
}catch(FileNotFoundException ffe){
}catch(IOException ioe){
}finally{
try{
if(bis != null){
bis.close();
}
if(fos != null){
fos.close();
}
}catch(Exception e){}
}
}
}
public class Test{
public static void main(String [] args){
Download d = new Download("http://www.google.cn/intl/zh-CN/images/logo_cn.gif");
d.down();
}
}
来自“:。。。。
import java.io.*;
public class Download{
private URL u = null;
private String file_name ;
BufferedInputStream bis= null;
FileOutputStream fos = null;
public Download(){}
public Download(String p){
try{
u = new URL(p);
}
catch(MalformedURLException mue){
}
getName(p);
}
private void getName(String path){
this.file_name = "./"+path.substring(path.lastIndexOf('/'));
if(this.file_name.length() == 0){
System.out.println("no file name!");
System.exit(0);
}
}
public void down(){
try{
bis = new BufferedInputStream(u.openStream());
fos = new FileOutputStream(this.file_name);
byte [] content = new byte [1000];
int size = 0;
while((size = bis.read(content)) != -1){
fos.write(content,0,size);
}
}catch(FileNotFoundException ffe){
}catch(IOException ioe){
}finally{
try{
if(bis != null){
bis.close();
}
if(fos != null){
fos.close();
}
}catch(Exception e){}
}
}
}
public class Test{
public static void main(String [] args){
Download d = new Download("http://www.google.cn/intl/zh-CN/images/logo_cn.gif");
d.down();
}
}
来自“:。。。。
相关文章推荐
- Java方法中 访问url 下载链接,下载文件
- tomcat下载文件时,URL含有文字参数与空格参数出错(java.io.FileNotFoundException错误) 解决方法(转)
- java通过url下载文件并输出的方法
- JAVA通过HttpURLConnection 上传和下载文件的方法
- Java Spring MVC 上传下载文件配置及controller方法详解
- java 从网络Url中下载文件 (转)
- java根据URL地址下载文件
- Java Web端程序实现文件下载的方法分享
- 经过证实的下载文件java实现方法
- 【Java】上传文件进度条的实现方法(附demo源码下载)
- java mongodb 文件上传,下载,删除方法
- JAVA通过访问页面中的URL实现Http文件下载到本地
- java实现文件导出和下载方法
- [Java]远程下载文件并读取实例方法
- java 从网络Url中下载文件
- 将java类成为可执行文件.exe文件的一些方法、软件、下载地址
- java 多线程下载文件 以及URLConnection和HttpURLConnection的区别
- java 从网络Url中下载文件
- java 从网络Url中下载文件
- java后台批量下载文件并压缩成zip下载的方法