【问题标题】:Uploading to FTP using Java使用 Java 上传到 FTP
【发布时间】:2013-07-01 12:41:46
【问题描述】:

我只是想知道是否有一种简单方法可以将小文件上传到 ftp 服务器。我已经查看了 Apache Commons Net 库,但老实说这似乎相当复杂。有没有更简单的方法可以把小文件上传到ftp?

最终使用了 Apache Commons Net Library,并不太难。

【问题讨论】:

    标签: java ftp uploading


    【解决方案1】:

    从此链接:Upload files to FTP server using URLConnection class。无需外部库。

    String ftpUrl = "ftp://%s:%s@%s/%s;type=i";
    String host = "www.myserver.com";
    String user = "tom";
    String pass = "secret";
    String filePath = "E:/Work/Project.zip";
    String uploadPath = "/MyProjects/archive/Project.zip";
    
    ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath);
    System.out.println("Upload URL: " + ftpUrl);
    
    try {
        URL url = new URL(ftpUrl);
        URLConnection conn = url.openConnection();
        OutputStream outputStream = conn.getOutputStream();
        FileInputStream inputStream = new FileInputStream(filePath);
    
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = -1;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
    
        inputStream.close();
        outputStream.close();
    
        System.out.println("File uploaded");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    

    【讨论】:

    • 这很好 - 只是小的修正 - 用户和密码必须使用 URLEncoder.encode 函数进行编码
    【解决方案2】:

    我想,我找到了一个很好的使用 org.apache.commons.net.ftp.FTPClient here

    的示例
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPClient;
    
    /**
     * A program that demonstrates how to upload files from local computer
     * to a remote FTP server using Apache Commons Net API.
     * @author www.codejava.net
     */
    public class FTPUploadFileDemo {
    
        public static void main(String[] args) {
            String server = "www.myserver.com";
            int port = 21;
            String user = "user";
            String pass = "pass";
    
            FTPClient ftpClient = new FTPClient();
            try {
    
                ftpClient.connect(server, port);
                ftpClient.login(user, pass);
                ftpClient.enterLocalPassiveMode();
    
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    
                // APPROACH #1: uploads first file using an InputStream
                File firstLocalFile = new File("D:/Test/Projects.zip");
    
                String firstRemoteFile = "Projects.zip";
                InputStream inputStream = new FileInputStream(firstLocalFile);
    
                System.out.println("Start uploading first file");
                boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
                inputStream.close();
                if (done) {
                    System.out.println("The first file is uploaded successfully.");
                }
    
                // APPROACH #2: uploads second file using an OutputStream
                File secondLocalFile = new File("E:/Test/Report.doc");
                String secondRemoteFile = "test/Report.doc";
                inputStream = new FileInputStream(secondLocalFile);
    
                System.out.println("Start uploading second file");
                OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
                byte[] bytesIn = new byte[4096];
                int read = 0;
    
                while ((read = inputStream.read(bytesIn)) != -1) {
                    outputStream.write(bytesIn, 0, read);
                }
                inputStream.close();
                outputStream.close();
    
                boolean completed = ftpClient.completePendingCommand();
                if (completed) {
                    System.out.println("The second file is uploaded successfully.");
                }
    
            } catch (IOException ex) {
                System.out.println("Error: " + ex.getMessage());
                ex.printStackTrace();
            } finally {
                try {
                    if (ftpClient.isConnected()) {
                        ftpClient.logout();
                        ftpClient.disconnect();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      Apache commons lib 有这个实用程序org.apache.commons.net.ftp.FTPClient

      http://commons.apache.org/

      import org.apache.commons.net.ftp.FTPClient;
      
      FTPClient client = new FTPClient();
      String sFTP = "ftp.miservidor.com";
      String sUser = "usuario";
      String sPassword = "password";
               
      try {
          client.connect(sFTP);
          boolean login = client.login(sUser, sPassword);
      } catch (IOException ioe) {}
      

      完整示例: http://commons.apache.org/proper/commons-net/examples/ftp/FTPClientExample.java

      【讨论】:

      • 登录后如何上传文件?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      相关资源
      最近更新 更多