szu_zkjszu_zkj
szu_zkjszu_zkj
采纳率50%
2021-03-29 20:38 阅读 28
已采纳

java网络编程小问题 求助大佬

题目要求服务器端向客户端发送一个文件 客户端将文件保存并反馈“发送成功”给服务器端 

我的代码如下 为啥没法将文件内容写入到客户端文件夹里 求大佬帮忙 

public class tcptest {
    @Test
    public void client() throws IOException {
        Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),8990);

        InputStreamReader isr=new InputStreamReader(socket.getInputStream());
        BufferedReader br=new BufferedReader(isr);
        FileWriter fw=new FileWriter(new File("ClientData.txt"));


        String str=null;
        char i='1';

        while((str=br.readLine())!=null)
        {
            System.out.println("afhua");
            fw.write("aksnfdas");
            //fw.write(i+" "+str+'\n');
            i++;
        }
        System.out.println("knfkdsanf");

        BufferedOutputStream bos=new BufferedOutputStream(socket.getOutputStream());


        bos.write("接收成功".getBytes());

        fw.close();
        br.close();
        socket.close();
        bos.close();


    }
    @Test
    public void sever() throws IOException {

        ServerSocket ss=new ServerSocket(8990);

        Socket socket=ss.accept();

        OutputStreamWriter osw=new OutputStreamWriter(socket.getOutputStream());
        FileReader fr=new FileReader("SeverData.txt");
        BufferedReader br=new BufferedReader(fr);

          String str;

          while((str=br.readLine())!=null)
          {
              osw.write(str+'\n');
          }

          socket.shutdownOutput();
          BufferedInputStream bis=new BufferedInputStream(socket.getInputStream());
          ByteArrayOutputStream baos=new ByteArrayOutputStream();
          byte[] buffer=new byte[2];
          int len;
          while((len=bis.read(buffer))!=-1)
          {
              baos.write(buffer,0,len);
          }
        System.out.println(baos.toString());

          osw.close();
          br.close();
          socket.close();
          ss.close();
          bis.close();
          baos.close();
    }
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    technologist_15 CSDN专家-三岁丫 2021-03-29 23:35
    public class Test2 {
    
      public static void main(String[] args) throws Exception {
        ServerSocket ss=new ServerSocket(8990);
    
        Socket socket=ss.accept();
        OutputStreamWriter osw=new OutputStreamWriter(socket.getOutputStream());
        BufferedReader br=new BufferedReader(new FileReader("ServerData.txt"));
    
        String str;
        while((str=br.readLine())!=null)
        {
          osw.write(str);
          osw.flush();
        }
        socket.shutdownOutput();
    
        Reader reader = new InputStreamReader(new BufferedInputStream(socket.getInputStream()));
        char[] buffer=new char[1024];
        int len;
        while((len=reader.read(buffer)) != -1) {
          System.out.println(new String(buffer, 0, len));
        }
    
    
      }
    
    }
    public class Test {
    
      public static void main(String[] args) throws Exception {
        Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),8990);
    
        InputStreamReader isr=new InputStreamReader(socket.getInputStream());
        BufferedReader br=new BufferedReader(isr);
        FileWriter fw=new FileWriter("ClientData.txt");
    
        String str;
        while((str=br.readLine())!=null)
        {
          fw.write(str + "\n");
        }
        fw.flush();
    
        BufferedOutputStream bos=new BufferedOutputStream(socket.getOutputStream());
        bos.write("接收成功".getBytes());
        bos.flush();
    
        fw.close();
        br.close();
        socket.close();
        bos.close();
      }
    }

    做了一点调整,你对照着原来的代码看哪里改了吧

    点赞 评论 复制链接分享

相关推荐