-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathHTTPSendFile.java
More file actions
44 lines (42 loc) · 1.33 KB
/
Copy pathHTTPSendFile.java
File metadata and controls
44 lines (42 loc) · 1.33 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
package HTTP;
import java.io.*;
import util.*;
class HTTPSendFile extends Thread {
private final static String CRLF = "\r\n";
private DataOutputStream out = null;
private String file = null;
private int ID = 0;
public HTTPSendFile(DataOutputStream out, String file, int ID){
this.out = out;
this.file = file;
this.ID = ID;
}
public void run(){
int size = 0, curPos = 0;
try{
FileInputStream fis = new FileInputStream(file);
size = fis.available();
Out.info("FileSend: " + ID, "Attempting to send " + file);
out.writeBytes(HTTPServer.buildHeader(200, "application/zip", size) + CRLF);
int lastPer = -1;
byte[] buff = new byte[1024];
int flag = fis.read(buff);
while(flag != -1) {
out.write(buff, 0, flag);
curPos += flag;
int curPer = (int)(((double)curPos/(double)size)*100);
if (curPer % 10 == 0 && curPer > lastPer){
Out.info("FileSend: " + ID, curPer + "% compleet");
lastPer = curPer;
}
flag = fis.read(buff);
}
Out.info("FileSend: " + ID, "Sending " + file + " complete");
fis.close();
fis = null;
}catch(IOException e){
Out.info("FileSend: " + ID, "Sending " + file + " failed client canceled downlod");
return;
}
}
}