将文件从一台Windows机器(本地)传输到另一台Windows机器(服务器)

时间:2016-04-07 11:15:43

标签: java ftp sftp ftp-client

我想编写一个代码,用于在Linux和Windows平台上将文件从一台机器传输到另一台机器。

我使用ssh库(sftp连接)将文件传输到Linux机器。

现在,我想为Windows机器做同样的事情。有人可以帮帮我吗?

描述:将文件从一台Windows机器(本地)传输到另一台Windows机器(服务器)。

另外,我在java中检查了FTP库,但是我无法在为ftp创建/共享的文件夹之外创建一个目录。

以下是我目前用于ftp的代码。

      FTPClient ftpClient = new FTPClient();  
      FileInputStream inputStream = null;  

      try {  
       // pass directory path on server to connect  
       ftpClient.connect("172.30.17.17");  

       // pass username and password, returned true if authentication is  
       // successful  
       boolean login = ftpClient.login("Administrator", "Password1!");  

       if (login) {  
        System.out.println("Connection established...");  
        inputStream = new FileInputStream("C:/Demo/abcd.txt");  

        boolean uploaded = ftpClient.storeFile("uploadedFile3.txt",inputStream);  
        if (uploaded) {  
         System.out.println("File uploaded successfully !");  
        } else {  
         System.out.println("Error in uploading file !");  
        }  

        ftpClient.makeDirectory("C:/Demo1"); //Unable to create this here

        System.out.println("Folder Created  successfully !");  


        // logout the user, returned true if logout successfully  
        boolean logout = ftpClient.logout();  
        if (logout) {  
         System.out.println("Connection close...");  
        }  
       } else {  
        System.out.println("Connection fail...");  
       }  

      } catch (SocketException e) {  
       e.printStackTrace();  
      } catch (IOException e) {  
       e.printStackTrace();  
      } finally {  
       try {  
        ftpClient.disconnect();  
       } catch (IOException e) {  
        e.printStackTrace();  
       }  
      }  
     }  

0 个答案:

没有答案