-
Notifications
You must be signed in to change notification settings - Fork 60
/
Sender.java
37 lines (31 loc) · 1.21 KB
/
Sender.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
import java.io.*;
import java.net.Socket;
public class Sender {
private static DataOutputStream dataOutputStream = null;
private static DataInputStream dataInputStream = null;
public static void main(String[] args) {
try(Socket socket = new Socket("193.161.193.99", 64510)) {
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
sendFile("path/to/file2.pdf"); //TODO: To be changed
dataInputStream.close();
dataInputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
private static void sendFile(String path) throws Exception{
int bytes = 0;
File file = new File(path);
FileInputStream fileInputStream = new FileInputStream(file);
// send file size
dataOutputStream.writeLong(file.length());
// break file into chunks
byte[] buffer = new byte[4*1024];
while ((bytes=fileInputStream.read(buffer))!=-1){
dataOutputStream.write(buffer,0,bytes);
dataOutputStream.flush();
}
fileInputStream.close();
}
}