首页

通过配置URL及下载路径实现二进制jar包及相关配置pom等文件的网络下载

标签:FileOutputStream,BufferedOutputStream,BufferedInputStream,离线下载,URL,网络,java     发布时间:2017-02-28   

通过配置URL及下载路径实现在线jar包及相关pom、xml等文件网络下载,主要通过java.net.URL、java.io.BufferedInputStream、java.io.BufferedOutputStream及java.io.FileOutputStream网络包类,示例如下所示

import java.io.BufferedInputStream;@b@import java.io.BufferedOutputStream;@b@import java.io.File;@b@import java.io.FileOutputStream;@b@import java.net.URL;@b@@b@public class DownLoadHandler{@b@	@b@	private String url;@b@	private String out_path;@b@@b@	public DownLoadHandler(String url, String out_path) {@b@		super();@b@		this.url = url;@b@		this.out_path = out_path;@b@	}@b@@b@@b@	public  void  execute(){@b@		try {@b@			File f = new File(out_path);@b@			if (f.exists())@b@				f.delete();@b@			@b@			URL _url = new URL(url); @b@	        @b@	        BufferedInputStream bis = new BufferedInputStream(_url.openStream()); @b@	        FileOutputStream out=new FileOutputStream(out_path);@b@	        BufferedOutputStream bos = new BufferedOutputStream(out);  @b@	        @b@	        byte buf[] = new byte[10000];  @b@	        int len;  @b@	        while((len=bis.read(buf)) != -1){  @b@	            bos.write(buf, 0, len);  @b@	            System.out.println(new String(buf, 0, len));  @b@	        }  @b@	        bos.close();  @b@	        out.close();  @b@	        bis.close(); @b@		} catch (Exception e) {@b@			e.printStackTrace();@b@		}@b@	}@b@@b@@b@	public static void main(String[] args) {@b@		new DownLoadHandler("http://xwood.net:8081/nexus/content/groups/public/asc-common/maven-metadata-local.xml","C:/WS/NJ/test/maven-metadata-local.xml").execute();@b@		new DownLoadHandler("http://xwood.net:8081/nexus/content/groups/public/ant/ant/1.7.0/m2e-lastUpdated.properties","C:/WS/NJ/test/m2e-lastUpdated.properties").execute();@b@		new DownLoadHandler("http://xwood.net:8081/nexus/content/groups/public/ant/ant/1.7.0/ant-1.7.0.jar","C:/WS/NJ/test/ant-1.7.0.jar").execute();@b@	}@b@	@b@@b@}

效果如下所示,可以正常下载jar包内容

通过配置URL及下载路径实现二进制jar包及相关配置pom等文件的网络下载