SSISO Community

시소당

자바로 이미지 파일 송신 수신하는 소스

//Client_sendFile.java 

 

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class Client_sendFile extends JFrame implements ActionListener{

    FileDialog fd;    //  파일 전송 다이알로그창
    JButton b1,b2;  //   J버튼
    JTextField tf;   // 파일전송 상태를 나타내줄 텍스트필드.
    String directory="" , file="",  ipAddr;  //  저장될 디렉토리와 파일명 그리고 접속할 IP주소

 


    /**
    * Client_sendFile 메서드 호출시 파일을 선택하고 전송하기 위함
    * @param ipAddr  --   연결하고자 하는 ip주소..
    */

    public Client_sendFile(String ipAddr) throws Exception {

        this.ipAddr = ipAddr;
        b1 = new JButton( "파일선택" );
            b1.addActionListener( this );
        tf = new JTextField(20);
        b2 = new JButton( "파일전송" );
            b2.addActionListener(this);

        add( b1, "North" );
        add( tf, "Center" );
        add( b2, "South" ); 

        setBounds(200,200,200,200);
        setVisible(true);
    }

 

    /**
    * 이벤트 메서드 오버라이딩
    */

    public void actionPerformed(ActionEvent ae){
        try{
            if(ae.getActionCommand().equals( "파일선택" ) ){
                fd = new FileDialog (this, "", FileDialog.LOAD);  // 다이얼로그창 생성
                fd.setVisible(true);
                tf.setText("");

                directory = fd.getDirectory();  // 다이얼로그창에서 선택된 디렉토리를..

                file = fd.getFile();  // 파일을...
                tf.setText( directory+file );

            } else {
                Socket s = new Socket( ipAddr, 7777 );   // 주어지는 ip주소와 포트번호로 접속하는 소켓생성.
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter( s.getOutputStream() ) );
                System.out.println( "파일명 : "+file );
                bw.write( file+"\n" );
                bw.flush();

                DataInputStream dis = new DataInputStream(new FileInputStream(new File( tf.getText() ) ) );
                DataOutputStream dos = new DataOutputStream( s.getOutputStream() );
                System.out.println( "바이트 단위로 쓰겠습니다..!!" );

                    int i=0;
                    while( (i=dis.read()) != -1 ){
                        System.out.print("1");
                        dos.writeByte(i);
                        dos.flush();
                    }
                    System.out.println("이햐~ 다 썼네요..!!");

                // 자원정리
                dis.close();
                dos.close();
                s.close();
                dis = null;
                dos = null;
                s = null;

                System.out.println( "시스템 정리 후 파일 전송 종료하께요^^" );
                this.setVisible(false);
            }
        }catch(Exception e){
            System.out.println( e + "접속하는 데 실패하셨습니다..!!" );
          }
    }
}

 

 

 

//Server_sendFile.java

 

import java.net.*;
import java.io.*;

public class Server_sendFile{


    /**
    *  클라이언트간의 파일전송을 목적으로 함.
    */

    public Server_sendFile() throws Exception {
        ServerSocket ss = new ServerSocket(7777);
        System.out.println( "서버소켓을 생성하였습니다..!!" );

        Socket s = ss.accept();
        System.out.println("소켓 "+s+" 와 연결되었습니다..!!");

        InputStream in = s.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        System.out.println("스트림을 얻었습니다..!!");
        String fileName = br.readLine();

        File f = new File("c:\\Documents and Settings\\All Users\\바탕 화면\\", fileName);
        System.out.println("객체 생성");

        FileOutputStream out = new FileOutputStream(f);

            int  i = 0;
            while( ( i = in.read() ) != -1) {
                out.write ((char)i);
            }

        // 시스템정리
        br.close();
        in.close();
        out.close();
        s.close();
        ss.close();
        br=null;
        in=null;
        out=null;
        s=null;
        ss=null;

    }
}

1062 view

4.0 stars