서버에 네트워크 프로그래밍 Socket 파일 업로드

3365 단어 소켓 통신
서버:
package com.softeem.scokt;

import java.io.BufferedInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketFileup
{

    public static void fileup() {
        ServerSocket ss = null;
        DataOutputStream dout = null;
        FileInputStream fis = null;
        InputStream is = null;
        BufferedInputStream bis = null;
        RandomAccessFile rf = null;
        File f = new File("f:/test/java .txt");
        System.out.println(" 。。。");
        try {
            //   
            ss = new ServerSocket(8985);
            //   
            Socket st = ss.accept();
            //socket 
            is = st.getInputStream();
            // 
            bis = new BufferedInputStream(is);
            rf = new RandomAccessFile(f, "rw");
            byte b[] = new byte[1024];
            int len = 0;
            // 
            while ((len = bis.read(b)) != -1) {
                rf.write(b, 0, len);
                rf.skipBytes(len);

            }
            System.out.println(" !");
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            try {
                if (fis != null)
                    fis.close();
                if (rf != null)
                    rf.close();
                if (ss != null)
                    ss.close();
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {
        fileup();

    }

}
클라이언트:
package com.softeem.scokt;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

public class SocketFileClient
{

    public static void FileClient() {
        Socket st = null;
        BufferedOutputStream bos = null;
        FileInputStream fis = null;
        System.out.println(" 。。。");
        try {
            // 
            st = new Socket(InetAddress.getLocalHost(), 8985);
            // 
            File f = new File("F:/a/a.txt");
            bos = new BufferedOutputStream(st.getOutputStream());
            fis = new FileInputStream(f);
            int len = 0;
            byte b[] = new byte[1024];
            // 
            while ((len = fis.read(b)) != -1) {
                bos.write(b, 0, len);
                bos.flush();
            }
            System.out.println(" !");
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            try {
                // 
                fis.close();
                bos.close();
                st.close();
            }
            catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args) {
        FileClient();
    }

}

좋은 웹페이지 즐겨찾기