前言:

本文内容:TCP文件上传实现、初识Tomcat、UDP消息发送

推荐免费Java网络编程实战讲解视频:【狂神说Java】网络编程实战讲解_哔哩哔哩_bilibili

TCP文件上传实现

文件上传

客户端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.jokerdig.netDemo;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

/**
* @author Joker大雄
* @data 2021/9/3 - 19:04
**/
//输出端
public class TcpDemo02 {
public static void main(String[] args) throws Exception{
//创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//创建一个输出流
OutputStream out = socket.getOutputStream();
//文件流
FileInputStream fileInputStream = new FileInputStream(new File("OIP.jpg"));
//写出文件
byte[] buffer = new byte[1024];
int len;
while((len=fileInputStream.read(buffer))!=-1){
out.write(buffer,0,len);
}

//通知服务器,已经传输完了
socket.shutdownOutput();//传输完毕

//确定服务器接收完毕,才能断开连接
InputStream inputStream = socket.getInputStream();
//String byte[]
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[2014];
int len1;
while((len1=inputStream.read(bytes))!=-1){
byteArrayOutputStream.write(bytes,0,len1);
}
System.out.println(byteArrayOutputStream.toString());
//关闭资源
fileInputStream.close();
out.close();
socket.close();

}
}

服务端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.jokerdig.netDemo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
* @author Joker大雄
* @data 2021/9/2 - 19:33
**/
//服务端
public class TcpServerDemo {
public static void main(String[] args) {
ByteArrayOutputStream byteStream = null;
InputStream inputStream = null;
ServerSocket serverSocket = null;
Socket socket = null;
try {
//我得有一个地址
serverSocket = new ServerSocket(9988);
//等待客户端连接过来
socket = serverSocket.accept();
//读取客户端消息
inputStream = socket.getInputStream();
/* int len;
while((len=inputStream.read(buffer))!=1){
String msg= new String(buffer,0,len);
System.out.println(msg);
//这样中文进来会乱码
}
*/
//管道流
byteStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
byteStream.write(buffer, 0, len);
}
System.out.println(byteStream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//释放资源
if (byteStream != null) {
byteStream.close();
}
if (inputStream != null) {
inputStream.close();
}
if (socket != null) {
socket.close();
}
if (serverSocket != null) {
serverSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

初识Tomcat

Tomcat下载

服务端

  • 自定义S
  • Tomcat服务器

客户端

  • 自定义C
  • 浏览器B

解决服务器运行乱码:

​ Tomcat安装目录>conf>logging文件

1
2
3
4
#将该处UTF-8改为GBK
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter
java.util.logging.ConsoleHandler.encoding = GBK

UDP消息发送

发短信,不用连接,需要知道对方的地址。

发送端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.jokerdig.netDemo;

import java.io.IOException;
import java.net.*;

/**
* @author Joker大雄
* @data 2021/9/3 - 20:01
**/
//不需要连接服务器
public class UdpDemo3 {
public static void main(String[] args) throws IOException {
//建立一个socket
DatagramSocket datagramSocket = new DatagramSocket();
//建个包
String manage="你好,服务器!";
//发送的地址
InetAddress localhost = InetAddress.getByName("localhost");
int port =9090;
//数据 数据的长度起始,发送给谁
DatagramPacket packet = new DatagramPacket(manage.getBytes(),0, manage.getBytes().length, localhost, port);
//发送包
datagramSocket.send(packet);
//释放资源
datagramSocket.close();
}
}

接收端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.jokerdig.netDemo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
* @author Joker大雄
* @data 2021/9/3 - 20:08
**/
//还要等待客户端的连接
public class UdpDemo3_1 {
public static void main(String[] args) throws IOException {
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据包
byte[] buffer = new byte[1024];
int len;
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);//阻塞数据
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
//释放资源
socket.close();
}
}