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

你可能感兴趣的:(Log--JAVA)