-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFileServer.java
More file actions
73 lines (66 loc) · 2.06 KB
/
Copy pathFileServer.java
File metadata and controls
73 lines (66 loc) · 2.06 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
67
68
69
70
71
72
73
package socket;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import static java.lang.System.in;
/**
* Created by szh on 2017/3/28.
*/
public class FileServer {
ServerSocket serverSocket=null;
public void se() throws IOException {
while (true)
{
Socket socket =serverSocket.accept();
InputStream in =socket.getInputStream();
// DataInputStream dataInputStream =new DataInputStream(in);
// FileOutputStream fileOutputStream =new FileOutputStream(new File("D:\\JavaBasis\\java锁.pdf"));
// byte[] bytes =new byte[1024];
// int le;
// while ((le= dataInputStream.read(bytes))!= -1)
// {
// fileOutputStream.write(bytes,0,le);
// fileOutputStream.flush();
// }
ServerThread serverThread =new ServerThread(in);
Thread thread =new Thread(serverThread);
thread.start();
}
}
public FileServer() {
try {
serverSocket =new ServerSocket(9010);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String args[]) throws IOException {
FileServer fileServer =new FileServer();
fileServer.se();
}
}
class ServerThread extends Thread {
InputStream inputStream;
public ServerThread(InputStream inputStream) {
this.inputStream = inputStream;
}
@Override
public void run() {
try {
the(this.inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public void the(InputStream in) throws IOException {
DataInputStream dataInputStream =new DataInputStream(in);
FileOutputStream fileOutputStream =new FileOutputStream(new File("D:\\JavaBasis\\java锁1.pdf"));
byte[] bytes =new byte[1024];
int le;
while ((le= dataInputStream.read(bytes))!= -1)
{
fileOutputStream.write(bytes,0,le);
fileOutputStream.flush();
}
}
}