forked from KevinKien/Chat-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownload.java
More file actions
59 lines (48 loc) · 1.57 KB
/
Download.java
File metadata and controls
59 lines (48 loc) · 1.57 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
58
59
package socket;
import gui.ChatFrame;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Download implements Runnable{
public ServerSocket server;
public Socket socket;
public int port;
public String saveTo = "";
public InputStream In;
public FileOutputStream Out;
public ChatFrame ui;
public Download(String saveTo, ChatFrame ui){
try {
server = new ServerSocket(0);
port = server.getLocalPort();
this.saveTo = saveTo;
this.ui = ui;
}
catch (IOException ex) {
System.out.println("Exception [Download : Download(...)]");
}
}
@Override
public void run() {
try {
socket = server.accept();
System.out.println("Download : "+socket.getRemoteSocketAddress());
In = socket.getInputStream();
Out = new FileOutputStream(saveTo);
byte[] buffer = new byte[1024];
int count;
while((count = In.read(buffer)) >= 0){
Out.write(buffer, 0, count);
}
Out.flush();
ui.jTextArea1.append("[Application > Me] : Download complete\n");
if(Out != null){ Out.close(); }
if(In != null){ In.close(); }
if(socket != null){ socket.close(); }
}
catch (Exception ex) {
System.out.println("Exception [Download : run(...)]");
}
}
}