java网络编程之简单文件上传【二】

客户端:

package com.hanyxx.socket;

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

/**
 * @author layman
 */
public class FileUPload_Client {
    public static void main(String[] args) throws IOException {
        // 创建输入流,读取本地文件
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("food.jpg"));
        // 创建输出流,写入到服务端
        Socket socket = new Socket("localhost", 6666);
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());

        // 写出数据
        byte[] b  = new byte[1024];
        int len ;
        while (( len  = bis.read(b)) != -1) {
            bos.write(b, 0, len);
            bos.flush();
        }
        // 关闭输出流,通知服务端,写出数据完毕
        socket.shutdownOutput(); // 这个方法会避免read方法读不到结束标记 -1,出现死循环的情况

        System.out.println("文件发送完毕");
        // =====解析回写============
        InputStream in = socket.getInputStream();
        len = in.read(b);
        System.out.println(new String(b,0,len));
        in.close();
        
        // 释放资源
        socket.close();
        bis.close();
    }
}

服务器端:

package com.hanyxx.socket;

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

/**
 * @author layman
 */
public class FileUpload_Server {
    public static void main(String[] args) throws IOException {
        System.out.println("服务器 启动.....  ");
        // 创建服务端ServerSocket
        ServerSocket serverSocket = new ServerSocket(6666);
        File file = new File("copy");
        if(!file.exists()){
            file.mkdir();
        }
        // 循环接收,建立连接
        while (true) {
            Socket accept = serverSocket.accept();
          	 // socket对象交给子线程处理,进行读写操作,使用lambda表达式简化Runnable接口中的run方法
            new Thread(() -> {
                try (
                       BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
                        FileOutputStream fis = new FileOutputStream(file+"\\"+System.currentTimeMillis() + ".jpg");
                        BufferedOutputStream bos = new BufferedOutputStream(fis);
                ) {
                    // 读写数据
                    byte[] b = new byte[1024];
                    int len;
                    while ((len = bis.read(b)) != -1) {
                        bos.write(b, 0, len);
                    }

                    // =======信息回写===========================
                    System.out.println("back ........");
                    OutputStream out = accept.getOutputStream();
                    out.write("上传成功".getBytes());
                    out.close();
                    //================================

                    //关闭 资源
                    bos.close();
                    bis.close();
                    accept.close();
                    System.out.println("文件上传已保存");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
posted @ 2021-03-17 09:34  layman~  阅读(33)  评论(0)    收藏  举报