private void uploadFileToFtp(List fileNameList) {
final FTPClient ftpClient = new FTPClient();
// 创建线程池
ThreadPoolExecutor executors = new ThreadPoolExecutor(2, 3, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue(), new RejectHandler());
boolean isOver = false;
try {
ftpClient.connect("22.46.93.22", 21);
ftpClient.login("hqmis", "hqmis@123");
// 设置上传文件路径
String ftp_path = "/home/hqmis/upload/Files";
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
ftpClient.disconnect();
}
// 查看路径是否存在
boolean flag = ftpClient.changeWorkingDirectory(ftp_path);
if (!flag) {
ftpClient.makeDirectory(ftp_path);
}
// ftpClient.enterLocalPassiveMode();
// 指定上传路径
ftpClient.changeWorkingDirectory(ftp_path);
// 指定上传文件类型 二进制
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
for (final String fileName : fileNameList) {
executors.execute(new Runnable() {
@Override
public void run() {
// 读取本地文件
File file = new File(fileName);
InputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
boolean store = ftpClient.storeFile(file.getName(), inputStream);
System.out.println(store);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 删除本地文件
file.delete();
}
});
}
executors.shutdown();
// 判断线程池任务是否全部执行完成
while (true) {
if (executors.isTerminated()) {
isOver = true;
ftpClient.logout();
System.out.println("线程池执行完毕");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (isOver && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
利用线程池上传,ftpClient.storeFile(file.getName(), inputStream);方法一直返回false,请大神帮忙看看代码哪边需要改动呢?