web-dev-qa-db-ja.com

Java)を使用してFTPにアップロードする

simple小さなファイルをftpサーバーにアップロードする方法があるかどうか疑問に思っていました。 Apache Commons Netライブラリをチェックしましたが、正直に言うとかなり複雑に思えます。小さなファイルをftpにアップロードする簡単な方法はありますか?

最終的にApacheCommons Net Libraryを使用することになりましたが、それほど難しくはありませんでした。

8
user2526311

このリンクから: RLConnectionクラスを使用してFTPサーバーにファイルをアップロードします 。外部ライブラリは必要ありません。

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();
}
19
Loša

org.Apache.commons.net.ftp.FTPClienthere を使用したかなり素晴らしいサンプルを見つけたと思います

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

}
1
Dina Yefremova