gpt4 book ai didi

java - 使用 apache FTPClient 从 FTP 服务器下载文件

转载 作者:太空宇宙 更新时间:2023-11-04 06:31:27 25 4
gpt4 key购买 nike

我正在使用 apache 的 FTPClient 从 FTP 服务器下载文件。我的情况是 - FTP 服务器可能会失去网络连接,并且可能会保持断开状态最多 1 天。当它重新连接时,文件的下载应该从原来的位置开始。我使用以下代码连接到服务器,然后从服务器下载文件

public void retrieve(String server, int port, String username,
String password, String remote, String local, int fileType,
ProgressHandler progressHandler) throws Exception {
final FTPClient ftp = new FTPClient();
Date date = new Date();
long startTime_ms = date.getTime();
if (progressHandler != null) {
ftp.setCopyStreamListener(new FtpCopyStreamListener(progressHandler));
}

ftpConnect(server,ftp, port,startTime_ms);


if(ftp.getReplyCode()==0 || !String.valueOf(ftp.getReplyCode()).startsWith("2")){
cleanup(ftp, "Could not log into server: " + server, null);
return;
}

boolean loggedIn = false;
try {
if (username == null || username.isEmpty()) {
username = "anonymous";
password = System.getProperty("user.name") + "@"
+ InetAddress.getLocalHost().getHostName();
}
if (!ftp.login(username, password)) {
ftp.logout();
cleanup(ftp, "Could not log into server: " + server, null);
}
loggedIn = true;

ftp.setFileType(fileType);
ftp.enterLocalPassiveMode();
OutputStream output = null;
try {
output = new FileOutputStream(local);
LOGGER.info("About to download " + remote + " from " + server
+ " on " + (port > 0 ? port : ftp.getDefaultPort())
+ " to " + local);
ftp.setControlKeepAliveTimeout(300);
boolean isFileDownloaded = false;
try{
isFileDownloaded = ftp.retrieveFile(remote, output);

}
catch(IOException ex){
if(ftp.isConnected()){

try{
int retryCode = 0;
while (!String.valueOf(retryCode).startsWith("2") && (new Date().getTime() < (startTime_ms + TOTAL_DURATION_MSECS))){
try{

retryCode = ftp.retr(local);
}catch(Exception e1){
Thread.sleep(1000);
System.out.println("File not downloaded !! " + e1.toString());
ftpConnect(server, ftp, port, startTime_ms);
}

}
}catch(Exception e){
//ftp.getReplyCode()
//throw e;
//disconnect(ftp);
//ftpConnect(server, ftp, port, startTime_ms);

System.out.println("File not downloaded !! " + e.toString());
}
}
}
if(isFileDownloaded){
LOGGER.info("Finished downloading " + remote + " from "
+ server + " on "
+ (port > 0 ? port : ftp.getDefaultPort()) + " to "
+ local);
}
else{
System.out.println("File not downloaded !! ");
}

}catch(IOException io){
io.printStackTrace();

throw io;
}
finally {
if (output != null) {
try {
output.close();
} catch (IOException f) {
LOGGER.severe("output.close() error: "
+ ServiceUtils.stackToString(f));
}
}
}
}
catch (FTPConnectionClosedException e) {
LOGGER.severe("Server closed connection: " + server + " "
+ ServiceUtils.stackToString(e));
throw e;
} catch (IOException e) {
LOGGER.severe("IOException, server: " + server + " "
+ ServiceUtils.stackToString(e));
throw e;
} finally {
if (loggedIn) {
try {
ftp.logout();
} catch (IOException f) {
LOGGER.severe("ftp.logout() error: "
+ ServiceUtils.stackToString(f));
}
}
disconnect(ftp);
}
}

我的问题基本上是,如果retrieveFile()方法失败,我是否可以重新连接并从断开连接的位置开始下载。

现在我使用 Microsoft IIS 服务器作为我的 FTP 服务器,但在生产环境中它将是 FileZilla FTP 服务器。

感谢任何帮助。谢谢

最佳答案

很抱歉回复得太晚了。这是我为解决问题所做的:

  1. 设置 FileZilla 服务器
  2. 使用retrieveFileStream而不是retrieveFile。
  3. 保留写入的字节数并使用相同的输出流进行写入。

        BufferedInputStream inputStream = null;
    BufferedOutputStream outputStream = null;
    InputStream in = null;
    int osCount = 0;
    in = ftp.retrieveFileStream(remote);
    inputStream = new BufferedInputStream(in);
    File localFile = new File(local, fileName);
    outputStream = new BufferedOutputStream(new FileOutputStream(localFile));
    for (int read = inputStream.read(); read != -1; read = inputStream.read()) {
    outputStream.write(read);
    osCount++;
    }
    outputStream.flush();
    if (osCount < fileSize) {
    System.out.println(fileName + ": Errored out !!!");
    BufferedInputStream inputStream1 = null;
    InputStream in1 = null;
    if (ftp.isConnected()) {
    try {
    while (osCount < fileSize && new Date().getTime() < (startTime_ms + TOTAL_DURATION_MSECS)) {
    try {
    disconnect(ftp);
    ftpConnect(server, ftp, port, startTime_ms);
    ftp.login(username, password);
    ftp.setRestartOffset(osCount);
    System.out.println(fileName + ": Re reading from position " + osCount);
    in1 = ftp.retrieveFileStream(remote);
    inputStream1 = new BufferedInputStream(in1);
    for (int read = inputStream1.read(); read != -1; read = inputStream1.read()) {
    outputStream.write(read);
    osCount++;
    }
    outputStream.flush();
    if(FTPReply.isPositiveCompletion(ftp.getReplyCode())){
    isErrored = true;
    break;
    }
    } catch (Exception e1) {
    Thread.sleep(1000);
    LOGGER.severe(fileName + ": File not downloaded " + server + " " + ServiceUtils.stackToString(e1));
    }
    }

    } catch (Exception e) {
    LOGGER.severe(fileName + ": File not downloaded " + server + " " + ServiceUtils.stackToString(e));
    }
    }
    }

关于java - 使用 apache FTPClient 从 FTP 服务器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26067835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com