JAVA-IO多线程图片上传


关键在于需要熟悉各种流的操作,上传文本或者图片或者视频的区别都在于输入输出流的不同,这是IO部分的内容。

自身在IO部分基础不是特别牢靠,还需要深入了解。

服务端

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

public class PicUploadServer {
    public static void main(String[] args) {
        try {
            ServerSocket ss = new ServerSocket(23333);
            while (true) {
                Socket s = ss.accept();
                //每当有客户端上传图片,开启一个线程处理
                new Thread(new PicDealThread(s)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class PicDealThread implements Runnable {
    private Socket s;

    PicDealThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        try {
            //利用ip地址作为图片文件名
            int index = 1;
            String ip = s.getInetAddress().getHostAddress();
            File file = new File(ip + ".jpg");
            //如果存在同名图片,使用数字后缀,直到不重名
            while (file.exists()) {
                file = new File(ip + "(" + (index++) + ")" + ".jpg");
            }
            //输出流,用于存储图片
            BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream(file));
            byte[] buf = new byte[1024];
            int len;
            InputStream in = s.getInputStream();
            while ((len = in.read(buf)) != -1) {
                bufos.write(buf, 0, len);
            }
            PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
            pw.println("上传图片成功!");
            bufos.close();
            in.close();
            s.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

客户端

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

public class PicUploadClient {
    public static void main(String[] args) {
        try {
            //通过输入路径选择图片
            BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
            String fileName = bufr.readLine();
            File file = new File(fileName);

            //必要的检测
            if (!(file.exists() && file.isFile())) {
                System.err.println("并不是一个文件!");
                return;
            }
            if (!file.getName().endsWith("jpg")) {
                System.err.println("图片格式错误,请重新选择!");
                return;
            }
            if (file.length() > 1024 * 1024 * 5) {
                System.err.println("图片大小不得超过5M!");
                return;
            }
            //连接服务器
            Socket s = new Socket("192.168.149.1", 23333);
            //读取文件
            BufferedInputStream bufIn = new BufferedInputStream(new FileInputStream(fileName));
            //用于向服务器写入字节的输出流
            OutputStream out = s.getOutputStream();
            byte[] buf = new byte[1024];
            int len;
            while ((len = bufIn.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            //关闭输出流,即上传完毕
            s.shutdownOutput();
            //获取服务器反馈信息
            String info;
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
            info = in.readLine();
            System.out.println(info);
            buf.clone();
            bufIn.close();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

END

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值