package com.ctl.util; //须要commons-net-3.0.1.jar import java.io.*; import java.net.*; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.commons.net.ftp.*; import com.ctl.util.exception.FTPClientException; public class FTPClientUtil { /** logger */ private static ThreadLocal<FTPClient> ftpClientThreadLocal; // ftp ip address private static String host; // u7AEFu53E3u53F7 private static int port; private static String username; private static String password; private static boolean binaryTransfer; private static boolean passiveMode; // u7F16u7801 private static String encoding; // u8BBEu7F6Eu8FDEu63A5u8D85u65F6u65F6u95F4 private static int clientTimeout; // u67E5u8BE2u8FDCu7A0Bu76EEu5F55u6240u6709u7684file name private static List<String> listFileNames; // private static String filePathOfFtpserver; static { ftpClientThreadLocal = new ThreadLocal<FTPClient>(); host = ConfigUtils.getType("ftp.host"); port = Integer.parseInt(ConfigUtils.getType("ftp.port")); username = ConfigUtils.getType("ftp.username"); password = ConfigUtils.getType("ftp.password"); binaryTransfer = Boolean.parseBoolean(ConfigUtils .getType("ftp.binaryTransfer")); passiveMode = Boolean.parseBoolean(ConfigUtils .getType("ftp.passiveMode")); encoding = ConfigUtils.getType("ftp.encoding"); clientTimeout = Integer.parseInt(ConfigUtils .getType("ftp.clientTimeout")); listFileNames = new ArrayList<String>(); } /** * @description u83B7u53D6FTPu5BA2u6237u7AEFu8FDEu63A5 * @return * @throws FTPClientException * @throws SocketException * @throws IOException */ private static FTPClient getFTPClient() throws FTPClientException, SocketException, IOException { if (ftpClientThreadLocal.get() != null && ftpClientThreadLocal.get().isConnected()) { return ftpClientThreadLocal.get(); } else { FTPClient ftpClient = new FTPClient(); // u6784u9020u4E00u4E2AFtpClientu5B9Eu4F8B ftpClient.setControlEncoding(encoding); // u8BBEu7F6Eu5B57u7B26u96C6 ftpClient.setConnectTimeout(clientTimeout); ftpClient.connect(host, port); // u8FDEu63A5u540Eu68C0u6D4Bu8FD4u56DEu7801u6765u6821u9A8Cu8FDEu63A5u662Fu5426u6210u529F int reply = ftpClient.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) {// u767Bu9646u5230ftpu670Du52A1u5668 ftpClient.login(username, password); setFileType(ftpClient); // u8BBEu7F6Eu6587u4EF6u4F20u8F93u7C7Bu578B } else { ftpClient.disconnect(); } if (passiveMode) { ftpClient.enterLocalPassiveMode(); } ftpClientThreadLocal.set(ftpClient); return ftpClient; } } /** * @descriptionu8BBEu7F6Eu6587u4EF6u4F20u8F93u7C7Bu578B * @throws FTPClientException * @throws IOException */ private static void setFileType(FTPClient ftpClient) throws FTPClientException, IOException { if (binaryTransfer) { ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); } else { ftpClient.setFileType(FTPClient.ASCII_FILE_TYPE); } } /** * @descriptionu65ADu5F00ftpu8FDEu63A5 * @throws FTPClientException * @throws IOException */ public static void disconnect() throws FTPClientException, IOException { FTPClient ftpClient = getFTPClient(); ftpClient.logout(); if (ftpClient.isConnected()) { ftpClient.disconnect(); ftpClient = null; ftpClientThreadLocal.set(ftpClient); } } /** * @description u6279u91CFu5220u9664u6240u6709u76EEu5F55u4E0Bu7684u5BF9u5E94u7684u6587u4EF6 * @param delFiles * @return * @throws FTPClientException * @throws IOException */ public static boolean deleteRemoteFiles(String[] delFiles) throws FTPClientException, IOException { List<String> list=listNames();//u83B7u53D6u6240u6709u7684u6587u4EF6u540D for(String filename:delFiles){ for(Iterator<String> it=list.iterator();it.hasNext();){ String filepath=it.next(); if(filepath.contains(filename)){//u5982u679Cu8BE5u8DEFu5F84u5305u542Bu8BE5u6587u4EF6u540Du5219u5220u9664 boolean result= getFTPClient().deleteFile(filepath); if(!result){ return result; } } } } return true; } /** * @description u5217u51FAu8FDCu7A0Bu9ED8u8BA4u76EEu5F55u4E0Bu6240u6709u7684u6587u4EF6 * @return u8FDCu7A0Bu9ED8u8BA4u76EEu5F55u4E0Bu6240u6709u6587u4EF6u540Du7684u5217u8868uFF0Cu76EEu5F55u4E0Du5B58u5728u6216u8005u76EEu5F55u4E0Bu6CA1u6709u6587u4EF6u65F6u8FD4u56DE0u957Fu5EA6u7684u6570u7EC4 * @throws FTPClientException * @throws IOException */ public static List<String> listNames() throws FTPClientException, IOException { return listNames(null); } public static List<String> listNames(String remotePath) throws FTPClientException, IOException { return listNames(remotePath, true); } /** * @descriptionu5217u51FAu8FDCu7A0Bu76EEu5F55u4E0Bu6240u6709u7684u6587u4EF6 * @param remotePath * u8FDCu7A0Bu76EEu5F55u540D * @param autoClose * u662Fu5426u81EAu52A8u5173u95EDu5F53u524Du8FDEu63A5 * @return u8FDCu7A0Bu76EEu5F55u4E0Bu6240u6709u6587u4EF6u540Du7684u5217u8868uFF0Cu76EEu5F55u4E0Du5B58u5728u6216u8005u76EEu5F55u4E0Bu6CA1u6709u6587u4EF6u65F6u8FD4u56DE0u957Fu5EA6u7684u6570u7EC4 * @throws FTPClientException * @throws IOException */ public static List<String> listNames(String remotePath, boolean containSubdirectory) throws FTPClientException, IOException { if (null == remotePath) { remotePath = "." + File.separator; } try { FTPFile[] files = getFTPClient().listFiles(remotePath); if (files.length < 3) { return listFileNames; } for (FTPFile file : files) { if (!file.getName().equals(".") && !file.getName().equals("..")) { if (file.isFile()) {// u6587u4EF6 listFileNames .add("." + File.separator + file.getName()); } else {// u76EEu5F55 listNames2( remotePath + file.getName() + File.separator, containSubdirectory); } } } } catch (IOException e) { throw new FTPClientException("u5217u51FAu8FDCu7A0Bu76EEu5F55u4E0Bu6240u6709u7684u6587u4EF6u65F6u51FAu73B0u5F02u5E38", e); } return listFileNames; } //listNames2u9012u5F52u65B9u6CD5 private static void listNames2(String remotePath, boolean containSubdirectory) throws FTPClientException { try { FTPClient client = getFTPClient(); client.changeWorkingDirectory(remotePath); FTPFile[] files = client.listFiles(remotePath); if (files.length < 3) { return; } for (FTPFile file : files) { if (!file.equals(".") && !file.equals("..")) { if (file.isFile()) { listFileNames.add(remotePath + file.getName()); } if (file.isDirectory() && (!".".equals(file.getName())) && (!"..".equals(file.getName()))) { String path = remotePath + file.getName() + File.separator; listNames2(path, containSubdirectory); } } } } catch (IOException e) { throw new FTPClientException("u5217u51FAu8FDCu7A0Bu76EEu5F55u4E0Bu6240u6709u7684u6587u4EF6u65F6u51FAu73B0u5F02u5E38", e); } } /** * * @param remotePath * u8FDCu7A0Bu8DEFu5F84 * @param fileName * u8981u4E0Au4F20u7684u6587u4EF6u540D * @param localInputStream * u672Cu5730InputStreamu6D41 * @return * @throws IOException * @throws FTPClientException */ public static boolean uploadToRemote(String remotePath, String fileName, InputStream localInputStream) throws IOException, FTPClientException { remotePath=remotePath+File.separator; FTPClient client = getFTPClient(); int reply; reply = client.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { client.disconnect(); } client.makeDirectory(remotePath);//u5728u670Du52A1u7AEFu5EFAu7ACBu8BE5u6587u4EF6u5939 client.changeWorkingDirectory(remotePath); boolean result = client.storeFile(remotePath+fileName, localInputStream); localInputStream.close(); return result; } /** * * @param remotePath * u8FDCu7A0Bu8DEFu5F84 * @param localPath * u672Cu5730u8DEFu5F84 * @return * @throws IOException * @throws FTPClientException * @throws IOException */ public static boolean downloadToLocal(String remotePath, String localPath) throws IOException, FTPClientException, IOException { return downloadToLocal(remotePath, localPath, null); } /** * * @param remotePath * u8FDCu7A0Bu8DEFu5F84 * @param localPath * u8981u4E0Bu8F7Du7684u8DEFu5F84 * @param fileNames * u6240u6709u8981u4E0Bu8F7Du7684u6587u4EF6u540Du5B57 * @return * @throws IOException * @throws FTPClientException * @throws IOException */ public static boolean downloadToLocal(String remotePath, String localPath, String[] fileNames) throws IOException, FTPClientException, IOException { remotePath=remotePath+File.separator; localPath=localPath+File.separator; FTPClient client = getFTPClient(); client.changeWorkingDirectory(remotePath); FTPFile[] ftpList = client.listFiles(remotePath); boolean result = true; if (null == fileNames) { for (FTPFile f : ftpList) { if (f.getSize() > 0) { File file = new File(localPath); file.mkdirs(); OutputStream out = new FileOutputStream(localPath + f.getName()); result = client.retrieveFile(f.getName(), out); // u4E0Bu8F7D out.close(); if (!result) { break; } } } } else { for (String fileName : fileNames) { File file = new File(localPath); file.mkdirs(); OutputStream out = new FileOutputStream(localPath + File.separator + fileName); result = client.retrieveFile(fileName, out); // u4E0Bu8F7D out.close(); if (!result) { break; } } } return result; } /** * @param client * @param fileName * u8FDCu7A0Bu8DEFu5F84u540D * @return * @throws IOException * @throws FTPClientException */ public static int getRemoteFileSize(String fileName) throws IOException, FTPClientException { FTPClient client = getFTPClient(); int size = 0; FTPFile[] ftpList = client.listFiles(); for (FTPFile f : ftpList) { if (f.getName().equalsIgnoreCase(fileName)) { size = (int) f.getSize(); } } return size; } /** * * @param filename * u8981u4E0Bu8F7Du7684u6587u4EF6u540D u4ECEu6574u4E2Au670Du52A1u5668u4E2Du67E5u627E,u53EFu80FDu627Eu5230u591Au4E2Au76F8u540Cu540Du5B57u7684u6587u4EF6,u6309u5728u670Du52A1u7AEFu7684u8DEFu5F84u5728u6307u5B9Au672Cu5730u8DEFu5F84u4E0Bu521Bu5EFAu60F3u5BF9u5E94u7684u8DEFu5F84u548Cu6587u4EF6 * @param localPath * u672Cu5730u8DEFu5F84 * @return * @throws Exception */ public static boolean downloadToLocal2(String filename, String localPath) throws Exception { List<String> list=listNames(); OutputStream out; try{ for(Iterator<String> it=list.iterator();it.hasNext();){ String filepath=it.next(); if(filepath.contains(filename)){ String remoteFilePath=filepath.substring(1, filepath.length()); File file=new File(localPath+remoteFilePath); new File(file.getParent()).mkdirs(); out= new FileOutputStream(localPath+remoteFilePath); getFTPClient().retrieveFile(filepath, out); // u4E0Bu8F7D out.close(); } } return true; }catch (Exception e) { return false; } } /** * @description u521Bu5EFAu8FDCu7A0Bu76EEu5F55u5141u8BB8u521Bu5EFAu591Au7EA7u76EEu5F55 * @param remoteDir u8FDCu7A0Bu76EEu5F55 * @return * @throws SocketException * @throws IOException * @throws FTPClientException */ public static boolean mkdirs(String remoteDir) throws SocketException, IOException, FTPClientException{ String[] dirs=remoteDir.split("/"); String remotePath="."; for(String dir:dirs){ if(!dir.equals(".")&&null!=dir){ remotePath=remotePath+File.separator+dir+File.separator; boolean result=getFTPClient().makeDirectory(remotePath); if(!result){ return result; } } } return true; } }