FTP-abot move file

本文提供了一个使用Java实现FTP文件下载的示例代码。该示例展示了如何连接到FTP服务器,读取指定路径下的文件,并将其保存到本地指定目录中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


public void ftpGetFile(String sapepFilePath,String ipqrsPath, String fileName) throws Exception {
FTPClient ftp = this.getFtpConnect(HOSTNAME,USERNAME,PASSWORD);
InputStream ins = null;
try {
ins = ftp.retrieveFileStream(sapepFilePath);
this.saveFile(ins, ipqrsPath, fileName);
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
} finally {
try {
if (ins != null)
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (ftp != null && ftp.isConnected()) {
ftp.logout();
ftp.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void saveFile(InputStream ins, String strDir, String strFile) throws Exception {
File file = new File(strDir);
FileOutputStream fos = null;
// Create a buffer for reading the files
byte[] buf = new byte[8 * 1024];

try {
file.mkdirs();
file = new File(strDir + SLASH + strFile);
if (!file.exists()) {
file.createNewFile();
}
fos = new FileOutputStream(file);

int len;
while ((len = ins.read(buf)) > 0) {
fos.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
throw new Exception(e.getMessage());
} finally {
fos.close();
}
}
public FTPClient getFtpConnect(String hostName, String userName, String password){
FTPClient ftp = new FTPClient();
try{
ftp.connect(hostName);
if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
ftp.disconnect();
}
ftp.login(userName, password);
ftp.setBufferSize(100000);
ftp.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
} catch (Exception e) {
e.printStackTrace();
}
return ftp;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值