TCP-C/S構造で、クライアントがサーバ側にファイルを送信し、指定したパスとファイル名で保存する

16279 ワード

クライアントはサービス側にファイルを送信し、送信時にこのパスと保存する名前を指定します.サービス側が受信した後、クライアントから送信された情報に基づいて、指定されたパスにデータを保存します.例えば、クライアントが送信した構造は、dir、filename、dataサービス側が受信した後、対応するパスに基づいてクライアントを保存します.
public class TCPClient {
     
    public static void main(String[] args) {
     
        //    Fiel  。               
        File file = new File("X:\\XX\\XXX\\java.txt");
        //       
        Socket socket = null;
        //           
        FileInputStream fis = null;
        //          
        ObjectOutputStream oos = null;
        try {
     
            //       ,       ,   IP  ,    
            socket = new Socket("192.168.0.0", 8080);
            //        ,       file  
            fis = new FileInputStream(file);
            //      ,        
            byte[] bytes = new byte[1024];
            //    
            fis.read(bytes);
            //      ,            ,             
            Path path = new Path("X:\\XX\\XX", "java.txt", bytes);
            //         ,       ,   socket          
            oos = new ObjectOutputStream(socket.getOutputStream());
            //      ,   ,          
            oos.writeObject(path);
        } catch (IOException e) {
     
            e.printStackTrace();
        } finally {
     
            try {
     
                //    
                oos.close();
                fis.close();
                socket.close();
            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }
    }
}
//      ,   SeriaLiable  ,     
class Path implements Serializable {
      
    //         
    String dir;//       
    String filename;//   
    byte[] data;//    

    //    
    public Path() {
     
    }

    //    
    public Path(String dir, String filename, byte[] data) {
     
        this.dir = dir;
        this.filename = filename;
        this.data = data;
    }
}

サーバ側:
public class TCPServer {
     
    public static void main(String[] args) {
     
        //       
        ServerSocket server = null;
        //           
        FileOutputStream fos = null;
        //            
        ObjectInputStream ois = null;
        try {
     
            //       ,           
            server = new ServerSocket(8080);
            //          ,              socket
            Socket socket = server.accept();
            //         ,                    
            ois = new ObjectInputStream(socket.getInputStream());
            //         ,          
            Object readObject = ois.readObject();
            //     
            Path path = (Path) readObject;
            //    File  ,               
            File file = new File(path.dir);
            //file            ,   ,    ,     
            file.mkdirs();
            //         ,                     
            fos = new FileOutputStream(file + File.separator + path.filename);
            //      
            fos.write(path.data);
        } catch (ClassNotFoundException e){
     
            e.printStackTrace();
        }catch (IOException e) {
     
            e.printStackTrace();
        } finally {
     
            try {
     
                //    
                fos.close();
                ois.close();
                server.close();
            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }
    }
}

以上の方法は、サーバの書き込みなど、継続的に改善することができます.