用ftp下载时,执行到ftpClient.retrieveFile(fileName, os); 这个方法就卡死了,在网上找了很多方法也不行, ftpClient.enterLocalPassiveMode(); 加上主动被动都不行,跪求大神帮忙解决
以下为下载方法
/**
* 下载.
* @param remotePath FTP目录
* @param fileName 文件名
* @param localPath 本地目录
* @return Result
* @throws IOException
*/
public Result download(String remotePath, String fileName, String localPath) throws IOException {
boolean flag = true;
Result result = null;
// 初始化FTP当前目录
currentPath = remotePath;
// 初始化当前流量
response = 0;
// 更改FTP目录
ftpClient.changeWorkingDirectory(remotePath);
// 得到FTP当前目录下所有文件
FTPFile[] ftpFiles = ftpClient.listFiles();
// 循环遍历
for (FTPFile ftpFile : ftpFiles) {
// 找到需要下载的文件
if (ftpFile.getName().equals(fileName)) {
System.out.println("download...");
// 创建本地目录
File file = new File(localPath + "/" + fileName);
// 下载前时间
Date startTime = new Date();
if (ftpFile.isDirectory()) {
// 下载多个文件
flag = downloadMany(file);
} else {
// 下载当个文件
flag = downloadSingle(file, ftpFile);
}
// 下载完时间
Date endTime = new Date();
// 返回值
result = new Result(flag, Util.getFormatTime(endTime.getTime() - startTime.getTime()), Util.getFormatSize(response));
}
}
return result;
}
再建一个Result 类
/**
* 执行每一个动作后响应的结果,包括成功的和失败的.
*
* @author hao_yujie, cui_tao
*
*/
public class Result {
/**
* 响应的内容.
*/
private String response;
/**
* 响应的结果.
*/
private boolean succeed;
/**
* 响应的时间.
*/
private String time;
/**
* 无参的构造方法.
*/
public Result() {
}
/**
* 构造方法.
*
* @param res 响应的内容
*/
public Result(String res) {
this.response = res;
}
/**
* 构造方法.
*
* @param suc 响应的结果
* @param ti 响应的时间
* @param res 响应的内容
*/
public Result(boolean suc, String ti, String res) {
this.succeed = suc;
this.time = ti;
this.response = res;
}
/**
* 得到相应内容.
*
* @return 相应内容
*/
public String getResponse() {
return response;
}
/**
* 设置相应内容.
*
* @param response 响应内容
*/
public void setResponse(String response) {
this.response = response;
}
/**
* 得到相应结果.
*
* @return 相应结果
*/
public boolean isSucceed() {
return succeed;
}
/**
* 设置响应结果.
*
* @param succeed 响应结果
*/
public void setSucceed(boolean succeed) {
this.succeed = succeed;
}
/**
* 得到响应时间.
*
* @return 响应时间
*/
public String getTime() {
return time;
}
/**
* 设置响应时间.
*
* @param time 响应时间
*/
public void setTime(String time) {
this.time = time;
}
}
下载文件的方法是用什么方式去下载都没给
1. File localFile = new File(downFilePath + name);
1. OutputStream is = new FileOutputStream(localFile);
1. //retrieveFile的第一个参数需要是 ISO-8859-1 编码,并且必须是完整路径!
1. String fileName = partsRemoteDir + name;
1. ftpClient.retrieveFile(new String(fileName.getBytes("GBK"),"ISO-8859-1"), is);
1. result="下载成功";
1. is.close();
我是直接连接到ftp,然后用这段代码下载
粘贴>>>复制>>>修改里面的参数和地址,就可以用了
public static boolean downloadFile(String localPath, String fileName) {
FTPClient ftp = new FTPClient();
boolean flag = false;
try {
int reply;
ftp.connect(HOST);
ftp.login(USERNAME, PASSWORD);
ftp.enterLocalPassiveMode();
ftp.setFileType(FTP.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return flag;
}
ftp.changeWorkingDirectory(localPath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
//is = ftp.retrieveFileStream(localPath + ff.getName());
File localFile = new File(localPath);
if (!localFile.exists()) {
localFile.mkdir();
}
File file = new File(localPath + ff.getName());
if (!file.exists()) {
OutputStream is = new FileOutputStream(file);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
flag = true;
}
}
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
参考:
[ java实现ftp文件上传下载,解决慢,中文乱码,多个文件下载等问题](https://www.cnblogs.com/hrx-star/p/5976561.html "")