使用不带FTP协议的java将文件从windows复制到远程linux计算机

使用不带FTP协议的java将文件从windows复制到远程linux计算机,java,linux,windows,io,Java,Linux,Windows,Io,是否有任何方法可以使用java而不使用FTP协议将文件从windows复制到远程linux计算机?使用java套接字传输文件 服务器/发送方 import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; imp

是否有任何方法可以使用java而不使用FTP协议将文件从windows复制到远程linux计算机?

使用java套接字传输文件

服务器/发送方

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Sender {
    public static Scanner scanner;
    /**
     * 
     * @param args
     * this function collect all required data from user.
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException
    {
        String fileLocation;
        int portNo;
        scanner=new Scanner(System.in);
        System.out.println("Enter port number of machine(e.g. '2000') :");
        portNo=scanner.nextInt();
        System.out.println("Please enter file location with file name (e.g. 'D:\\abc.txt'): ");
        fileLocation=scanner.next();
         Sender.send(portNo,fileLocation);
    }
    /**
     * this function actually transfers file
     * @param portNo
     * @param fileLocation
     * @return
     * @throws IOException 
     */
    public static  void send(int portNo,String fileLocation) throws IOException
    {

        FileInputStream fileInputStream = null;
        BufferedInputStream bufferedInputStream = null;

        OutputStream outputStream = null;
        ServerSocket serverSocket = null;
        Socket socket = null;

        //creating connection between sender and receiver
        try {
            serverSocket = new ServerSocket(portNo);
            System.out.println("Waiting for receiver...");
                try {
                        socket = serverSocket.accept();
                        System.out.println("Accepted connection : " + socket);
                        //connection established successfully

                        //creating object to send file
                        File file = new File (fileLocation);
                        byte [] byteArray  = new byte [(int)file.length()];
                        fileInputStream = new FileInputStream(file);
                        bufferedInputStream = new BufferedInputStream(fileInputStream);
                        bufferedInputStream.read(byteArray,0,byteArray.length); // copied file into byteArray

                        //sending file through socket
                        outputStream = socket.getOutputStream();
                        System.out.println("Sending " + fileLocation + "( size: " + byteArray.length + " bytes)");
                        outputStream.write(byteArray,0,byteArray.length);           //copying byteArray to socket
                        outputStream.flush();                                       //flushing socket
                        System.out.println("Done.");                                //file has been sent
                    }
                    finally {
                        if (bufferedInputStream != null) bufferedInputStream.close();
                        if (outputStream != null) bufferedInputStream.close();
                        if (socket!=null) socket.close();
                    }       
            } catch (IOException e) {

                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally {
                if (serverSocket != null) serverSocket.close();
            }
    }
}
客户/接收人

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Scanner;

public class Receiver {


    public static Scanner scanner;


    public static void main (String [] args ) throws IOException {


        String fileLocation,ipAddress;
        int portNo;
        scanner=new Scanner(System.in);
        System.out.println("Enter ipAddress of machine(if you are testing this on same machine than enter 127.0.0.1) :");
        ipAddress=scanner.next();

        System.out.println("Enter port number of machine(e.g. '2000') :");
        portNo=scanner.nextInt();
        System.out.println("Please enter file location with file name to save (e.g. 'D:\\abc.txt'): ");     //you can modify this program to receive file name from server and then you can skip this step
        fileLocation=scanner.next();
        Receiver.receiveFile(ipAddress, portNo, fileLocation);


    }
    public static void receiveFile(String ipAddress,int portNo,String fileLocation) throws IOException
    {

        int bytesRead=0;
        int current = 0;
        FileOutputStream fileOutputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        Socket socket = null;
        try {

            //creating connection.
            socket = new Socket(ipAddress,portNo);
            System.out.println("connected.");

            // receive file
            byte [] byteArray  = new byte [6022386];                    //I have hard coded size of byteArray, you can send file size from socket before creating this.
            System.out.println("Please wait downloading file");

            //reading file from socket
            InputStream inputStream = socket.getInputStream();
            fileOutputStream = new FileOutputStream(fileLocation);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            bytesRead = inputStream.read(byteArray,0,byteArray.length);                 //copying file from socket to byteArray

            current = bytesRead;
            do {
                bytesRead =inputStream.read(byteArray, current, (byteArray.length-current));
                if(bytesRead >= 0) current += bytesRead;
            } while(bytesRead > -1);
            bufferedOutputStream.write(byteArray, 0 , current);                         //writing byteArray to file
            bufferedOutputStream.flush();                                               //flushing buffers

            System.out.println("File " + fileLocation  + " downloaded ( size: " + current + " bytes read)");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            if (fileOutputStream != null) fileOutputStream.close();
            if (bufferedOutputStream != null) bufferedOutputStream.close();
            if (socket != null) socket.close();
        }
    }
}

使用java套接字传输文件

服务器/发送方

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Sender {
    public static Scanner scanner;
    /**
     * 
     * @param args
     * this function collect all required data from user.
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException
    {
        String fileLocation;
        int portNo;
        scanner=new Scanner(System.in);
        System.out.println("Enter port number of machine(e.g. '2000') :");
        portNo=scanner.nextInt();
        System.out.println("Please enter file location with file name (e.g. 'D:\\abc.txt'): ");
        fileLocation=scanner.next();
         Sender.send(portNo,fileLocation);
    }
    /**
     * this function actually transfers file
     * @param portNo
     * @param fileLocation
     * @return
     * @throws IOException 
     */
    public static  void send(int portNo,String fileLocation) throws IOException
    {

        FileInputStream fileInputStream = null;
        BufferedInputStream bufferedInputStream = null;

        OutputStream outputStream = null;
        ServerSocket serverSocket = null;
        Socket socket = null;

        //creating connection between sender and receiver
        try {
            serverSocket = new ServerSocket(portNo);
            System.out.println("Waiting for receiver...");
                try {
                        socket = serverSocket.accept();
                        System.out.println("Accepted connection : " + socket);
                        //connection established successfully

                        //creating object to send file
                        File file = new File (fileLocation);
                        byte [] byteArray  = new byte [(int)file.length()];
                        fileInputStream = new FileInputStream(file);
                        bufferedInputStream = new BufferedInputStream(fileInputStream);
                        bufferedInputStream.read(byteArray,0,byteArray.length); // copied file into byteArray

                        //sending file through socket
                        outputStream = socket.getOutputStream();
                        System.out.println("Sending " + fileLocation + "( size: " + byteArray.length + " bytes)");
                        outputStream.write(byteArray,0,byteArray.length);           //copying byteArray to socket
                        outputStream.flush();                                       //flushing socket
                        System.out.println("Done.");                                //file has been sent
                    }
                    finally {
                        if (bufferedInputStream != null) bufferedInputStream.close();
                        if (outputStream != null) bufferedInputStream.close();
                        if (socket!=null) socket.close();
                    }       
            } catch (IOException e) {

                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally {
                if (serverSocket != null) serverSocket.close();
            }
    }
}
客户/接收人

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Scanner;

public class Receiver {


    public static Scanner scanner;


    public static void main (String [] args ) throws IOException {


        String fileLocation,ipAddress;
        int portNo;
        scanner=new Scanner(System.in);
        System.out.println("Enter ipAddress of machine(if you are testing this on same machine than enter 127.0.0.1) :");
        ipAddress=scanner.next();

        System.out.println("Enter port number of machine(e.g. '2000') :");
        portNo=scanner.nextInt();
        System.out.println("Please enter file location with file name to save (e.g. 'D:\\abc.txt'): ");     //you can modify this program to receive file name from server and then you can skip this step
        fileLocation=scanner.next();
        Receiver.receiveFile(ipAddress, portNo, fileLocation);


    }
    public static void receiveFile(String ipAddress,int portNo,String fileLocation) throws IOException
    {

        int bytesRead=0;
        int current = 0;
        FileOutputStream fileOutputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        Socket socket = null;
        try {

            //creating connection.
            socket = new Socket(ipAddress,portNo);
            System.out.println("connected.");

            // receive file
            byte [] byteArray  = new byte [6022386];                    //I have hard coded size of byteArray, you can send file size from socket before creating this.
            System.out.println("Please wait downloading file");

            //reading file from socket
            InputStream inputStream = socket.getInputStream();
            fileOutputStream = new FileOutputStream(fileLocation);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            bytesRead = inputStream.read(byteArray,0,byteArray.length);                 //copying file from socket to byteArray

            current = bytesRead;
            do {
                bytesRead =inputStream.read(byteArray, current, (byteArray.length-current));
                if(bytesRead >= 0) current += bytesRead;
            } while(bytesRead > -1);
            bufferedOutputStream.write(byteArray, 0 , current);                         //writing byteArray to file
            bufferedOutputStream.flush();                                               //flushing buffers

            System.out.println("File " + fileLocation  + " downloaded ( size: " + current + " bytes read)");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            if (fileOutputStream != null) fileOutputStream.close();
            if (bufferedOutputStream != null) bufferedOutputStream.close();
            if (socket != null) socket.close();
        }
    }
}

实现这一点的方法不止一种。您可以使用套接字、RMI等将文件从windows传输到linux或linux传输到windows您可以使用scp,使用JSch有多种方法可以实现这一点。您可以使用套接字、RMI等将文件从windows传输到linux或linux传输到windows。您可以使用JSch执行scp。此代码不起作用。客户端忽略流的结尾,因此它从不停止从连接读取数据。它还假设整个文件适合内存,并且其大小适合
int
。此代码不起作用。客户端忽略流的结尾,因此它从不停止从连接读取数据。它还假设整个文件适合内存,并且其大小适合
int