rfop

Transfer

Nov 20th, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.57 KB | None | 0 0
  1. package servidor;
  2. import java.io.BufferedInputStream;
  3.  
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.io.OutputStream;
  9. import java.net.InetAddress;
  10. import java.net.ServerSocket;
  11. import java.net.Socket;
  12.  
  13.  
  14.  
  15. @SuppressWarnings("unused")
  16. public class Servidor {
  17.     private ServerSocket socketServidor;
  18.     private Socket socketConexao;
  19.     private int porta;
  20.    
  21.     public Servidor(int porta) {
  22.         this.porta = porta;
  23.     }
  24.     public void iniciaConexaoServidor() {
  25.        
  26.         try {
  27.             this.socketServidor = new ServerSocket(this.porta);
  28.         } catch (IOException e) {
  29.             System.out.println("Não conseguiu iniciar o socket do servidor");
  30.             e.printStackTrace();
  31.         }
  32.         try {
  33.             this.socketConexao = socketServidor.accept();
  34.         } catch (IOException e) {
  35.             System.out.println("Não conseguiu aceitar a conexao com o cliente.");
  36.             e.printStackTrace();
  37.         }
  38.     }
  39.     public void enviarArquivo(String diretorio) throws Exception {
  40.         File arquivo = new File(diretorio);
  41.         FileInputStream fis = new FileInputStream(arquivo);
  42.         BufferedInputStream bis = new BufferedInputStream(fis);
  43.         OutputStream os = this.socketConexao.getOutputStream();
  44.         byte[] conteudo;
  45.         long fileLength = arquivo.length();
  46.         long current = 0;
  47.        
  48.         while(current!=fileLength){
  49.             int size = 10000;
  50.             if(fileLength - current >= size)
  51.                 current += size;    
  52.             else{
  53.                 size = (int)(fileLength - current);
  54.                 current = fileLength;
  55.             }
  56.             conteudo = new byte[size];
  57.             bis.read(conteudo, 0, size);
  58.             os.write(conteudo);
  59.             System.out.println("Mandando arquivo ... "+(current*100)/fileLength+"% completo!"+"ip do cliente:"+socketConexao.getInetAddress());
  60.         }  
  61.        
  62.         os.flush();
  63.         this.socketConexao.close();
  64.         this.socketServidor.close();
  65.         bis.close();
  66.         System.out.println("Arquivo enviado com sucesso!");
  67.     }
  68. }
  69.  
  70. package Cliente;
  71. import java.io.BufferedOutputStream;
  72. import java.io.FileOutputStream;
  73. import java.io.IOException;
  74. import java.io.InputStream;
  75. import java.net.InetAddress;
  76. import java.net.InetSocketAddress;
  77. import java.net.Socket;
  78. import java.net.UnknownHostException;
  79.  
  80. public class Cliente {
  81.     private Socket socketCliente;
  82.     int porta;
  83.     String address;
  84.     public Cliente(int porta, String address) {
  85.         this.porta=porta;
  86.         this.address=address;
  87.     }
  88.     public void iniciarConexaoCliente() {
  89.         try {
  90.             this.socketCliente = new Socket(address, porta);
  91.         } catch (UnknownHostException e) {
  92.             System.out.println("Não conseguiu iniciar a conexao com o servidor");
  93.             e.printStackTrace();
  94.         } catch (IOException e) {
  95.             System.out.println("Não conseguiu iniciar a conexao com o servidor");
  96.             e.printStackTrace();
  97.         }
  98.     }
  99.     public void transferenciaArquivo(String diretorio) throws IOException {
  100.         byte[] conteudo = new byte[10000];
  101.        
  102.         FileOutputStream fos = new FileOutputStream(diretorio);
  103.         BufferedOutputStream bos = new BufferedOutputStream(fos);
  104.         InputStream is = socketCliente.getInputStream();
  105.         int bytesRead = 0;
  106.         long current= 0;
  107.         long conteudoLength = conteudo.length;
  108.         while((bytesRead=is.read(conteudo))!=-1) {
  109.             bos.write(conteudo, 0, bytesRead);
  110.             current = bytesRead;
  111.             System.out.println("Recebendo arquivo..."+(current*100)/conteudoLength+"% recebido!" +"ip do servidor:"+this.address);
  112.         }
  113.         bos.flush();
  114.         socketCliente.close();
  115.        
  116.         System.out.println("Arquivo recebido!");
  117.     }
  118.    
  119. }
  120. import Cliente.Cliente;
  121. import servidor.Servidor;
  122.  
  123. import java.io.IOException;
  124. import java.util.Scanner;
  125. @SuppressWarnings("unused")
  126. public class TransferMain {
  127.  
  128.     public static void main(String[] args) {
  129.         String adress; //192.168.25.9
  130.         int porta;
  131.         String escolha;
  132.         Scanner in = new Scanner(System.in);
  133.         Scanner str = new Scanner(System.in);
  134.         System.out.println("Servidor ou cliente? S ou C");
  135.         escolha = str.nextLine();
  136.         System.out.println("endereço ip: ");
  137.         adress = str.nextLine();
  138.         System.out.println("porta: ");
  139.         porta = in.nextInt();
  140.         if(escolha.equals("S")) {
  141.             Servidor servidor = new Servidor(porta);
  142.             servidor.iniciaConexaoServidor();
  143.             String diretorioServidor = "MusicasTeste.rar";
  144.             try {
  145.                 servidor.enviarArquivo(diretorioServidor);
  146.             } catch (Exception e) {
  147.                 // TODO Auto-generated catch block
  148.                 e.printStackTrace();
  149.             }
  150.         } else {
  151.              Cliente cliente = new Cliente(porta, adress);
  152.              try {
  153.                  cliente.iniciarConexaoCliente();
  154.              } catch (Exception e) {
  155.                  System.out.println("Não iniciou a conexão com o Cliente.");
  156.                  e.printStackTrace();
  157.              }
  158.              String diretorioCliente = "MusicasTeste2.rar";
  159.              try {
  160.                  cliente.transferenciaArquivo(diretorioCliente);
  161.              } catch (IOException e) {
  162.                  System.out.println("Não conseguiu receber o arquivo.");
  163.                  e.printStackTrace();
  164.              }
  165.            
  166.         }
  167.        
  168.        
  169.        
  170.        
  171.        
  172.     }
  173.  
  174. }
Add Comment
Please, Sign In to add comment