网络编程Socket文件上传到服务器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SS__FF/article/details/78915244

服务器:

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();
    }

}



猜你喜欢

转载自blog.csdn.net/SS__FF/article/details/78915244