这里如果连接报错,或者登陆失败,我感觉都该把异常抛出去,老师觉得呢。
private boolean connectServer(String ip,int port,String user,String pwd){
boolean isSuccess = false;
ftpClient = new FTPClient();
try {
ftpClient.connect(ip);
isSuccess = ftpClient.login(user,pwd);
} catch (IOException e) {
logger.error("连接FTP服务器异常",e);
}
return isSuccess;
}
同理我觉得这里,也不需要catch,而是直接抛出。只要上传过程中碰到异常,全部抛出,由上层去处理
private boolean uploadFile(String remotePath,List<File> fileList) throws IOException {
boolean uploaded = true;
FileInputStream fis = null;
//连接FTP服务器
if(connectServer(this.ip,this.port,this.user,this.pwd)){
try {
//remotePath如果不传,表示就在当前文件夹下面
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
//指定这个,可以防止一些乱码的事情
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//之前在FTP服务器上配置的是一个被动模式,并且对外也开放了一个服务的被动端口范围vsftpd的安装章节,
//这个时候打开本地的被动模式,
ftpClient.enterLocalPassiveMode();
for(File fileItem : fileList){
fis = new FileInputStream(fileItem);
ftpClient.storeFile(fileItem.getName(),fis);
}
} catch (IOException e) {
logger.error("上传文件异常",e);
uploaded = false;
e.printStackTrace();
} finally {
fis.close();
ftpClient.disconnect();
}
}
return uploaded;
}