-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFTPServer.java
More file actions
66 lines (57 loc) · 1.46 KB
/
Copy pathFTPServer.java
File metadata and controls
66 lines (57 loc) · 1.46 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
65
66
package java_base;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FTPServer {
static {
File f = new File("D:\\upload");
if(!f.exists())
{
f.mkdir();
}
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
System.out.println("服务器端程序启动...");
ServerSocket ss = new ServerSocket(8888);
while(true) {
Socket socket = ss.accept();
new ReadFileThread(socket).start();
}
}
}
class ReadFileThread extends Thread{
Socket socket;
public ReadFileThread(Socket socket) {
// TODO Auto-generated constructor stub
this.socket = socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
String name = "D:\\upload\\" + sdf.format(new Date()) + ".txt";
FileOutputStream fos = new FileOutputStream(name);
InputStream is = socket.getInputStream();
int len = 0;
byte[] ba = new byte[1024];
while((len = is.read(ba)) != -1) {
fos.write(ba, 0, len);
}
// PrintStream ps = new PrintStream(socket.getOutputStream());
// ps.println("上传成功!");
// ps.flush();
// fos.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}