当前位置: 首页 > 面试题库 >

用于Java中文件传输的FTP客户端服务器模型

松安民
2023-03-14
问题内容

好吧,我正在尝试用Java实现ftp服务器和ftp客户端。我正在尝试从服务器接收文件。以下是代码行。我能够实现服务器与客户端之间的连接,但是也无法将文件名发送到服务器。谁能指导我这种方法是否正确,请提出正确的建议。

服务器的实现:

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

class MyServer {
    ServerSocket ss;
    Socket clientsocket;
    BufferedReader fromclient;
    InputStreamReader isr;
    PrintWriter toclient;

    public MyServer() {
        String str = new String("hello");
        try {
            // Create ServerSocket object.
            ss = new ServerSocket(1244);
            System.out.println("Server Started...");
            while(true) {
                System.out.println("Waiting for the request...");

                // accept the client request.
                clientsocket = ss.accept();

                System.out.println("Got a client");
                System.out.println("Client Address " + clientsocket.getInetAddress().toString());
                isr = new InputStreamReader(clientsocket.getInputStream());
                fromclient = new BufferedReader(isr);
                toclient = new PrintWriter(clientsocket.getOutputStream());

                String strfile;
                String stringdata;
                boolean file_still_present = false;

                strfile = fromclient.readLine();

                System.out.println(strfile);
                //toclient.println("File name received at Server is  " + strfile);

                File samplefile = new File(strfile);
                FileInputStream fileinputstream = new FileInputStream(samplefile);
                // now ready to send data from server ..... 
                int notendcharacter;
                do {
                    notendcharacter = fileinputstream.read();
                    stringdata = String.valueOf(notendcharacter);
                    toclient.println(stringdata);

                    if (notendcharacter != -1) {
                        file_still_present = true;
                    } else {
                        file_still_present = false;
                    }
                } while(file_still_present);

                fileinputstream.close();    
                System.out.println("File has been send successfully .. message print from server");

                if (str.equals("bye")) {
                  break;
                }

                fromclient.close();
                toclient.close();
                clientsocket.close();
            }
        } catch(Exception ex) {
            System.out.println("Error in the code : " + ex.toString());
        }
    }

    public static void main(String arg[]) {
        MyServer serverobj = new MyServer();
    }
}

客户实施:

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

class MyClient {
    Socket soc;
    BufferedReader fromkeyboard, fromserver;
    PrintWriter toserver;
    InputStreamReader isr;

    public MyClient() {
        String str;
        try {
            // server is listening on this port.
            soc = new Socket("localhost", 1244);

            fromkeyboard = new BufferedReader(new InputStreamReader(System.in));
            fromserver = new BufferedReader(new InputStreamReader(soc.getInputStream()));

            System.out.println("PLEASE ENTER THE MESSAGE TO BE SENT TO THE SERVER");
            str = fromkeyboard.readLine();
            System.out.println(str);
            String ddd;
            ddd = str;
            toserver = new PrintWriter(soc.getOutputStream());

            String strfile;
            int notendcharacter;
            boolean file_validity = false;
            System.out.println("send to server" + str);

            System.out.println("Enter the filename to be received from server");
            strfile = fromkeyboard.readLine();
            toserver.println(strfile);

            File samplefile = new File(strfile);
            //File OutputStream helps to get write the data from the file ....
            FileOutputStream fileOutputStream = new FileOutputStream(samplefile);

            // now ready to get the data from server .... 
            do {
                str = fromserver.readLine();
                notendcharacter = Integer.parseInt(str);

                if (notendcharacter != -1) {
                    file_validity = true;
                } else {
                    System.out.println("Read and Stored all the Data Bytes from the file ..." +
                        "Received File Successfully");
                }
                if (file_validity) {
                    fileOutputStream.write(notendcharacter);
                }
            } while(file_validity);

            fileOutputStream.close();

            toserver.close();
            fromserver.close();
            soc.close();
        } catch(Exception ex) {
            System.out.println("Error in the code : " + ex.toString());
        }
    }

    public static void main(String str[]) {
        MyClient clientobj = new MyClient();
    }
}

问题答案:

上述问题的答案是:

