-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFTPServer.java
More file actions
64 lines (53 loc) · 1.52 KB
/
FTPServer.java
File metadata and controls
64 lines (53 loc) · 1.52 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
52
53
54
55
56
57
58
59
60
61
62
63
64
package server;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class FTPServer extends ServerSocket{
private static final int PORT = 2013;
private ServerSocket server;
private Socket client;
private DataInputStream dis;
private FileOutputStream fos;
public FTPServer() throws Exception{
try{
server = new ServerSocket(PORT);
while(true){
client = server.accept();
dis = new DataInputStream(client.getInputStream());
String fileName = dis.readUTF();
long fileLength = dis.readLong();
fos = new FileOutputStream(new File("E:\\"+fileName));
byte[] sendBytes = new byte[1024];
int transLen = 0;
System.out.println("------ Start to receive files <" + fileName+">, the size of the file is <"+fileLength+"> ------");
while(true){
int read = 0;
read = dis.read(sendBytes);
if(read == -1)
break;
transLen += read;
System.out.println("The process of receving file <" + fileName+"> is " + 100.0*transLen/fileLength + "% ...");
fos.write(sendBytes, 0, read);
fos.flush();
}
System.out.println("------ Receive file <"+fileName+"> successfully ---------");
client.close();
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(dis != null){
dis.close();
}
if(fos != null){
fos.close();
}
server.close();
}
}
public static void main(String[] args) throws Exception{
new FTPServer();
}
}