day29
字符集
ASCII
ISO8859-1
GB2312
GBK
Unicode
UTF-8
网络编程
package shangGuiGu.day29;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class demo01 {
public static void main(String[] args) throws UnknownHostException {
InetAddress byName = InetAddress.getByName("192.168.10.14");
System.out.println(byName);
InetAddress byName1 = InetAddress.getByName("www.atguigu.com");
System.out.println(byName1);
InetAddress byName2 = InetAddress.getByName("127.0.0.1");//本机地址
System.out.println(byName2);
//获取本机ip
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
System.out.println(byName2.getHostAddress());
System.out.println(byName2.getHostName());
}
}
package shangGuiGu.day29;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
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;
public class demo02 {
//客户端
@Test
public void test() throws IOException {
Socket socket= null;
OutputStream os= null;
try {
//1.创建Socket对象,指明服务器端的ip和端口号
InetAddress inet=InetAddress.getByName("127.0.0.1");
socket = new Socket(inet,8899);
//2.获取一个输出流,用于输出数据
os = socket.getOutputStream();
//3.写出数据的操作
os.write("hello".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.资源的关闭
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//服务端
}
@Test
public void test2() {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
ByteArrayOutputStream baos= null;
try {
serverSocket = new ServerSocket(8899);
accept = serverSocket.accept();
inputStream = accept.getInputStream();
// byte[] buffer=new byte[20];
// int len;
// while((len=inputStream.read(buffer))!=-1){
// String str=new String(buffer,0,len);
// System.out.println(str);
// }
baos = new ByteArrayOutputStream();
byte[] buffer=new byte[5];
int len;
while((len=inputStream.read(buffer))!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
System.out.println(serverSocket.getInetAddress().getHostAddress());
} catch (IOException e) {
e.printStackTrace();
} finally {
if(baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(serverSocket!=null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(accept!=null){
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package shangGuiGu.day29;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class demo03 {
@Test
public void test()throws IOException {
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
OutputStream os=socket.getOutputStream();
FileInputStream fis=new FileInputStream(new File("C:\\Users\\王誉潼\\IdeaProjects\\javase\\src\\shangGuiGu\\day29\\d2.jpg"));
byte[] buffer=new byte[1024];
int len;
while((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
fis.close();
os.close();
socket.close();
}
@Test
public void test2() throws IOException {
ServerSocket ss = new ServerSocket((9090));
Socket socket = ss.accept();
InputStream is=socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\王誉潼\\IdeaProjects\\javase\\src\\shangGuiGu\\day29\\d2.jpg"));
byte[] buffer=new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
is.close();
socket.close();
}
}
URL类
统一资源定位符,对应着互联网的某一资源地址
协议://主机名 端口号 资源地址 参数列表
package shangGuiGu.day29;
import org.junit.Test;
import java.net.MalformedURLException;
import java.net.URL;
public class demo04 {
public static void main(String[] args) {
try{
URL url = new URL("https://discovery.lenovo.com.cn/home/baidu/v1/c2");
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println(url.getQuery());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
反射
package shangGuiGu.day29;
public class demo05 {
private String name;
public int age;
public demo05() {
}
public demo05(String name, int age) {
this.name = name;
this.age = age;
}
private demo05(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void show(){
System.out.println("hello");
}
private String shownation(String nation){
System.out.println(nation);
return nation;
}
@Override
public String toString() {
return "demo05{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
package shangGuiGu.day29;
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class demo06 {
@Test
public void test(){
demo05 p1=new demo05("Tom",12);
p1.age=10;
System.out.println(p1.toString());
p1.show();
}
@Test
public void test2() throws Exception{
Class clazz=demo05.class;
Constructor constructor = clazz.getConstructor(String.class, int.class);
Object obj = constructor.newInstance("Tom", 12);
demo05 p=(demo05)obj;
System.out.println(p.toString());
Field age = clazz.getDeclaredField("age");
age.set(p,10);
System.out.println(p.toString());
Method show = clazz.getDeclaredMethod("show");
show.invoke(p);
//通过反射,可以调用私有的结构
Constructor cons1 = clazz.getDeclaredConstructor(String.class);
cons1.setAccessible(true);
demo05 p1=(demo05)cons1.newInstance("jerry");
System.out.println(p1);
Field name = clazz.getDeclaredField("name");
name.setAccessible(true);
name.set(p1,"han");
System.out.println(p1);
Method shownation = clazz.getDeclaredMethod("shownation", String.class);
shownation.setAccessible(true);
shownation.invoke(p1,"china");
System.out.println(shownation);
}
}
package shangGuiGu.day29;
public class demo05 {
private String name;
public int age;
public demo05() {
}
public demo05(String name, int age) {
this.name = name;
this.age = age;
}
private demo05(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void show(){
System.out.println("hello");
}
private String shownation(String nation){
System.out.println(nation);
return nation;
}
@Override
public String toString() {
return "demo05{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}