retrievefile文件字节为0_Java实现FTP下载,下载下来的文件个别为0KB,请高手们指教(急)...

FTP包:commons-net-2.2.jar

public void download() throws Exception {

String filename = null;

String errorMsg = null;

try {

ftpClient = new FTPClient();

ftpClient.setControlEncoding("utf-8");

ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

ftpClient.connect(this.ftpDownloadItemDbo.getFtpAddress(), this.ftpDownloadItemDbo.getFtpPort());

ftpClient.login(this.ftpDownloadItemDbo.getFtpAccount(), this.ftpDownloadItemDbo.getFtpPassword());

log.info(" ---- login on ftp service success.");

ftpClient.changeWorkingDirectory("/")

String dirName = DataUtility.getStringByDate("yyyyMMdd",DataUtility.getUTCDateByLocal(new Date()));

String path = this.ftpDownloadItemDbo.getFtpFileDirectory();

if(path.trim().endsWith("/"))

dirName = path + dirName;

else

dirName = path+ "/" + dirName;

ftpClient.changeWorkingDirectory(dirName)

String fileNameRegex = this.buildRegex(this.ftpDownloadItemDbo.getFileNameFormat());

Pattern regex = Pattern.compile(fileNameRegex);

Map fileMap = new HashMap();

FTPFile[] fileDirs = ftpClient.listFiles();

for(FTPFile fileDir : fileDirs){

if(fileDir.isFile()){

filename = fileDir.getName();

Matcher regexMatcher = regex.matcher(filename);

if(regexMatcher.matches()){

fileMap.put(fileDir, filename);

}

}

}

path = path.replaceAll("/", "");

path = SystemConfig.getFTPDownloadFolderRoot() + path;

File dirFile = new File(path);

while(!fileMap.isEmpty()){

Entry file = fileMap.entrySet().iterator().next();

filename = file.getValue();

boolean repeat = false;

for(String ftpFile : fileMap.values()){

if(this.compareFileLevel(filename, ftpFile) < 0)

repeat = true;

}

if(repeat){

fileMap.remove(file.getKey());

} else{

if(!this.isExists(filename) ){

// save ftp download history

this.recordHistory(file.getKey());

if(!dirFile.exists()){

dirFile.mkdirs();

}

OutputStream out = new FileOutputStream(path + File.separator + filename);

ftpClient.retrieveFile(filename, out);

out.flush();

out.close();

}else{

fileMap.remove(file.getKey());

}

}

}

} catch(Exception e) {

// save log

this.downloadLog(filename, e.getMessage());

e.printStackTrace();

} finally {

if (ftpClient != null){

ftpClient.disconnect();

}

this.getSessionUtil().closeSession();

}

}

下载的文件数量少的时候不会出现内容为0kb的情况,批量下载时就会出现下载的文件为0KB,每次出现0KB的文件不固定

请高手们帮忙

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的FTPFile类是用于表示远程FTP服务器上的文件或目录的类,可以通过FTP客户端连接到FTP服务器,并使用FTPClient类来下载文件。 以下是一个简单的示例代码,演示如何使用FTPClientFTPFile下载文件: ```java import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class FTPFileDownload { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String username = "username"; String password = "password"; String remoteFilePath = "/path/to/remote/file.txt"; String localFilePath = "/path/to/local/file.txt"; FTPClient ftpClient = new FTPClient(); try { // 连接FTP服务器 ftpClient.connect(server, port); ftpClient.login(username, password); // 切换到二进制文件传输模式 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // 获取远程文件 FTPFile[] files = ftpClient.listFiles(remoteFilePath); if (files.length != 1) { throw new IOException("远程文件不存在或者有多个文件匹配:" + remoteFilePath); } FTPFile file = files; // 创建输出流 OutputStream outputStream = new FileOutputStream(localFilePath); // 下载文件 if (!ftpClient.retrieveFile(remoteFilePath, outputStream)) { throw new IOException("下载文件失败:" + remoteFilePath); } // 关闭输出流 outputStream.close(); System.out.println("文件已成功下载到本地:" + localFilePath); } catch (IOException e) { e.printStackTrace(); } finally { try { // 断开连接 ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值