黑马程序员技术交流社区

标题: daima [打印本页]

作者: heqinghui    时间: 2019-10-10 13:26
标题: daima
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();
    }
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2