RMI传输文件

时间:2014-09-30 14:56:22

标签: java io

我接到了一个任务,要求我实现Java RMI,即客户端/服务器。它必须使用以下命令:

list - 列出当前目录中的文件 下载 - 将文件从服务器复制到客户端 上传 - 从客户端上传文件到服务器

我已经阅读了很多教程,但我没有得到任何明确的指导方针。 任何帮助将不胜感激

由于

这是我正在使用的教程http://www.censhare.com/en/insight/overview/article/file-streaming-using-java-rmi

1 个答案:

答案 0 :(得分:0)

您可以使用 apache commons 在ftp服务器上传文件,请试用此代码

import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.SocketException;

    import org.apache.commons.io.IOUtils;
    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPClient;

    public class Ftpdemo {  


         public static void main(String args[]) {  

          // get an ftpClient object  
          FTPClient ftpClient = new FTPClient();  
          ftpClient.setConnectTimeout(300);
          FileInputStream inputStream = null;  

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

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

           if (login) {  
            System.out.println("Connection established...");  
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            ftpClient.enterLocalPassiveMode();

            inputStream = new FileInputStream("image/path/mypic.png");  

            boolean uploaded = ftpClient.storeFile("user_screens/test3.png",inputStream);



                  if (uploaded) {  
             System.out.println("File uploaded successfully !");  
            } else {  
             System.out.println("Error in uploading file !");  
            }  

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


        }