import java.io.*;
import java.net.*;
public class SimpleClient {
Socket socket;
String fileName;
int fileBytes;
public void go() {
try {
//Start a new socket to get the info
socket = new Socket("127.0.0.1",7575);
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
BufferedReader reader = new BufferedReader(isr);
fileName = reader.readLine();
fileBytes = Integer.parseInt(reader.readLine());
socket.close();
//Socket to handle file transfer
socket = new Socket("127.0.0.1",7575);
byte[] bytes = new byte[fileBytes];
InputStream is = socket.getInputStream();
for(int i=0;i<fileBytes;i++) {
is.read(bytes);
}
//Stores the file in a new directory in your system's default directory
File dir = new File("/TestApp");
if(!dir.exists()) {
dir.mkdir();
}
FileOutputStream fos = new FileOutputStream("/TestApp/"+fileName);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(bytes,0,bytes.length);
bos.close();
socket.close();
System.out.println("Transfer Complete");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In RED