简体   繁体   English

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

[英]FTP client server model for file transfer in Java

Well, I am trying to implement the ftp server and ftp client in Java. 好吧,我正在尝试用Java实现ftp服务器和ftp客户端。 I am trying to receive a file from server. 我正在尝试从服务器接收文件。 Following is line of codes. 以下是代码行。 I am able to achieve Connection between server and client, but unable to send filename to server also. 我能够实现服务器与客户端之间的连接,但是也无法将文件名发送到服务器。 Well can anyone guide me whether this approach is correct or if not, please suggest proper changes. 谁能指导我这种方法是否正确,请提出适当的建议。

Server's Implementation: 服务器的实现:

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

Client's Implementation: 客户的实施:

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

The answer to the above question is : 上述问题的答案是:

FTP Client : 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 Server : 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();
    }
} 

> >

Server side 服务器端

import java.io.*;

import java.net.*;

class serversvi

{

public static void main(String svi[])throws IOException

{

try

{

ServerSocket servsock=new ServerSocket(105);

DataInputStream dis=new DataInputStream(System.in);

System.out.println("enter the file name");

String fil=dis.readLine();

System.out.println(fil+" :is file transfer");

File myfile=new File(fil);

while(true)

{

Socket sock=servsock.accept();

byte[] mybytearray=new byte[(int)myfile.length()];

BufferedInputStream bis=new BufferedInputStream(new FileInputStream(myfile));

bis.read(mybytearray,0,mybytearray.length);

OutputStream os=sock.getOutputStream();

os.write(mybytearray,0,mybytearray.length);

os.flush();

sock.close();

}

}


catch(Exception saranvi)

{

System.out.print(saranvi);

}

}

}

> 

Client side: 客户端:

import java.io.*;

import java.net.*;

class clientsvi

{

public static void main(String svi[])throws IOException

{

try

{

Socket sock=new Socket("localhost",105);

byte[] bytearray=new byte[1024];

InputStream is=sock.getInputStream();

DataInputStream dis=new DataInputStream(System.in);

System.out.println("enter the file name");

String fil=dis.readLine();

FileOutputStream fos=new FileOutputStream(fil);

BufferedOutputStream bos=new  BufferedOutputStream(fos);

int bytesread=is.read(bytearray,0,bytearray.length);

bos.write(bytearray,0,bytesread);

System.out.println("out.txt file is received");

bos.close();

sock.close();

}

catch(Exception SVI)

{

System.out.print(SVI);

}

}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM