-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleFileClient.java
More file actions
86 lines (72 loc) · 2.45 KB
/
SimpleFileClient.java
File metadata and controls
86 lines (72 loc) · 2.45 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
*
* @author Himanshu Shukla
*/
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.io.*;
import java.util.zip.*;
public class SimpleFileClient {
public final static int SOCKET_PORT = 15123; // you may change this
public final static String SERVER = "172.18.7.115"; // IP
public final static String
FILE_TO_RECEIVED = "G:/def-downloaded";
public final static int FILE_SIZE = 6022386; // file size temporary hard coded
// should bigger than the file to be downloaded
public static void main (String [] args ) throws IOException {
int bytesRead;
int current = 0;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
Socket sock = null;
try {
sock = new Socket(SERVER, SOCKET_PORT); //make a socket connection to the server
System.out.println("Connecting...");
// receive file
byte [] mybytearray = new byte [FILE_SIZE]; // byte array conversion
InputStream is = sock.getInputStream();
fos = new FileOutputStream(FILE_TO_RECEIVED);
bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
current = bytesRead;
do {
bytesRead =
is.read(mybytearray, current, (mybytearray.length-current));
if(bytesRead >= 0) current += bytesRead;
} while(bytesRead> -1);
bos.write(mybytearray, 0 , current);
bos.flush();
//fos.flush();
System.out.println("File " + FILE_TO_RECEIVED
+ " downloaded (" + current + " bytes read)");
////////////////////////////////////////////////////////
//decompression starts
try{
FileInputStream fin=new FileInputStream("G:/def-downloaded");
InflaterInputStream in=new InflaterInputStream(fin);
FileOutputStream fout=new FileOutputStream("G:/client.mp3");
int i;
while((i=in.read())!=-1){
fout.write((byte)i);
fout.flush();
}
fin.close();
fout.close();
in.close();
}catch(Exception e){System.out.println(e);}
//decompression ends
}
finally {
if (fos != null) {
fos.flush();
fos.close();
}
if (bos != null) {bos.close();bos.flush();}
if (sock != null) sock.close();
}
//file recieved
}
}