-
Notifications
You must be signed in to change notification settings - Fork 5
/
Client.java
84 lines (71 loc) · 1.99 KB
/
Client.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import javax.swing.*;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client {
private static final Scanner kb = new Scanner(System.in);
private static BufferedOutputStream output;
private static BufferedInputStream p_input;
private static final int buffer = 16000000;
private static final byte[] b = new byte[buffer];
private static Socket socket;
private static int n;
public static void main(String args[]) {
int port = 69;
System.out.print("Enter The Server IP Address: ");
String ip = kb.nextLine();
try {
socket = new Socket(ip, port);
String file = getTransferFile();
setUpStreams(file);
sendFileName(file);
transferData();
closeCrap();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
/*
*
* The getTransferFile() method takes the URL of the file to be transfered
* as String and then returns the URL as a String.
*
*/
private static String getTransferFile() {
String file = "";
JFileChooser fc = new JFileChooser();
int ans = fc.showOpenDialog(null);
if(ans == JFileChooser.APPROVE_OPTION)
{
file = fc.getSelectedFile().getAbsolutePath();
}
return file;
}
/*
*
* The setUpStreams() method sets up the BufferedInputStream p_input and
* BufferedOutputStream output. The p_input takes a FileInputStream pointing
*
*/
private static void setUpStreams(String file) throws IOException {
p_input = new BufferedInputStream(new FileInputStream(file), buffer);
output = new BufferedOutputStream(socket.getOutputStream(), buffer);
}
private static void sendFileName(String file) throws IOException {
String fileName = new File(file).getName();
output.write(fileName.getBytes("UTF-8"));
output.write(0);
output.flush();
}
private static void transferData() throws IOException {
while((n = p_input.read(b, 0, buffer)) != -1) {
output.write(b, 0, n);
}
}
private static void closeCrap() throws IOException {
kb.close();
p_input.close();
output.close();
socket.close();
}
}