FTP客户端:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class FileClient {
    public static void main(String[] args) throws Exception {

        long start = System.currentTimeMillis();

        // localhost for testing
        Socket sock = new Socket("127.0.0.1", 13267);
        System.out.println("Connecting...");
        InputStream is = sock.getInputStream();
        // receive file
        new FileClient().receiveFile(is);
        OutputStream os = sock.getOutputStream();
        //new FileClient().send(os);
        long end = System.currentTimeMillis();
        System.out.println(end - start);

        sock.close();
    }


    public void send(OutputStream os) throws Exception {
        // sendfile
        File myFile = new File("/home/nilesh/opt/eclipse/about.html");
        byte[] mybytearray = new byte[(int) myFile.length() + 1];
        FileInputStream fis = new FileInputStream(myFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(mybytearray, 0, mybytearray.length);
        System.out.println("Sending...");
        os.write(mybytearray, 0, mybytearray.length);
        os.flush();
    }

    public void receiveFile(InputStream is) throws Exception {
        int filesize = 6022386;
        int bytesRead;
        int current = 0;
        byte[] mybytearray = new byte[filesize];

        FileOutputStream fos = new FileOutputStream("def");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = is.read(mybytearray, 0, mybytearray.length);
        current = bytesRead;

        do {
            bytesRead = is.read(mybytearray, current,
                    (mybytearray.length - current));
            if (bytesRead >= 0)
                current += bytesRead;
        } while (bytesRead > -1);

        bos.write(mybytearray, 0, current);
        bos.flush();
        bos.close();
    }
}

FTP服务器:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class FileServer {
    public static void main(String[] args) throws Exception {
        // create socket
        ServerSocket servsock = new ServerSocket(13267);
        while (true) {
            System.out.println("Waiting...");

            Socket sock = servsock.accept();
            System.out.println("Accepted connection : " + sock);
            OutputStream os = sock.getOutputStream();
            //new FileServer().send(os);
            InputStream is = sock.getInputStream();
            new FileServer().receiveFile(is);
            sock.close();
        }
    }

    public void send(OutputStream os) throws Exception {
        // sendfile
        File myFile = new File("/home/nilesh/opt/eclipse/about.html");
        byte[] mybytearray = new byte[(int) myFile.length() + 1];
        FileInputStream fis = new FileInputStream(myFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(mybytearray, 0, mybytearray.length);
        System.out.println("Sending...");
        os.write(mybytearray, 0, mybytearray.length);
        os.flush();
    }

    public void receiveFile(InputStream is) throws Exception {
        int filesize = 6022386;
        int bytesRead;
        int current = 0;
        byte[] mybytearray = new byte[filesize];

        FileOutputStream fos = new FileOutputStream("def");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = is.read(mybytearray, 0, mybytearray.length);
        current = bytesRead;

        do {
            bytesRead = is.read(mybytearray, current,
                    (mybytearray.length - current));
            if (bytesRead >= 0)
                current += bytesRead;
        } while (bytesRead > -1);

        bos.write(mybytearray, 0, current);
        bos.flush();
        bos.close();
    }
}


 类似资料:
  • 问题内容: 我应该为“ .thrift”文件定义哪种服务,以便以后将其用于我的程序? 此文件传输应该在客户端和服务器之间,并且应该是“部分”。 StreamFileService.thrift: StreamFileClient.java: } StreamFileServer.java: } StreamFileServiceImpl: } 问题答案: 您的代码对我来说似乎还不错(未经测试),没

  • 本文向大家介绍java模拟客户端向服务器上传文件,包括了java模拟客户端向服务器上传文件的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了java客户端向服务器上传文件的具体代码,供大家参考,具体内容如下 先来了解一下客户端与服务器Tcp通信的基本步骤: 服务器端先启动,然后启动客户端向服务器端发送数据。 服务器端收到客户端发送的数据,服务器端会响应应客户端,向客户端发送响应结果。

  • 问题内容: 我正在尝试在服务器和客户端之间进行文件传输,但是工作非常糟糕。基本上需要发生的是: 1)客户端将txt文件发送到服务器(我称为“ quotidiani.txt”) 2)服务器将其保存在另一个txt文件中(“ receive.txt”) 3)服务器运行脚本上对其进行修改并以其他名称保存(“ output.txt”)的脚本 。4)服务器将文件发送回客户端,客户端以相同的名称(final.t

  • 本文向大家介绍Java实现文件上传服务器和客户端,包括了Java实现文件上传服务器和客户端的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了Java实现文件上传服务器和客户端的具体代码,供大家参考,具体内容如下 文件上传服务器端: 文件上传客户端: 本文已被整理到了《Java上传操作技巧汇总》,欢迎大家学习阅读。  以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐

  • 我有客户端服务器聊天 客户端发送文件,服务器接收它们。但是,问题是,我不认为文件被正确接收,因为当我检查文件的大小时,我看到由于某些原因差异是一半! 我正在使用GUI在客户端浏览文件,然后我向服务器发送命令以知道客户端正在发送文件。但是它不工作 这里是客户端和服务器 计算机网络服务器 有人能帮我解决这个问题吗?因为我不知道如何正确发送和接收文件 谢谢你

  • 我想在一些计算机之间建立点对点连接,这样用户就可以在没有外部服务器的情况下聊天和交换文件。我最初的想法如下: 我在服务器上制作了一个中央服务器插座,所有应用程序都可以连接到该插座。此ServerSocket跟踪已连接的套接字(客户端),并将新连接的客户端的IP和端口提供给所有其他客户端。每个客户端都会创建一个新的ServerSocket,所有客户端都可以连接到它。 换句话说:每个客户端都有一个Se