-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerPro.java
349 lines (310 loc) · 9.57 KB
/
ServerPro.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package fileTransfer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ServerPro {
private ExecutorService threadPool = null;
private int PORT = 10002;
private String DIR = "C:\\Users\\shiweigang\\Desktop\\";
public ServerPro(int port, String dirPath){
threadPool = Executors.newFixedThreadPool(15);
this.PORT = port;
this.DIR = dirPath;
}
public void execute(){
System.out.println("start to accept new request...");
ServerSocket server = null;
Socket socket = null;
long sT = System.currentTimeMillis();
int count = 0;
try{
server = new ServerSocket(PORT);
server.setSoTimeout(10000);
System.out.print("receiving...");
while(true){
socket = server.accept();
System.out.println("starting to receive "+(++count)+"-th task");
System.out.println("task "+count+" has pushed queue!");
threadPool.execute(new ServerHandler(socket));
Thread.sleep(1);
}
}catch(Exception e){
if(e instanceof SocketTimeoutException){
System.err.println("waiting threads...");
threadPool.shutdown();
try {
if(threadPool.awaitTermination(180, TimeUnit.SECONDS)){
if(count >= 1){
System.out.println("files received successfully!");
}
System.err.println("it costed total time is :"+(System.currentTimeMillis()-sT));
System.exit(0);
}
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else{
System.err.print(e);
}
}finally{
if(server != null){
try {
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.exit(0);
}
}
class ServerHandler implements Runnable{
private Socket socket;
private byte startOver = 0;
private byte flag = 0;
private int index = 0;
private long remainFileLength = 0L;
private int saveLength = 0;
private int nextLength = 0;
private FileOutputStream out = null;
private InputStream in = null;
public ServerHandler(Socket socket){
this.socket = socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
int size = 5094;
byte [] buff = new byte[size];
byte [] saveBuff = new byte[size];
int length = 0;
try {
in = socket.getInputStream();
while((length = in.read(buff)) != -1){
copyFile(buff, saveBuff, 0, length, DIR);
}
if(out != null){
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void copyFile(byte [] buff, byte [] saveBuff, int off, int len, String parentPath) throws IOException{
if(startOver != 3 || saveLength == 0){
if(off == len)return;
}
index = off;
if(startOver == 0){
startOver = 1;
flag = buff[index++];
if(flag == 0 || flag == 1){
nextLength = 4;
saveLength = 0;
if(index == len)return;
if(len - index >= 4){
nextLength = 0;
System.arraycopy(buff, index, saveBuff, 0, 4);
index += 4;
copyFile(buff, saveBuff, index, len, parentPath);
}else{
System.arraycopy(buff, index, saveBuff, 0, len-index);
nextLength = 4 - len + index;
saveLength = len-index;
}
}else{
nextLength = 8;
saveLength = 0;
if(index == len)return;
if(len - index >= 8){
nextLength = 0;
saveLength = 8;
System.arraycopy(buff, index, saveBuff, 0, 8);
index += 8;
copyFile(buff, saveBuff, index, len, parentPath);
}else{
nextLength = 8 - len + index;
saveLength = len - index;
System.arraycopy(buff, index, saveBuff, 0, saveLength);
}
}
}else if(startOver == 1){
startOver = 3;
if(flag == 0 || flag == 1){
if(nextLength != 0){
if(len - index >= nextLength){
System.arraycopy(buff, index, saveBuff, saveLength, nextLength);
index += nextLength;
nextLength = 0;
}else{
System.arraycopy(buff, index, saveBuff, saveLength, len-index);
saveLength += len - index;
nextLength = nextLength - saveLength;
startOver = 1;
return;
}
}
int fNlength = bytesToInt(saveBuff, 0);
saveLength = 0;
nextLength = fNlength;
if(len - index >= fNlength){
saveLength = nextLength;
nextLength = 0;
System.arraycopy(buff, index, saveBuff, 0, fNlength);
index += fNlength;
copyFile(buff, saveBuff, index, len, parentPath);
}else{
nextLength = fNlength - len + index;
saveLength = len - index;
System.arraycopy(buff, index, saveBuff, 0, saveLength);
}
}else{
if(nextLength != 0){
if(len - index >= nextLength){
System.arraycopy(buff, index, saveBuff, saveLength, nextLength);
saveLength += nextLength;
index += nextLength;
nextLength = 0;
}else{
System.arraycopy(buff, index, saveBuff, saveLength, len-index);
nextLength = nextLength - len + index;
saveLength += nextLength;
startOver = 1;
return;
}
}
remainFileLength = bytesToLong(saveBuff, 0);
copyFile(buff, saveBuff, index, len, parentPath);
}
}else{
if(flag == 0){
startOver = 0;
if(nextLength != 0){
if(len - index >= nextLength){
System.arraycopy(buff, index, saveBuff, saveLength, nextLength);
index += nextLength;
saveLength += nextLength;
nextLength = 0;
}else{
System.arraycopy(buff, index, saveBuff, saveLength, len-index);
nextLength = nextLength - len + index;
saveLength = saveLength + len - index;
startOver = 3;
return;
}
}
String fileName = new String(saveBuff, 0, saveLength+nextLength);
File file = new File(parentPath + fileName);
if(!file.exists()){
file.mkdir();
}
copyFile(buff, saveBuff, index, len, parentPath);
}else if(flag == 1){
startOver = 0;
if(nextLength != 0){
if(len - index >= nextLength){
System.arraycopy(buff, index, saveBuff, saveLength, nextLength);
index += nextLength;
saveLength += nextLength;
nextLength = 0;
}else{
System.arraycopy(buff, index, saveBuff, saveLength, len-index);
nextLength = nextLength - len + index;
saveLength += len - index;
startOver = 3;
return;
}
}
String fileName = new String(saveBuff, 0, saveLength+nextLength);
File file = new File(parentPath + fileName);
if(!file.exists()){
file.createNewFile();
out = new FileOutputStream(file);
}
copyFile(buff, saveBuff, index, len, parentPath);
}else{
startOver = 0;
if(len - index >= remainFileLength){
out.write(buff, index, (int)remainFileLength);
out.close();
index += (int)remainFileLength;
remainFileLength = 0L;
copyFile(buff, saveBuff, index, len, parentPath);
}else{
startOver = 3;
remainFileLength = remainFileLength - len + index;
out.write(buff, index, len-index);
}
}
}
}
private int bytesToInt(byte[] buf,int off){
int i=0;
i=i|((buf[off]&255)<<24);
i=i|((buf[off+1]&255)<<16);
i=i|((buf[off+2]&255)<<8);
i=i|(buf[off+3]&255);
return i;
}
private long bytesToLong(byte[] buf,int off){
long i=0;
i=i|(((long)buf[off]&255)<<56)
|(((long)buf[off+1]&255)<<48)
|(((long)buf[off+2]&255)<<40)
|(((long)buf[off+3]&255)<<32)
|(((long)buf[off+4]&255)<<24)
|(((long)buf[off+5]&255)<<16)
|(((long)buf[off+6]&255)<<8)
|((long)buf[off+7]&255);
return i;
}
}
public static void main(String []args){
//云桌面接受文件
// ServerFinal server = new ServerFinal(10002, "C:\\Users\\shiweigang\\Desktop\\");
// long sT = System.currentTimeMillis();
// server.execute();
// System.out.println("costed total time is "+(System.currentTimeMillis() - sT));
//从云桌面下载文件
try {
File file = new File("C:\\Users\\shiweigang\\Desktop\\GSRCUMobileBank.zip");
if(file.isFile()){
ServerSocket server = new ServerSocket(10003);
System.out.println("start receive..");
Socket socket = server.accept();
OutputStream out = socket.getOutputStream();
FileInputStream fi = new FileInputStream(file);
String fileName = file.getName();
byte fileNameLength = (byte) fileName.getBytes().length;
out.write(fileNameLength);
out.write(fileName.getBytes());
byte bytes [] = new byte[1024];
int length = 0;
while((length = fi.read(bytes)) != -1){
out.write(bytes, 0, length);
}
fi.close();
out.close();
socket.close();
server.close();
System.out.println("file send already!");
}else{
System.err.println("read file exception");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}