Duvidas no envio de arquivos via sockets

1 resposta
G

Galera,

Sou iniciante em Java e estou tendo dificuldades em criar uma aplicação Cliente\Servidor usando sockets par ao envio de arquivos.

A ideia é que o cliente envie o arquivo e o servidor receba este arquivo gravando em uma pasta.

Inicialmente escrevi estes codigos:

Cliente
public class Net_Client {
    private Socket cliente = null;
    PrintStream ps = null;
    
       public Net_Client()  {
    }
    
    public void start_cli(){
        try{
        
              cliente = new Socket("127.0.0.1",7000);
              System.out.println("teste");
		        
		        
 DataOutputStream out = new DataOutputStream (cliente.getOutputStream());
	/* abrir arquivo para o envio  */
                FileInputStream fin = new FileInputStream ("C:\\raiz\\FGTS.txt");
                DataInputStream in = new DataInputStream (fin);

                /* cria um buffer de 512 bytes para o envio */
                //byte buffer[] = new byte[10240];            
                byte[] cache = new byte[10240]; 
                int size = 0; 
                while ((size = in.read(cache)) > -1) { 
                out.write(cache, 0, size); 
                }
                
                
               }catch(IOException e){
            System.out.println("Erro");
            //e.printStackTrace();
            System.exit(0);   
        }
    }
  
    }

Servidor:

public class Net_Server {
    private ServerSocket servidor = null;
    private Socket client  = null;       
    /** Creates a new instance of Net_Server */
    public Net_Server() {
    }
    
    public void start_serv(){
        try{
    		servidor = new ServerSocket(7000,100);
    		int nCont = 0;
            while(true){
                client = servidor.accept();
             	nCont++;
             	System.out.println(nCont+"º Cliente conectado");
                new Connect(client).start();
}catch(IOException e){
            e.printStackTrace();
            System.exit(0);    
        }
        
    } 
     
class Connect extends Thread{
	private Socket conexao;
	
        public Connect(Socket conexao){
        this.conexao = conexao;
        
        }
        
        public void run() 
	{
		try{
			
        		DataInputStream entrada = new DataInputStream (client.getInputStream());
                        DataOutputStream saida = new DataOutputStream (new FileOutputStream("C:\\teste\\FGTS.txt"));
                        saida.flush();
                        byte buffer[] = new byte[10240];
                        while (entrada.read()!=-1) saida.write(buffer);
       			entrada.close();
       			saida.close();
       			conexao.close();
       			        	
       		}	
	        catch (Exception e) {
        		System.err.println("Erro: " + e.getMessage());
	        }
	}
}

Se alguem puder me ajudar agradeço

Abraço!!!

1 Resposta

Abdon

Vc não esta chamando o método flush do DataOutputStream no seu servidor.

Criado 31 de maio de 2007
Ultima resposta 31 de mai. de 2007
Respostas 1
Participantes 2