Advertisement
rfop

Transfer

Nov 20th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 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.        
  45.         byte[] conteudo;
  46.         long fileLength = arquivo.length();
  47.         long current = 0;
  48.        
  49.         while(current!=fileLength){
  50.             int size = 10000;
  51.             if(fileLength - current >= size)
  52.                 current += size;    
  53.             else{
  54.                 size = (int)(fileLength - current);
  55.                 current = fileLength;
  56.             }
  57.             conteudo = new byte[size];
  58.             bis.read(conteudo, 0, size);
  59.             os.write(conteudo);
  60.             System.out.println("Mandando arquivo ... "+(current*100)/fileLength+"% completo!");
  61.         }  
  62.        
  63.         os.flush();
  64.         this.socketConexao.close();
  65.         this.socketServidor.close();
  66.         bis.close();
  67.         System.out.println("Arquivo enviado com sucesso!");
  68.     }
  69. }
  70.  
  71. package Cliente;
  72. import java.io.BufferedOutputStream;
  73. import java.io.FileOutputStream;
  74. import java.io.IOException;
  75. import java.io.InputStream;
  76. import java.net.InetAddress;
  77. import java.net.InetSocketAddress;
  78. import java.net.Socket;
  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() throws Exception {
  89.         this.socketCliente = new Socket(address, porta);
  90.     }
  91.     public void transferenciaArquivo(String diretorio) throws IOException {
  92.         byte[] conteudo = new byte[10000];
  93.        
  94.         FileOutputStream fos = new FileOutputStream(diretorio);
  95.         BufferedOutputStream bos = new BufferedOutputStream(fos);
  96.         InputStream is = socketCliente.getInputStream();
  97.    
  98.         int bytesRead = 0;
  99.        
  100.         while((bytesRead=is.read(conteudo))!=-1) {
  101.             bos.write(conteudo, 0, bytesRead);
  102.         }
  103.         bos.flush();
  104.         socketCliente.close();
  105.        
  106.         System.out.println("Arquivo recebido!");
  107.     }
  108.    
  109. }
  110. import Cliente.Cliente;
  111. import servidor.Servidor;
  112.  
  113. import java.io.IOException;
  114. import java.util.Scanner;
  115. @SuppressWarnings("unused")
  116. public class Talya {
  117.  
  118.     public static void main(String[] args) {
  119.         String adress; //192.168.25.9
  120.         int porta;
  121.         Scanner in = new Scanner(System.in);
  122.         Scanner str = new Scanner(System.in);
  123.         System.out.println("endereรงo ip: ");
  124.         adress = str.nextLine();
  125.         System.out.println("porta: ");
  126.         porta = in.nextInt();
  127.        
  128.         Cliente cliente = new Cliente(porta, adress);
  129.         Servidor servidor = new Servidor(porta);
  130.         servidor.iniciaConexaoServidor();
  131.         try {
  132.             cliente.iniciarConexaoCliente();
  133.         } catch (Exception e) {
  134.             // TODO Auto-generated catch block
  135.             e.printStackTrace();
  136.         }
  137.         String diretorioServidor = "C:\\Users\\ange4\\eclipse-workspace\\ProjetoComunicacao\\TesteMusic.rar";
  138.         try {
  139.             servidor.enviarArquivo(diretorioServidor);
  140.         } catch (Exception e) {
  141.             // TODO Auto-generated catch block
  142.             e.printStackTrace();
  143.         }
  144.         String diretorioCliente = "C:\\Users\\ange4\\eclipse-workspace\\ProjetoComunicacao\\TesteMusic2.rar";
  145.         try {
  146.             cliente.transferenciaArquivo(diretorioCliente);
  147.         } catch (IOException e) {
  148.             // TODO Auto-generated catch block
  149.             e.printStackTrace();
  150.         }
  151.        
  152.        
  153.        
  154.     }
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement