-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileClient.java
58 lines (45 loc) · 1.39 KB
/
FileClient.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
import java.net.*;
import java.io.*;
public class FileClient {
private static Socket s;
private static int filesize = 0;
public FileClient(String host, int port) {
try {
s = new Socket(host, port);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void saveFile(Socket clientSock, String file) throws IOException {
DataInputStream dis = new DataInputStream(clientSock.getInputStream());
FileOutputStream fos = new FileOutputStream("received-" + file);
byte[] buffer = new byte[4096];
int read = 0;
int totalRead = 0;
int remaining = filesize;
while ((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
totalRead += read;
remaining -= read;
System.out.println("read " + totalRead + " bytes.");
fos.write(buffer, 0, read);
}
fos.close();
dis.close();
}
public static void main(String[] args) throws Exception {
FileClient fc = new FileClient("localhost", 1102);
DataInputStream din = new DataInputStream(s.getInputStream());
DataOutputStream dout = new DataOutputStream(s.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String file = "", size = "";
file = br.readLine();
dout.writeUTF(file);
dout.flush();
size = din.readUTF();
System.out.println("File Size: " + size);
filesize = Integer.parseInt(size);
saveFile(s, file);
dout.close();
s.close();
}
}