-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSharingClient.java
More file actions
33 lines (28 loc) · 999 Bytes
/
FileSharingClient.java
File metadata and controls
33 lines (28 loc) · 999 Bytes
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
package fileSharing;
import java.io.*;
import java.net.*;
import java.util.*;
public class FileSharingClient {
public static void main(String[] args) throws UnknownHostException, IOException {
Scanner sc = new Scanner(System.in);
Socket sock = new Socket("localhost" , 12345);
System.out.println("Connected to server");
System.out.println("Enter file path : ");
String filePath = sc.next();
File file = new File(filePath);
long fileSize = file.length();
DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
dos.writeUTF(file.getName());
dos.writeLong(fileSize);
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
int read;
while((read = fis.read(buffer)) != -1) {
dos.write(buffer,0,read);
}
fis.close();
dos.flush();
System.out.println("File Sent: " + file.getName());
sock.close();
}
}