最近学习java编程是一个实验让我困惑了很久就是:
编写实现类似FTP功能的程序,客户程序向服务器发送字符串”ls”,服务器返回几个文件名,客户程序从中选择一个文件,服务器将该文件发送给客户端,客户程序接受该文件并保存在本地硬盘中。(说明:不要求采用FTP协议实现)
下面把代码贴出来,可以实现文件传送,
传送文件中,服务器端吧文件对象内容写到byte数组中然后由socket发送
接收端主要也是读getinputstream然后保存到byte数组中,在写入到文件中
客户端程序:
package    conan;
import java.io.*;
import java.net.*;
class FTPClient extends Thread{
  Socket     sl;    
  DataOutputStream dos;
  DataInputStream dis;
   public static void main (String[] args) {
    FTPClient ftpc= new FTPClient();
    ftpc.start();    
}
public    String GetCom() throws Exception{
      System.out.println( "请输入命令: ");
      InputStreamReader is= new InputStreamReader(System.in);
        
       char[] buf= new char[100];    
       int len = is.read(buf);
      String str= new String(buf,0,len).trim();
       return str;
}
public    String GetFileName() throws Exception{ //获得输入的文件名
      System.out.println( "输入文件名: ");
      InputStreamReader is= new InputStreamReader(System.in);
       char[] buf= new char[100];
       int len2=is.read(buf);
      String str= new String(buf,0,len2).trim();    
       return str;
}
public     void    receiveFile(Socket s) throws Exception     //接收文件
                    {                        
                      sl= s;
                      String filename=GetFileName();
                      dos= new DataOutputStream(sl.getOutputStream());
      dos.writeUTF(filename);
      System.out.println( "发送文件名成功");
                      File f= new File(File.separator); //当前根目录
                      File file = new File(f, "Downloads"+File.separator+filename); //保存到当前目录得Downloads文件里面                    
                      sl= new Socket( "127.0.0.1",5432);     //创建一个socket对象用来传送文件            
                      BufferedOutputStream buff= new BufferedOutputStream( new FileOutputStream(file));
                      dis= new DataInputStream(sl.getInputStream());
                       byte[]    line= new     byte[102400];    
                       int mount;
                      System.out.println( "开始接收文件");
                      System.out.println( "文件传送中....");
                         while((mount=dis.read(line))!=-1){
                          buff.write(line,0,mount);
                          buff.flush(); //刷新    
                          Thread.sleep(50); //设置一个等待时间,有利于接收文件
                        }
                        sl.close();
                        buff.close();
                        System.out.println( "finished");
}
public void run(){
   try{
      Socket sc;
      sc= new Socket( "localhost",4321);
      dos= new DataOutputStream(sc.getOutputStream());
      dos.writeUTF(GetCom());
      dis= new DataInputStream(sc.getInputStream());
       for( int i=0;i<30;i++){
        System.out.println(dis.readUTF());    
      }
      receiveFile(sc);
    }
     catch(Exception e){
      e.printStackTrace();
    }    
}
}
服务器端程序:
package conan;
import    java.net.*;
import    java.io.*;
class FTPServer extends Thread
{    
  ServerSocket server,server1;
  Socket s;
  DataInputStream dis;
  DataOutputStream dos;
    
   public static void main (String[] args) {
    FTPServer ftps= new FTPServer();
    ftps.start();
}
public     void sendFile(String filename) throws Exception
                    {
                       try{    
                        server1= new ServerSocket(5432);
                        s=server1.accept();
                        File path= new File(File.separator); //当前根目录
                        File file= new File(path,filename); //创建该文件对象
                        System.out.println( "将文件独到缓冲流中");
                        BufferedInputStream buffile= new BufferedInputStream( new FileInputStream(file));
                         byte[] buf= new byte[102400]; //每次发送的块打大小
                         int mount;
                        dos= new DataOutputStream(s.getOutputStream());
                        System.out.println( "开始传送文件");
                         while((mount=buffile.read(buf))!=-1){ //传送文件,将文件内容以字节形式保存到buf中再发送
                          dos.write(buf,0,mount);
                          Thread.sleep(50);                                
                        }                        
                        System.out.println( "文件传送完毕");
                        server1.close();
                        s.close(); //关闭清理工作
                        buffile.close();            
                      }
                       catch(Exception e){
                        e.printStackTrace();
                      }
}
public void run(){
   try{
    server= new ServerSocket(4321); //4321端口用来接接收命令
    System.out.println( "Listen at 4321 port");
     while( true)
    {
      s=server.accept(); //监听
      dis= new DataInputStream(s.getInputStream());
       byte [] buf= new byte[100];
       int len=dis.read(buf);
      String str= new String(buf,0,len).trim();
      System.out.println( "接收命令成功");
       if(str.startsWith( "ls")){
        File localfile= new File(File.separator);
        String[] filelist=localfile.list();
        dos= new DataOutputStream(s.getOutputStream()); //输出文件列表
        dos.writeUTF( "-------------------File    List--------------------");
         for( int i=0;i<30;i++){
          dos.writeUTF(filelist[i]);
        }
      }
      dis= new DataInputStream(s.getInputStream());
      String filename_from_client=dis.readUTF().trim();
      System.out.println( "接收文件名成功");
      System.out.println( "传送得文件名 :"+filename_from_client);
      sendFile(filename_from_client);
        

    }
  }
   catch(Exception ex){
    ex.printStackTrace();
  }
}
}
 
终于把实验做完了,从中也学到不少socket和io得知识,还真不错。