提问者:小点点

服务器客户端文件传输程序出错?


服务器类:

import java.net.*;
import java.io.*;
public class fileserver {
    public static void main(String args []) {
        ServerSocket ss=new ServerSocket(2345);
        Socket s= ss.accept();
        FileInputStream f=new FileInputStream("D:\\FEATURED.txt");
        DataOutputStream dout= new DataOutputStream(s.getOutputStream);
        byte[] b=new byte[2002];
        f.read(b,0,b.length);
        dout.write(b,0,b.length);
        dout.close();
        f.close();
        s.close();
    }   
}
    

客户端类:

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

public class clientserver {
    public static void main(String args []) {
      Socket s=new Socket("localhost",2345);
      FileOutputStream f=new FileOutputStream("E:\\FEATUREDCOPIED.txt");
      DataInputStream din= new DataInputStream(s.getInputStream);
      byte[] b=new byte[2002];
      din.read(b,0,b.length);
      f.write(b,0,b.length);
        din.close();
        f.close();
        s.close();
    }
}

错误:

  1. GetOutputStream无法解析或不是服务器中的字段
  2. GetInputStream无法解析或不是客户端中的字段

请帮助我解决此查询。


共3个答案

匿名用户

注意偏执狂,因为您调用了套接字中的方法:S.GetOutputStream**()**S.GetInputStream**()**

匿名用户

GetInputStreamGetOutputStream是方法,因此必须调用它们,这意味着在它们的名称后面加上括号:S.GetInputStream(),而不只是S.GetInputStream

匿名用户

**更正代码**

服务器类:

import java.net.*;
import java.io.*;
public class fileserver {
    public static void main(String args []) {
     try {
            ServerSocket ss=new ServerSocket(2345);
            Socket s= ss.accept();
            FileInputStream f=new FileInputStream("D:\\FEATURED.txt");
            DataOutputStream dout= new DataOutputStream(s.getOutputStream());
            byte[] b=new byte[2002];
            f.read(b,0,b.length);
            dout.write(b,0,b.length);
            dout.close();
            f.close();
            s.close();
        }
     catch(Exception e) {
         System.out.println(e.getMessage());
     }
    }
}

客户端类:

import java.io.*;
import java.net.*;
import java.lang.*;

public class clientserver {
    public static void main(String args []) {
     try {
        Socket s=new Socket("localhost",2345);
        FileOutputStream f=new FileOutputStream("E:\\FEATUREDCOPIEDAGAINs.txt");
        DataInputStream din= new DataInputStream(s.getInputStream());
        byte[] b=new byte[2002];
        din.read(b,0,b.length);
        f.write(b,0,b.length);
        din.close();
        f.close();
        s.close();
     }
     catch(Exception e) {
         System.out.println(e.getMessage());
     }
    }
}