package src.com.atguigu.exer;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import org.junit.Test;
//客户端给服务端发送文本,服务端会将文本转成大写在返回给客户端。
//如下程序为了保证相应的流及socket的关闭(即使在关闭之前出现异常,也一定要保证相应的资源的关闭),要求是用
//try-catch-finally进行操作。要求将关闭的信息写在finally里!
public class TestTCP {
@Test
public void client() {
// 1.
Socket socket = null;
// 2.
OutputStream os = null;
Scanner scanner = null;
// 4.接收来自于服务端的数据
InputStream is = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
os = socket.getOutputStream();
// 3.向服务端发送数据
// os.write("abc".getBytes());
System.out.println("请输入多个字符:");
scanner = new Scanner(System.in);
String str = scanner.next();
os.write(str.getBytes());
socket.shutdownOutput();
is = socket.getInputStream();
byte[] b = new byte[10];
int len;
while ((len = is.read(b)) != -1) {
String str1 = new String(b, 0, len);
System.out.print(str1);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5.
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(scanner != null){
scanner.close();
}
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
@Test
public void server() {
// 1.
ServerSocket ss = null;
// 2.
Socket s = null;
// 3.接收来自于客户端的信息
InputStream is = null;
// 4.返回给客户端
OutputStream os = null;
try {
ss = new ServerSocket(9090);
s = ss.accept();
is = s.getInputStream();
byte[] b = new byte[10];
int len;
String str = new String();
while ((len = is.read(b)) != -1) {
String str1 = new String(b, 0, len);
str += str1;
}
String strUpperCase = str.toUpperCase();
os = s.getOutputStream();
os.write(strUpperCase.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ss != null){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// 5.
}
}
package src.com.atguigu.java;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface Human {
void info();
void fly();
}
// 被代理类
class SuperMan implements Human {
public void info() {
System.out.println(“我是超人!我怕谁!”);
}
public void fly() {
System.out.println("I believe I can fly!");
}
}
class HumanUtil {
public void method1() {
System.out.println("=方法一=");
}
public void method2() {
System.out.println("=======方法二=======");
}
}
class MyInvocationHandler implements InvocationHandler {
Object obj;// 被代理类对象的声明
public void setObject(Object obj) {
this.obj = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
HumanUtil h = new HumanUtil();
h.method1();
Object returnVal = method.invoke(obj, args);
h.method2();
return returnVal;
}
}
class MyProxy {
// 动态的创建一个代理类的对象
public static Object getProxyInstance(Object obj) {
MyInvocationHandler handler = new MyInvocationHandler();
handler.setObject(obj);
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
.getClass().getInterfaces(), handler);
}
}
public class TestAOP {
public static void main(String[] args) {
SuperMan man = new SuperMan();//创建一个被代理类的对象
Object obj = MyProxy.getProxyInstance(man);//返回一个代理类的对象
Human hu = (Human)obj;
hu.info();//通过代理类的对象调用重写的抽象方法
System.out.println();
hu.fly();
//*********
NikeClothFactory nike = new NikeClothFactory();
Object obj1 = MyProxy.getProxyInstance(nike);
ClothFactory cloth = (ClothFactory)obj1;
cloth.productCloth();
}
}
package src.com.atguigu.java;
//静态代理模式
//接口
interface ClothFactory{
void productCloth();
}
//被代理类
class NikeClothFactory implements ClothFactory{
@Override
public void productCloth() {
System.out.println("Nike工厂生产一批衣服");
}
}
//代理类
class ProxyFactory implements ClothFactory{
ClothFactory cf;
//创建代理类的对象时,实际传入一个被代理类的对象
public ProxyFactory(ClothFactory cf){
this.cf = cf;
}
@Override
public void productCloth() {
System.out.println("代理类开始执行,收代理费$1000");
cf.productCloth();
}
}
public class TestClothProduct {
public static void main(String[] args) {
NikeClothFactory nike = new NikeClothFactory();//创建被代理类的对象
ProxyFactory proxy = new ProxyFactory(nike);//创建代理类的对象
proxy.productCloth();
}
}
package src.com.atguigu.java1;
import java.net.InetAddress;
import java.net.UnknownHostException;
/*
- 网络通信的第一个要素:IP地址。通过IP地址,唯一的定位互联网上一台主机
- InetAddress:位于java.net包下
- 1.InetAddress用来代表IP地址。一个InetAdress的对象就代表着一个IP地址
- 2.如何创建InetAddress的对象:getByName(String host)
- 3.getHostName(): 获取IP地址对应的域名
- getHostAddress():获取IP地址
*/
public class TestInetAddress {
public static void main(String[] args) throws Exception {
//创建一个InetAddress对象:getByName()
InetAddress inet = InetAddress.getByName(“www.atguigu.com”);
//inet = InetAddress.getByName(“42.121.6.2”);
System.out.println(inet);
//两个方法
System.out.println(inet.getHostName());
System.out.println(inet.getHostAddress());
//获取本机的IP:getLocalHost()
InetAddress inet1 = InetAddress.getLocalHost();
System.out.println(inet1);
System.out.println(inet1.getHostName());
System.out.println(inet1.getHostAddress());
}
}
package src.com.atguigu.java1;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
//TCP编程例一:客户端给服务端发送信息。服务端输出此信息到控制台上
//网络编程实际上就是Socket的编程
public class TestTCP1 {
// 客户端
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
try {
// 1.创建一个Socket的对象,通过构造器指明服务端的IP地址,以及其接收程序的端口号
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
// 2.getOutputStream():发送数据,方法返回OutputStream的对象
os = socket.getOutputStream();
// 3.具体的输出过程
os.write("我是客户端,请多关照".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 4.关闭相应的流和Socket对象
if (os != null) {
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 服务端
@Test
public void server() {
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
try {
// 1.创建一个ServerSocket的对象,通过构造器指明自身的端口号
ss = new ServerSocket(9090);
// 2.调用其accept()方法,返回一个Socket的对象
s = ss.accept();
// 3.调用Socket对象的getInputStream()获取一个从客户端发送过来的输入流
is = s.getInputStream();
// 4.对获取的输入流进行的操作
byte[] b = new byte[20];
int len;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len);
System.out.print(str);
}
System.out.println("收到来自于" + s.getInetAddress().getHostAddress()
+ "的连接");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5.关闭相应的流以及Socket、ServerSocket的对象
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (s != null) {
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package src.com.atguigu.java1;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import org.junit.Test;
//
//TCP编程例二:客户端给服务端发送信息,服务端将信息打印到控制台上,同时发送“已收到信息”给客户端
public class TestTCP2 {
//客户端
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
InputStream is = null;
try {
socket = new Socket(InetAddress.getByName(“127.0.0.1”),8989);
os = socket.getOutputStream();
os.write(“我是客户端”.getBytes());
//shutdownOutput():执行此方法,显式的告诉服务端发送完毕!
socket.shutdownOutput();
is = socket.getInputStream();
byte[] b = new byte[20];
int len;
while((len = is.read(b)) != -1){
String str = new String(b,0,len);
System.out.print(str);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void server(){
ServerSocket ss = null;
Socket s = null;
InputStream is = null;
OutputStream os = null;
try {
ss = new ServerSocket(8989);
s = ss.accept();
is = s.getInputStream();
byte[] b = new byte[20];
int len;
while((len = is.read(b)) != -1){
String str = new String(b,0,len);
System.out.print(str);
}
os = s.getOutputStream();
os.write("我已收到你的情意".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(os != null){
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(s != null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ss != null){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
package src.com.atguigu.java1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import org.junit.Test;
//TCP编程例三:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。并关闭相应的连接。
//如下的程序,处理异常时,要使用try-catch-finally!!本例仅为了书写方便~
public class TestTCP3 {
@Test
public void client()throws Exception{
//1.创建Socket的对象
Socket socket = new Socket(InetAddress.getByName(“127.0.0.1”), 9898);
//2.从本地获取一个文件发送给服务端
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File(“1.png”));
byte[] b = new byte[1024];
int len;
while((len = fis.read(b)) != -1){
os.write(b,0,len);
}
socket.shutdownOutput();
//3.接收来自于服务端的信息
InputStream is = socket.getInputStream();
byte[] b1 = new byte[1024];
int len1;
while((len1 = is.read(b1)) != -1){
String str = new String(b1,0,len1);
System.out.print(str);
}
//4.关闭相应的流和Socket对象
is.close();
os.close();
fis.close();
socket.close();
}
@Test
public void server() throws Exception{
//1.创建一个ServerSocket的对象
ServerSocket ss = new ServerSocket(9898);
//2.调用其accept()方法,返回一个Socket的对象
Socket s = ss.accept();
//3.将从客户端发送来的信息保存到本地
InputStream is = s.getInputStream();
FileOutputStream fos = new FileOutputStream(new File(“1.png”));
byte[] b = new byte[1024];
int len;
while((len = is.read(b)) != -1){
fos.write(b, 0, len);
}
System.out.println(“收到来自于” + s.getInetAddress().getHostAddress() + “的文件”);
//4.发送"接收成功"的信息反馈给客户端
OutputStream os = s.getOutputStream();
os.write(“你发送的图片我已接收成功!”.getBytes());
//5.关闭相应的流和Socket及ServerSocket的对象
os.close();
fos.close();
is.close();
s.close();
ss.close();
}
}
package src.com.atguigu.java1;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import org.junit.Test;
//UDP编程的实现
public class TestUDP {
// 发送端
@Test
public void send() {
DatagramSocket ds = null;
try {
ds = new DatagramSocket();
byte[] b = "你好,我是要发送的数据".getBytes();
//创建一个数据报:每一个数据报不能大于64k,都记录着数据信息,发送端的IP、端口号,以及要发送到
//的接收端的IP、端口号。
DatagramPacket pack = new DatagramPacket(b, 0, b.length,
InetAddress.getByName("127.0.0.1"), 9090);
ds.send(pack);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(ds != null){
ds.close();
}
}
}
// 接收端
@Test
public void receive() {
DatagramSocket ds = null;
try {
ds = new DatagramSocket(9090);
byte[] b = new byte[1024];
DatagramPacket pack = new DatagramPacket(b, 0, b.length);
ds.receive(pack);
String str = new String(pack.getData(), 0, pack.getLength());
System.out.println(str);
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(ds != null){
ds.close();
}
}
}
}
package src.com.atguigu.java1;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
//URL:统一资源定位符,一个URL的对象,对应着互联网上一个资源。
//我们可以通过URL的对象调用其相应的方法,将此资源读取(“下载”)
public class TestURL {
public static void main(String[] args) throws Exception {
//1.创建一个URL的对象
URL url = new URL(“http://127.0.0.1:8080/examples/HelloWorld.txt?a=b”);//File file = new File(“文件的路径”);
/*
* public String getProtocol( ) 获取该URL的协议名
public String getHost( ) 获取该URL的主机名
public String getPort( ) 获取该URL的端口号
public String getPath( ) 获取该URL的文件路径
public String getFile( ) 获取该URL的文件名
public String getRef( ) 获取该URL在文件中的相对位置
public String getQuery( ) 获取该URL的查询名
*/
// System.out.println(url.getProtocol());
// System.out.println(url.getHost());
// System.out.println(url.getPort());
// System.out.println(url.getFile());
// System.out.println(url.getRef());
// System.out.println(url.getQuery());
//如何将服务端的资源读取进来:openStream()
InputStream is = url.openStream();
byte[] b = new byte[20];
int len;
while((len = is.read(b)) != -1){
String str = new String(b,0,len);
System.out.print(str);
}
is.close();
//如果既有数据的输入,又有数据的输出,则考虑使用URLConnection
URLConnection urlConn = url.openConnection();
InputStream is1 = urlConn.getInputStream();
FileOutputStream fos = new FileOutputStream(new File(“abc.txt”));
byte[] b1 = new byte[20];
int len1;
while((len1 = is1.read(b1)) != -1){
fos.write(b1, 0, len1);
}
fos.close();
is1.close();
}
}
package src.com.atguigu.review;
public class Animal {
private String name;
public int age;
static String desc = “我是一个动物”;
public Animal() {
super();
System.out.println("!!!");
}
private Animal(String name, int age) {
super();
this.name = name;
this.age = age;
}
public static void info(){
System.out.println("动物");
}
public void show(String desc){
System.out.println("我是一个:" + desc);
}
private int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static String getDesc() {
return desc;
}
public static void setDesc(String desc) {
Animal.desc = desc;
}
@Override
public String toString() {
return "Animal [name=" + name + ", age=" + age + "]";
}
}
package src.com.atguigu.review;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.Test;
public class TestReflection {
//调用指定的方法
@Test
public void test4() throws Exception{
Class clazz = Class.forName("com.atguigu.review.Animal");
Object obj = clazz.newInstance();
Animal a = (Animal)obj;
//调用非public的方法
Method m1 = clazz.getDeclaredMethod("getAge");
m1.setAccessible(true);
int age = (Integer)m1.invoke(a);
System.out.println(age);
//调用public的方法
Method m2 = clazz.getMethod("show", String.class);
Object returnVal = m2.invoke(a,"金毛");
System.out.println(returnVal);
//调用static的方法
Method m3 = clazz.getDeclaredMethod("info");
m3.setAccessible(true);
// m3.invoke(Animal.class);
m3.invoke(null);
}
//调用指定属性
@Test
public void test3() throws Exception{
Class clazz = Class.forName("com.atguigu.review.Animal");
Object obj = clazz.newInstance();
Animal a = (Animal)obj;
//调用非public的属性
Field f1 = clazz.getDeclaredField("name");
f1.setAccessible(true);
f1.set(a, "Jerry");
//调用public的属性
Field f2 = clazz.getField("age");
f2.set(a, 9);
System.out.println(f2.get(a));
System.out.println(a);
//调用static的属性
Field f3 = clazz.getDeclaredField("desc");
System.out.println(f3.get(null));
}
//调用指定的构造器创建运行时类的对象
@Test
public void test2() throws Exception{
Class clazz = Animal.class;
Constructor cons = clazz.getDeclaredConstructor(String.class,int.class);
cons.setAccessible(true);
Animal a = (Animal)cons.newInstance("Tom",10);
System.out.println(a);
}
//获取运行时类的对象:方法一
@Test
public void test1() throws Exception{
Class clazz = Class.forName("com.atguigu.review.Animal");
Object obj = clazz.newInstance();
Animal a = (Animal)obj;
System.out.println(a);
}
}