forked from KevinKien/Chat-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpload.java
More file actions
57 lines (48 loc) · 1.56 KB
/
Upload.java
File metadata and controls
57 lines (48 loc) · 1.56 KB
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
package socket;
import gui.ChatFrame;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Upload implements Runnable{
public String addr;
public int port;
public Socket socket;
public FileInputStream In;
public OutputStream Out;
public File file;
public ChatFrame ui;
public Upload(String addr, int port, File filepath, ChatFrame frame){
super();
try {
file = filepath; ui = frame;
socket = new Socket(InetAddress.getByName(addr), port);
Out = socket.getOutputStream();
In = new FileInputStream(filepath);
}
catch (Exception ex) {
System.out.println("Exception [Upload : Upload(...)]");
}
}
@Override
public void run() {
try {
byte[] buffer = new byte[1024];
int count;
while((count = In.read(buffer)) >= 0){
Out.write(buffer, 0, count);
}
Out.flush();
ui.jTextArea1.append("[Applcation > Me] : File upload complete\n");
ui.jButton5.setEnabled(true); ui.jButton6.setEnabled(true);
ui.jTextField5.setVisible(true);
if(In != null){ In.close(); }
if(Out != null){ Out.close(); }
if(socket != null){ socket.close(); }
}
catch (Exception ex) {
System.out.println("Exception [Upload : run()]");
ex.printStackTrace();
}
}
}