-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.java
78 lines (66 loc) · 2.05 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
package fileTransfer;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class Client {
public final static String IP = "198.198.207.68";
public final static int PORT = 10002;
public final static String IP2 = "localhost";
public final static int PORT2 = 8000;
public final static String TEXT_FILE_PATH = "C:\\Users\\59205\\Desktop\\text.txt";
public final static String FILE_PATH = "C:\\Users\\59205\\Desktop\\FileTransferClient.java";
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Socket socket = new Socket(IP, PORT);
OutputStream os = socket.getOutputStream();
byte type = 1;//0 text 1 file
os.write(type);
if(0 == type){
sendText(TEXT_FILE_PATH, os);
}else if(1 == type){
sendFile(FILE_PATH, os);
}
}
public static void sendText(String filePath, OutputStream out) throws IOException{
File file = new File(filePath);
if(file.isFile()){
FileInputStream fi = new FileInputStream(file);
byte bytes [] = new byte[1024];
int length = 0;
while((length = fi.read(bytes)) != -1){
out.write(bytes, 0, length);
}
fi.close();
out.close();
System.out.println("text send already!");
}else{
System.err.println("read file exception");
out.close();
}
}
public static void sendFile(String filePath, OutputStream out) throws IOException{
File file = new File(filePath);
if(file.isFile()){
FileInputStream fi = new FileInputStream(file);
String fileName = file.getName();
byte fileNameLength = (byte) fileName.getBytes().length;
out.write(fileNameLength);
out.write(fileName.getBytes());
byte bytes [] = new byte[1024];
int length = 0;
while((length = fi.read(bytes)) != -1){
out.write(bytes, 0, length);
}
fi.close();
out.close();
System.out.println("file send already!");
}else{
System.err.println("read file exception");
out.close();
}
}
public static void sendDir(String filePath, OutputStream out) throws IOException{
}
}