-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
134 lines (123 loc) · 3.43 KB
/
Client.java
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Client {
private Socket s;
public String dir = "./data/";
public String name = "temp";
public String sendname = "toBeSent";
private DataInputStream ctrli;
private DataOutputStream ctrlo;
private BufferedInputStream sendi;
private DataOutputStream sendo;
private DataInputStream geti;
private BufferedOutputStream geto;
private SimpleDateFormat sdf;
public Client() {
try {
s = new Socket("127.0.0.1", 8848);
ctrlo = new DataOutputStream(s.getOutputStream());
ctrli = new DataInputStream(s.getInputStream());
sdf = new SimpleDateFormat();
sdf.applyPattern("yyyy_MM_dd-HH_mm_ss");
} catch (UnknownHostException e) {
System.out.println("UnknownHostException!");
// e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException!");
// e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
}
public void sendFile(String filename) throws IOException {
// 用于读取指定文件并上传
// 函数接受参数为文件名,不包含路径。
try {
Date date = new Date();
String targetName = sdf.format(date);
sendo = new DataOutputStream(s.getOutputStream());
// 向服务器端发送一条消息
File f = new File(filename);
if (!f.exists()) {
System.out.println("文件不存在!");
return;
}
// 发送标记信息
ctrlo.writeInt(1);
// 发送文件信息
ctrlo.writeUTF(targetName);
ctrlo.flush();
ctrlo.writeLong(f.length());
ctrlo.flush();
System.out.println("---Start sending " + targetName + ".---");
// 读取本地文件
sendi = new BufferedInputStream(new FileInputStream(f));
byte b[] = new byte[1024];
int length = 0;
long progress = 0;
while ((length = sendi.read(b)) != -1) {
sendo.write(b);
sendo.flush();
progress += length;
System.out.println("" + (int) (100 * progress / f.length()) + "%");
}
System.out.println("---" + targetName + "\tsent successfully.---");
} catch (UnknownHostException e) {
System.out.println("UnknownHostException!");
// e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException!");
// e.printStackTrace();
} finally {
if (sendi != null)
sendi.close();
if (sendo != null)
sendo.close();
s.close();
}
}
public void getFile(String filename) throws IOException {
// 下载指定文件并保存至本地
// 函数接受参数为文件名,不包含路径。
try {
ctrlo.writeInt(2);
ctrlo.writeUTF(filename);
File f = new File(this.dir + this.name);
geto = new BufferedOutputStream(new FileOutputStream(f));
geti = new DataInputStream(s.getInputStream());
byte[] bytes = new byte[1024];
while ((geti.read(bytes, 0, bytes.length)) != -1) {
geto.write(bytes);
geto.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (geti != null)
geti.close();
if (geto != null)
geto.close();
s.close();
}
}
public String[] getList() throws IOException {
try {
ctrlo.writeInt(0);
Integer n = ctrli.readInt();
String[] str = new String[n];
for (int i = 0; i < n; i++) {
String temp = ctrli.readUTF();
str[i] = temp;
}
return str;
} catch (IOException e) {
System.out.println("读取列表失败!!!");
return null;
} finally {
s.close();
}
}
}