demire
demire

Reputation: 49

Connection from server to client, file transfer

By using instance of PrintStream and println function, I can send raw string to the client(s). But, I want to send whole .html file to the client in order to see the web page. For this reason, What should be my approach ? I have tried to read a file and give the whatever is read on the println function. But, attempts is failed.

Upvotes: 0

Views: 78

Answers (1)

Eernie
Eernie

Reputation: 469

Maby something like this will help:

// sendfile
File myFile = new File ("source.html");
byte [] mybytearray  = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...");
os.write(mybytearray,0,mybytearray.length);
os.flush();
sock.close();

Upvotes: 1

Related Questions