客户端需求:把一个图片文件发送到服务端并读取回馈信息。要求判断文件是否存在及格式是否为jpg并要求文件小于2M。 服务端需求:接收客户端发送过来的图片数据。进行存储后,回馈“上传成功”。支持多用户并发

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

public class MyThread implements Runnable {
    public Socket socket;

    public MyThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        BufferedInputStream bis=null;
        FileOutputStream fos = null;
        try {
            //        创建输出流
            fos = new FileOutputStream("d:\\ccc.png");

            InputStream inputStream = socket.getInputStream();
//        转换缓冲流
            bis = new BufferedInputStream(inputStream);

            int len;
            byte[] bytes = new byte[1024];
//        写文件
            while ((len = bis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("上传成功!");
            bw.flush();


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
===========================================================
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ServerDemo {
    public static void main(String[] args) throws IOException {
//        服务端需求:接收客户端发送过来的图片数据。进行存储后,
//        回馈一个“上传成功”字样。支持多的并发用户访问。

        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,//核心线程数量
                10,   //线程池的总数量
                60,   //临时线程空闲时间
                TimeUnit.SECONDS, //临时线程空闲时间的单位
                new ArrayBlockingQueue<>(5),//阻塞队列
                Executors.defaultThreadFactory(),//创建线程的方式
                new ThreadPoolExecutor.AbortPolicy()//任务拒绝策略
        );
//        开启服务器
        ServerSocket ss = new ServerSocket(10086);
        while (true) {
            Socket socket = ss.accept();
            MyThread m = new MyThread(socket);
//            new Thread(m).start();
            pool.submit(m);
        }
    }
}
=================================================
import java.io.*;
import java.net.Socket;

public class MyThread implements Runnable {
    public Socket socket;

    public MyThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        BufferedInputStream bis=null;
        FileOutputStream fos = null;
        try {
            //        创建输出流
            fos = new FileOutputStream("d:\\ccc.png");

            InputStream inputStream = socket.getInputStream();
//        转换缓冲流
            bis = new BufferedInputStream(inputStream);

            int len;
            byte[] bytes = new byte[1024];
//        写文件
            while ((len = bis.read(bytes)) != -1) {
                fos.write(bytes, 0, len);
            }

            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("上传成功!");
            bw.flush();


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

最好的期待,未来可期

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值