forked from varunon9/Remote-Control-PC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendFile.java
More file actions
57 lines (54 loc) · 1.89 KB
/
SendFile.java
File metadata and controls
57 lines (54 loc) · 1.89 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package filesharing;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
/**
*
* @author varun
*/
public class SendFile {
public void sendFile(final String path, final ObjectOutputStream out) {
new Thread() {
@Override
public void run() {
FileInputStream fis = null;
try {
File file = new File(path);
long fileSize = file.length();
//sending fileSize first
out.writeObject(fileSize);
out.flush();
System.out.println(path + " " + fileSize);
fis = new FileInputStream(file);
byte[] buffer = new byte[4096];
int read = 0;
long totalRead = 0;
int remaining = (int) fileSize;
while ((read = fis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
totalRead += read;
remaining -= read;
System.out.println("Transfer Progress: " + ((totalRead * 100) / fileSize));
out.write(buffer, 0, read);
out.flush();
}
out.flush();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
}.start();
}
}