szu_zkjszu_zkj 2021-03-29 20:38 采纳率: 33.3%
浏览 35
已采纳

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条回答 默认 最新

  • 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();
      }
    }

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码
  • ¥80 LS dyna mpp并行报错
  • ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题,但部署到linux环境报错
  • ¥15 vue页面,node封装接口
  • ¥15 求TMS320F280039C工程模板!
  • ¥15 delphi+fastreport实现分组补空打印问题
  • ¥15 使用python把两台mysql数据库服务器数据导出和导入
  • ¥15 NodeBB论坛配置Apache Solr中文搜索引擎的详细教程