简体   繁体   English

是否可以使用java从远程路径检索文件?

[英]Is it possible to retrieve a file from a remote path using java?

I've been tasked with a project to automate a process in which we extract a file from a WinSCP client daily. 我一直负责一个项目来自动化我们每天从WinSCP客户端提取文件的过程。 So far I've been able to automate the login and setup a recurring schedule for the code to run; 到目前为止,我已经能够自动登录并设置运行代码的重复计划; however it seems I've hit a bump in the road. 然而,似乎我在路上撞了一下。 When I attempt to locate a file for retrieval nothing happens. 当我尝试找到要检索的文件时,没有任何反应。 This is because the file I wish to access is through a remote directory. 这是因为我希望访问的文件是通过远程目录。 I'm almost positive that the code I've written is bug free. 我几乎肯定我写的代码是没有bug的。 I am just unsure if specify a certain path which java can locate the file. 我只是不确定是否指定了java可以找到该文件的特定路径。 I have no idea how to tell the java code where to extract this file from. 我不知道如何告诉java代码从哪里提取此文件。 Any thoughts? 有什么想法吗?

You can try and use the code below: 您可以尝试使用以下代码:

More details can be found here . 更多细节可以在这里找到。

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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 demonstrates how to upload files from local computer to a remote
 * FTP server using Apache Commons Net API.
 * @author www.codejava.net
 */
public class FTPDownloadFileDemo {

    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: using retrieveFile(String, OutputStream)
            String remoteFile1 = "/test/video.mp4";
            File downloadFile1 = new File("D:/Downloads/video.mp4");
            OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
            boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
            outputStream1.close();

            if (success) {
                System.out.println("File #1 has been downloaded successfully.");
            }

            // APPROACH #2: using InputStream retrieveFileStream(String)
            String remoteFile2 = "/test/song.mp3";
            File downloadFile2 = new File("D:/Downloads/song.mp3");
            OutputStream outputStream2 = new BufferedOutputStream(new FileOutputStream(downloadFile2));
            InputStream inputStream = ftpClient.retrieveFileStream(remoteFile2);
            byte[] bytesArray = new byte[4096];
            int bytesRead = -1;
            while ((bytesRead = inputStream.read(bytesArray)) != -1) {
                outputStream2.write(bytesArray, 0, bytesRead);
            }

            success = ftpClient.completePendingCommand();
            if (success) {
                System.out.println("File #2 has been downloaded successfully.");
            }
            outputStream2.close();
            inputStream.close();

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM