Unconfigured Ad Widget

Collapse

Anúncio

Collapse
No announcement yet.

Envio de Arquivos via Socket in Java

Collapse
X
 
  • Filter
  • Tempo
  • Show
Clear All
new posts

  • Font Size
    #1

    Video Aula Envio de Arquivos via Socket in Java

    Olá galerinha... Fiz um videozinho ensinando a enviar arquivos por socket em Java(infelizmente muita gente não sabe, espero que ajudem)...


    [ame]http://www.youtube.com/watch?v=eqoGu2m44ds[/ame]

    Source do Cliente
    Código PHP:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package socket_in_java;

    import java.io.*;
    import java.net.Socket;
    import java.net.UnknownHostException;

    /**
     *
     * @author Eduardo
     */
    public class Cliente {

        
    /**
         * @param args the command line arguments
         */
        
    public static void main(String[] argsthrows UnknownHostExceptionIOException {
            
    Socket cliente = new Socket("127.0.0.1",1234);
            
            
    /*DataInputStream in = new DataInputStream(cliente.getInputStream());
            DataOutputStream out = new DataOutputStream(cliente.getOutputStream());
            
            int intt = in.readInt();
            
            System.out.println(intt);*/
          
            
    ObjectOutputStream out = new ObjectOutputStream(cliente.getOutputStream());
            
            
    FileInputStream file = new FileInputStream("c:/z/1.jpg");
            
            
    byte[] buf = new byte[4096];
            
            while(
    true){
                
    int len file.read(buf);
                if(
    len == -1) break;
                
    out.write(buf0len);
            }
            
        }


    Source do Servidor:
    Código PHP:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package socket_in_java;

    import java.io.*;
    import java.net.ServerSocket;
    import java.net.Socket;

    /**
     *
     * @author Eduardo
     */
    public class Servidor {
        
        public static 
    void main(String[] argsthrows IOException{
            
            
    ServerSocket servidor = new ServerSocket(1234);
            
            
    Socket sv servidor.accept();
            
            
    /*DataInputStream in = new DataInputStream(sv.getInputStream());
            DataOutputStream out = new DataOutputStream(sv.getOutputStream());
            
            out.writeInt(123456789);*/
              
            
    ObjectInputStream out = new ObjectInputStream(sv.getInputStream());
            
            
    FileOutputStream file = new FileOutputStream("c:/z/Arquivoqueclienteenviou.jpg");
            
            
    byte[] buf = new byte[4096];
            
            while(
    true){
                
    int len out.read(buf);
                if(
    len == -1) break;
                
    file.write(buf0len);
            }
            
            
        }
        


    Att #M0rph
    "Nunca desista de seus sonhos. Desisti de seus sonhos é abrir mão da felicidade."
    (Augusto Cury)



    Meu Blog... http://www.lab-infor.blogspot.com
    Meu canal do Youtube... http://www.youtube.com/user/rodrigo32323232
X
Working...
X