Java基础知识之多线程并行执行上传图片操作

一首先看客户端:

public class uploadpicThreadClient {
    public static void main(String[] args) throws IOException {
        //1、创建客户端socket
        Socket socket = new Socket("127.0.0.1", 8080);

        //2、读取客户端要上传的图片
        FileInputStream fis = new FileInputStream("E:\\5.JPG");

        //3、获取socket输出流,将读到的图片数据发给服务端
        OutputStream outputStream = socket.getOutputStream();
        byte[] buf = new byte[1024];

        int len = 0;
        //使用输出流写出图片数据
        while ((len = fis.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }

        // 告诉服务端,客户端数据已发送完毕,服务端停止读取
        socket.shutdownOutput();

        //读取服务端返回的数据
        InputStream inputStream = socket.getInputStream();
        //缓冲区存放数据提高读写速度
        byte[] bufIn = new byte[1024];

        int lenIn = inputStream.read(buf);
        String text = new String(buf, 0, lenIn);
        System.out.println(text);

        //关闭资源
        fis.close();
        socket.close();
    }
}

服务端 :
服务端一定是一直处于待机状态,当有图片上传需求时,就接受需求。

public class uploadpicThreadServer {
    public static void main(String[] args) throws IOException {
        //创建TCP的socket服务端
        ServerSocket ss =new ServerSocket(8080);
        while (true){
            //获取客户端
            //阻塞式方法,有任务时就开启,否则一直处于等待状态
            Socket socket = ss.accept();
            new Thread(new UploadTask(socket)).start();
        }
    }
}

线程任务:
获得连接的IP、处理发送过来的图片数据等,这些都是每个线程需要执行的任务,所以需放在线程的run( )方法中执行。

public class UploadTask implements Runnable {
    //规定上传文件的最大大小
    private static final int SIZE = 1024*1024*2;
    private Socket socket;

    public UploadTask(Socket socket) {
        this.socket=socket;
    }
    @Override
    public void run() {
        int count=0;
        String ip = socket.getInetAddress().getHostAddress();
        System.out.println(ip+".........connected");
        try {
            //读取客户端发来的数据
            InputStream inputStream = socket.getInputStream();

            //将读取到的文件存储到一个文件中
            File dir =new File("E:\\image");
            if (!dir.exists()){
                dir.mkdirs();
            }
            File file =new File(dir,ip+".jpg");
            //如果文件已经存在服务端,就文件名IP在原来的基础上+1
            while (file.exists()){
                file = new File(dir,ip+"("+(++count)+").jpg");
            }
            FileOutputStream fos =new FileOutputStream(file);

            byte[] buf=new byte[1024];

            int len=0;
            while ((len=inputStream.read(buf))!=-1){

                fos.write(buf,0,len);
                if (file.length()>SIZE){
                    System.out.println(ip+"文件体积过大");
                    fos.close();
                    socket.close();

                    System.out.println(ip+"....."+file.delete());
                    return;
                }
            }
            //获取socket输出流,将上传成功字样发给服务端
            OutputStream outputStream = socket.getOutputStream();
            outputStream.write("上传成功".getBytes());
            fos.close();
            socket.close();
        }catch (IOException e){
        }
    }
}

测试

首先启动服务端 uploadpicThreadServe
再启动客户端 uploadpicThreadClient

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值