-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClienttcp5.java
More file actions
51 lines (40 loc) · 1.29 KB
/
Clienttcp5.java
File metadata and controls
51 lines (40 loc) · 1.29 KB
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
package com.cpucode.java;
import java.io.*;
import java.net.Socket;
public class Clienttcp5 {
public static void main(String[] args) throws IOException {
// 1.创建流对象
// 1.1 创建输入流,读取本地文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test.jpg"));
// // 1.2 创建输出流,写到服务端
Socket socket = new Socket("localhost", 666);
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
//.写出数据
byte[] b = new byte[1024 * 8];
int len;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
bos.flush();
}
// 关闭输出流,通知服务端,写出数据完毕
socket.shutdownOutput();
System.out.println("文件发送完毕");
// 3. =====解析回写============
InputStream in = socket.getInputStream();
byte[] back = new byte[20];
in.read(back);
System.out.println(new String(back));
in.close();
//=============
// 关闭资源
bos.close();
socket.close();
bis.close();
System.out.println("文件上传完毕 ");
}
}
/*
文件发送完毕
上传成功
文件上传完毕
*/