1 import org.apache.commons.net.ftp.FTPClient; 2 import org.apache.commons.net.ftp.FTPFile; 3 import org.apache.commons.net.ftp.FTPReply; 4 5 import java.io.*; 6 import java.net.MalformedURLException; 7 8 public class FtpUtils { 9 // ftp服务器地址 10 private String hostName; 11 // ftp服务器端口号 12 private Integer port; 13 private String userName; 14 // ftp登录密码 15 private String password; 16 17 private final FTPClient ftpClient = new FTPClient(); 18 19 /** 20 * 初始化ftp服务器 21 */ 22 public FtpUtils(String hostName, Integer port, String userName, String password) { 23 this.hostName = hostName; 24 this.port = port; 25 this.userName = userName; 26 this.password = password; 27 ftpClient.setControlEncoding("utf-8"); 28 try { 29 System.out.println("connecting...ftp服务器:" + this.hostName + ":" + this.port); 30 ftpClient.connect(hostName, port); // 连接ftp服务器 31 ftpClient.login(userName, password); // 登录ftp服务器 32 int replyCode = ftpClient.getReplyCode(); // 是否成功登录服务器 33 if (!FTPReply.isPositiveCompletion(replyCode)) { 34 System.out.println("connect failed...ftp服务器:" + this.hostName + ":" + this.port); 35 } 36 System.out.println("connect successfu...ftp服务器:" + this.hostName + ":" + this.port); 37 } catch (MalformedURLException e) { 38 e.printStackTrace(); 39 } catch (IOException e) { 40 e.printStackTrace(); 41 } 42 } 43 44 /** 45 * 上传文件 46 * 47 * @param pathName ftp服务保存地址 48 * @param fileName 上传到ftp的文件名 49 * @param originfilename 待上传文件的名称(绝对地址) * 50 * @return 51 */ 52 public boolean uploadFile(String pathName, String fileName, String originfilename) { 53 InputStream inputStream = null; 54 try { 55 inputStream = new FileInputStream(new File(originfilename)); 56 } catch (FileNotFoundException e) { 57 e.printStackTrace(); 58 } 59 return this.uploadFile(pathName, fileName, inputStream); 60 } 61 62 /** 63 * 上传文件 64 * 65 * @param pathName ftp服务保存地址 66 * @param fileName 上传到ftp的文件名 67 * @param inputStream 输入文件流 68 * @return 69 */ 70 public boolean uploadFile(String pathName, String fileName, InputStream inputStream) { 71 boolean flag = false; 72 try { 73 System.out.println("开始上传文件"); 74 // initFtpClient(); 75 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); 76 createDirecroty(pathName); 77 ftpClient.makeDirectory(pathName); 78 ftpClient.changeWorkingDirectory(pathName); 79 ftpClient.storeFile(fileName, inputStream); 80 inputStream.close(); 81 ftpClient.logout(); 82 flag = true; 83 System.out.println("上传文件成功"); 84 } catch (Exception e) { 85 System.out.println("上传文件失败"); 86 e.printStackTrace(); 87 } finally { 88 this.close(ftpClient, null, inputStream); 89 } 90 return flag; 91 } 92 93 // 改变目录路径 94 public boolean changeWorkingDirectory(String directory) { 95 boolean flag = true; 96 try { 97 flag = ftpClient.changeWorkingDirectory(directory); 98 if (flag) { 99 System.out.println("进入文件夹" + directory + " 成功!"); 100 } else { 101 System.out.println("进入文件夹" + directory + " 失败!开始创建文件夹"); 102 } 103 } catch (IOException ioe) { 104 ioe.printStackTrace(); 105 } 106 return flag; 107 } 108 109 // 创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建 110 public boolean createDirecroty(String remote) throws IOException { 111 String directory = remote + "/"; 112 // 如果远程目录不存在,则递归创建远程服务器目录 113 if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) { 114 int start = 0; 115 int end = 0; 116 if (directory.startsWith("/")) { 117 start = 1; 118 } else { 119 start = 0; 120 } 121 end = directory.indexOf("/", start); 122 String path = ""; 123 String paths = ""; 124 while (true) { 125 String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1"); 126 path = path + "/" + subDirectory; 127 if (!existFile(path)) { 128 if (makeDirectory(subDirectory)) { 129 changeWorkingDirectory(subDirectory); 130 } else { 131 System.out.println("创建目录[" + subDirectory + "]失败"); 132 changeWorkingDirectory(subDirectory); 133 } 134 } else { 135 changeWorkingDirectory(subDirectory); 136 } 137 paths = paths + "/" + subDirectory; 138 start = end + 1; 139 end = directory.indexOf("/", start); 140 // 检查所有目录是否创建完毕 141 if (end <= start) { 142 break; 143 } 144 } 145 } 146 return true; 147 } 148 149 // 判断ftp服务器文件是否存在 150 public boolean existFile(String path) throws IOException { 151 boolean flag = false; 152 ftpClient.changeWorkingDirectory(path); 153 FTPFile[] ftpFileArr = ftpClient.listFiles(); 154 if (ftpFileArr.length > 0) { 155 flag = true; 156 } 157 return flag; 158 } 159 160 // 创建目录 161 public boolean makeDirectory(String dir) { 162 boolean flag = true; 163 try { 164 flag = ftpClient.makeDirectory(dir); 165 if (flag) { 166 System.out.println("创建文件夹" + dir + " 成功!"); 167 168 } else { 169 System.out.println("创建文件夹" + dir + " 失败!"); 170 } 171 } catch (Exception e) { 172 e.printStackTrace(); 173 } 174 return flag; 175 } 176 177 /** 178 * 下载文件 * 179 * 180 * @param pathName FTP服务器文件目录 * 181 * @param fileName 文件名称 * 182 * @param localpath 下载后的文件路径 * 183 * @return 184 */ 185 public boolean downloadFile(String pathName, String fileName, String localpath) { 186 boolean flag = false; 187 OutputStream os = null; 188 try { 189 System.out.println("开始下载文件"); 190 // initFtpClient(); 191 // 切换FTP目录 192 ftpClient.changeWorkingDirectory(pathName); 193 FTPFile[] ftpFiles = ftpClient.listFiles(); 194 for (FTPFile file : ftpFiles) { 195 if (fileName.equalsIgnoreCase(file.getName())) { 196 //如果该目录下有同名文件,则执行删除操作. 197 if (new File(localpath + "/" + file.getName()).exists()) { 198 new File(localpath + "/" + file.getName()).delete(); 199 } 200 201 File localFile = new File(localpath + "/" + file.getName()); 202 os = new FileOutputStream(localFile); 203 ftpClient.retrieveFile(file.getName(), os); 204 os.close(); 205 } 206 } 207 ftpClient.logout(); 208 flag = true; 209 System.out.println("下载文件成功"); 210 } catch (Exception e) { 211 System.out.println("下载文件失败"); 212 e.printStackTrace(); 213 } finally { 214 215 } 216 return flag; 217 } 218 219 /** 220 * @return 221 * @description: 关闭相关流操作方法 222 * @exception: 223 * @date: 2019/6/17 15:50 224 * @version: 1.0 225 */ 226 private void close(FTPClient ftpClient, OutputStream out, InputStream in) { 227 if (ftpClient.isConnected()) { 228 try { 229 ftpClient.disconnect(); 230 } catch (IOException e) { 231 e.printStackTrace(); 232 } 233 } 234 if (null != out) { 235 try { 236 out.close(); 237 } catch (IOException e) { 238 e.printStackTrace(); 239 } 240 } 241 if (null != in) { 242 try { 243 in.close(); 244 } catch (IOException e) { 245 e.printStackTrace(); 246 } 247 } 248 } 249 250 /** 251 * 删除文件 * 252 * 253 * @param pathName FTP服务器保存目录 * 254 * @param fileName 要删除的文件名称 * 255 * @return 256 */ 257 public boolean deleteFile(String pathName, String fileName) { 258 boolean flag = false; 259 try { 260 System.out.println("开始删除文件"); 261 // initFtpClient(); 262 // 切换FTP目录 263 ftpClient.changeWorkingDirectory(pathName); 264 ftpClient.dele(fileName); 265 ftpClient.logout(); 266 flag = true; 267 System.out.println("删除文件成功"); 268 } catch (Exception e) { 269 System.out.println("删除文件失败"); 270 e.printStackTrace(); 271 } finally { 272 this.close(ftpClient, null, null); 273 } 274 return flag; 275 } 276 }