"java 网络编程"简单总结_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > "java 网络编程"简单总结

"java 网络编程"简单总结

 2013/11/28 15:38:54  fangguanhong  程序员俱乐部  我要评论(0)
  • 摘要:packagecom.shengshiyuan.network;importjava.io.FileOutputStream;importjava.io.InputStream;importjava.io.OutputStream;importjava.net.URL;importjava.net.URLConnection;publicclassUrlConnection1{publicstaticvoidmain(String[]args)throwsException
  • 标签:总结 Java 网络 编程
class="java" name="code">package com.shengshiyuan.network;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnection1 {
	public static void main(String[] args) throws Exception {
		URL url = new URL("http://www.infoq.com");

		URLConnection conn = url.openConnection();

		InputStream is = conn.getInputStream();

		OutputStream os = new FileOutputStream("c:\\infoq.txt");

		byte[] buffer = new byte[1024];

		int length = 0;

		while (-1 != (length = is.read(buffer, 0, buffer.length))) {
			os.write(buffer, 0, length);
		}

		is.close();
		os.close();
	}
}


package com.shengshiyuan.network;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnection2 {
	public static void main(String[] args) throws Exception {
		URL url = new URL("http://www.qq.com");

		// URLConnection conn = url.openConnection();
		//
		// InputStream is = conn.getInputStream();
		// 更简单的方式
		InputStream is = url.openStream();

		OutputStream os = new FileOutputStream("c:\\infoq.txt");

		byte[] buffer = new byte[1024];

		int length = 0;

		while (-1 != (length = is.read(buffer, 0, buffer.length))) {
			os.write(buffer, 0, length);
		}

		is.close();
		os.close();
	}
}



具体详细讲解课件见附件(上课讲解用pdf文件):
  • network.zip (1.2 MB)
  • 下载次数: 0
发表评论
用户名: 匿名