A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© heqinghui 初级黑马   /  2019-10-10 13:26  /  473 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public class Dati4Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(8888);
        Socket s = ss.accept();

        InputStream is = s.getInputStream();
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\IdeaProjects\\DayDayUP\\copy.jpg"));
        byte[] bys = new byte[1024];
        int len;
        while ((len = is.read(bys)) != -1) {
            bos.write(bys, 0, len);
            bos.flush();
        }

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

        bw.close();
        is.close();
        ss.close();
    }
}public class Dati4Client {
    public static void main(String[] args) throws IOException {
        Socket s = new Socket("127.0.0.1",8888);

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\tupian.jpg"));
        OutputStream os = s.getOutputStream();
        byte[] bys = new byte[1024];
        int len;
        while ((len=bis.read(bys))!=-1){
          os.write(bys,0,len);
        }
       s.shutdownOutput();

        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String s1 = br.readLine();
        System.out.println("服务器反馈"+s1);

        bis.close();
        br.close();
        s.close();
    }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马