下载网络文件到本地的方法

public static void main(String[] args) {
	BufferedOutputStream bos = null;
	BufferedInputStream bis = null;
	try {
		URL url = new URL("http://www.baidu.com/img/baidu_logo.gif");
		URLConnection conn = url.openConnection();
		bis = new BufferedInputStream(conn.getInputStream());
		FileOutputStream fos = new FileOutputStream(new File("c:/", "logo.gif"));
		bos = new BufferedOutputStream(fos);
		byte[] buf = new byte[2097152];
		int len = -1;
		while ((len = bis.read(buf)) != -1) {
			bos.write(buf, 0, len);
		}
	} catch (IOException e) {
		e.printStackTrace();
	} finally {
		try {
			if (null != bis) {
				bis.close();
			}
			if (null != bos) {
				bos.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

你可能感兴趣的:(C++,c,C#)