ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Java - Network - File I/O
    Java 2018. 2. 7. 10:46
    반응형
    // Network를 통한 File I/O는
    // Socket을 통해서 주고 받는다


    import java.io.BufferedInputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.net.Socket;

    public class SenderMain {

        public static void main(String[] args) throws Exception {
            
            // 1. 경로 지정
            String path = "D:/io/bang.gif";
            File file = new File(path);
            
            // 2. 스트림 준비
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            
            // 소켓 준비
            Socket socket = new Socket("localhost", 9000);
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
            
            // 파일명 전송
            String[] depth = path.split("/"); // /로 문자 끊기
            // length가 마지막이니까 length -1로 파일명만 추출
            dos.writeUTF(depth[depth.length-1]);
            dos.flush();
            
            // 3. 읽고 쓰고
            byte[] arr = new byte[1024];
            while(bis.read(arr) != -1) {
                dos.write(arr);
            }
            
            // 4. 자원 정리
            dos.flush();
            System.out.println("전송 완료");
            System.out.println("전송 파일 사이즈 : "+file.length()/1024+"KB");
            
            socket.close();
            fis.close();
            bis.close();
            dos.close();
            
        }

    }





    import java.io.BufferedOutputStream;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;

    public class ReceiverMain {

        public static void main(String[] args) throws Exception {
            
            // 소켓 준비
            ServerSocket server = new ServerSocket(9000);
            
            while(true) { // 대기
                System.out.println("요청 대기 중");
                Socket socket = server.accept(); // 요청이 올 경우 수락
                System.out.println("요청 수락");
                // 여러 요청을 처리하기 위해 스레드 생성
                Thread th = new Thread() {
                    @Override
                    public void run() {
                        // 1. 파일 위치 지정
                        String path = "D:/Download";
                        // 2. 스트림 준비
                        try {
                            DataInputStream dis = new DataInputStream(socket.getInputStream());
                            // 폴더 만들기
                            File dir = new File(path);
                            if(!dir.exists()) {
                                System.out.println("폴더가 없으므로 폴더 생성");
                                dir.mkdirs();
                            }
                            // 3. 읽기 -> 쓰기
                            String fileName = dis.readUTF();
                            FileOutputStream fos = new FileOutputStream(path+fileName);
                            BufferedOutputStream bos = new BufferedOutputStream(fos);
                            byte[] arr = new byte[1024];
                            while(dis.read(arr) != -1) {
                                bos.write(arr);
                            }
                            // 4. flush
                            bos.flush();
                            System.out.println("다운로드 완료");
                            // 5. 자원 정리
                            bos.close();
                            fos.close();
                            dis.close();
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        
                    }
                };
                th.start();
            }
            
        }

    }







    반응형

    'Java' 카테고리의 다른 글

    Java - Lambda  (0) 2018.02.07
    Java - Webserver Http  (0) 2018.02.07
    Java - UDP  (0) 2018.02.06
    Java - Network, MultiChat  (0) 2018.02.06
    Java - Network - Chat  (0) 2018.02.06

    댓글

Designed by Tistory.