Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. package Client;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.net.Socket;
  8.  
  9. public class fileReciever {
  10. public static void main(String[] args) {
  11. try {
  12. Socket sock = new Socket("127.0.0.1", 10001);
  13. DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
  14. DataInputStream dis = new DataInputStream(sock.getInputStream());
  15.  
  16.  
  17. String fileName = "cake.mp3";
  18.  
  19. dos.writeUTF(fileName);
  20.  
  21. FileOutputStream fos = new FileOutputStream("D:\\save\\" + fileName);
  22.  
  23. int readCount = 0;
  24. byte[] buffer = new byte[8192];
  25.  
  26. long start = System.currentTimeMillis();
  27.  
  28. while((readCount = dis.read(buffer)) != -1) {
  29. fos.write(buffer, 0, readCount);
  30. }
  31.  
  32. long end = System.currentTimeMillis();
  33. System.out.println("[8192] Saved Safely! : " + (end - start) + "ms");
  34.  
  35. /*while(true) {
  36. int data = dis.read();
  37. if(data == -1) break;
  38. fos.write(data);
  39. }*/
  40.  
  41. dos.close();
  42. dis.close();
  43. fos.close();
  44. sock.close();
  45.  
  46. } catch(IOException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement