Advertisement
rfop

Tranfer

Nov 11th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.25 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.OutputStream;
  5. import java.net.InetAddress;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8.  
  9. public class Servidor {
  10.    
  11.     public static void main(String[] args) throws Exception {
  12.         //inicializa o servidor
  13.         ServerSocket ssock = new ServerSocket(5000);
  14.         //aceita conexao
  15.         Socket socket = ssock.accept();
  16.        
  17.         //The InetAddress specification
  18.         InetAddress IA = InetAddress.getByName("localhost");
  19.        
  20.         //especifica o arquivo
  21.         File file = new File("TesteMusic.rar");
  22.         // transforma o arquio em um formao que pode ser lido(stream de bytes)
  23.         FileInputStream fis = new FileInputStream(file);
  24.         // encapsula o fis para bis para aumentar a velocidade de transferencia
  25.         BufferedInputStream bis = new BufferedInputStream(fis);
  26.          
  27.         //Pega o output socket para mandar para o cliente
  28.         OutputStream os = socket.getOutputStream();
  29.                
  30.         //Inicializo o array de bytes
  31.         byte[] contents;
  32.         // tamanho do arquivo
  33.         long fileLength = file.length();
  34.         // long auxiliar para ficar vendo onde esta no arquivo
  35.         long current = 0;
  36.        
  37.         //loop para converter arquivo para array de bytes
  38.         while(current!=fileLength){
  39.             // meu buffer d tamanho 10000
  40.             int size = 10000;
  41.             // enquanto n chega na ultima parte incrementa current
  42.             if(fileLength - current >= size)
  43.                 current += size;    
  44.             else{
  45.                 // qnd chega na ultima parte, verfica qnt falta e coloca em size
  46.                 size = (int)(fileLength - current);
  47.                 current = fileLength;
  48.             }
  49.             contents = new byte[size];
  50.             bis.read(contents, 0, size);
  51.             os.write(contents);
  52.             System.out.println("Sending file ... "+(current*100)/fileLength+"% complete!");
  53.         }  
  54.        
  55.         os.flush();
  56.         //File transfer done. Close the socket connection!
  57.         socket.close();
  58.         ssock.close();
  59.         bis.close();
  60.         System.out.println("File sent succesfully!");
  61.     }
  62. }
  63.  
  64. import java.io.BufferedOutputStream;
  65. import java.io.FileOutputStream;
  66. import java.io.InputStream;
  67. import java.net.InetAddress;
  68. import java.net.Socket;
  69.  
  70.  
  71. public class Cliente {
  72.    
  73.     public static void main(String[] args) throws Exception{
  74.        
  75.         //Initialize socket
  76.         Socket socket = new Socket(InetAddress.getByName("localhost"), 5000);
  77.         byte[] contents = new byte[10000];
  78.        
  79.         //Initialize the FileOutputStream to the output file's full path.
  80.         FileOutputStream fos = new FileOutputStream("TesteMusic2.rar");
  81.         BufferedOutputStream bos = new BufferedOutputStream(fos);
  82.         InputStream is = socket.getInputStream();
  83.        
  84.         //No of bytes read in one read() call
  85.         int bytesRead = 0;
  86.        
  87.         while((bytesRead=is.read(contents))!=-1)
  88.             bos.write(contents, 0, bytesRead);
  89.        
  90.         bos.flush();
  91.         socket.close();
  92.        
  93.         System.out.println("File saved successfully!");
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement