1. 程式人生 > >java實現ftp檔案的上傳與下載

java實現ftp檔案的上傳與下載

複製程式碼
  1 import java.io.File;
  2 import java.io.FileInputStream;
  3 import java.io.FileOutputStream;
  4 import java.io.IOException;
  5 import java.io.OutputStream;
  6 
  7 
  8 import org.apache.commons.net.ftp.FTPClient;
  9 import org.apache.commons.net.ftp.FTPFile;
 10 import org.apache.commons.net.ftp.FTPReply;
11 import org.apache.log4j.Logger; 12 13 public class FtpUtil { 14 15 private static Logger logger=Logger.getLogger(FtpUtil.class); 16 17 private static FTPClient ftp; 18 19 /** 20 * 獲取ftp連線 21 * @param f 22 * @return 23 * @throws Exception
24 */ 25 public static boolean connectFtp(Ftp f) throws Exception{ 26 ftp=new FTPClient(); 27 boolean flag=false; 28 int reply; 29 if (f.getPort()==null) { 30 ftp.connect(f.getIpAddr(),21); 31 }else{ 32 ftp.connect(f.getIpAddr(),f.getPort());
33 } 34 ftp.login(f.getUserName(), f.getPwd()); 35 ftp.setFileType(FTPClient.BINARY_FILE_TYPE); 36 reply = ftp.getReplyCode(); 37 if (!FTPReply.isPositiveCompletion(reply)) { 38 ftp.disconnect(); 39 return flag; 40 } 41 ftp.changeWorkingDirectory(f.getPath()); 42 flag = true; 43 return flag; 44 } 45 46 /** 47 * 關閉ftp連線 48 */ 49 public static void closeFtp(){ 50 if (ftp!=null && ftp.isConnected()) { 51 try { 52 ftp.logout(); 53 ftp.disconnect(); 54 } catch (IOException e) { 55 e.printStackTrace(); 56 } 57 } 58 } 59 60 /** 61 * ftp上傳檔案 62 * @param f 63 * @throws Exception 64 */ 65 public static void upload(File f) throws Exception{ 66 if (f.isDirectory()) { 67 ftp.makeDirectory(f.getName()); 68 ftp.changeWorkingDirectory(f.getName()); 69 String[] files=f.list(); 70 for(String fstr : files){ 71 File file1=new File(f.getPath()+"/"+fstr); 72 if (file1.isDirectory()) { 73 upload(file1); 74 ftp.changeToParentDirectory(); 75 }else{ 76 File file2=new File(f.getPath()+"/"+fstr); 77 FileInputStream input=new FileInputStream(file2); 78 ftp.storeFile(file2.getName(),input); 79 input.close(); 80 } 81 } 82 }else{ 83 File file2=new File(f.getPath()); 84 FileInputStream input=new FileInputStream(file2); 85 ftp.storeFile(file2.getName(),input); 86 input.close(); 87 } 88 } 89 90 /** 91 * 下載連結配置 92 * @param f 93 * @param localBaseDir 本地目錄 94 * @param remoteBaseDir 遠端目錄 95 * @throws Exception 96 */ 97 public static void startDown(Ftp f,String localBaseDir,String remoteBaseDir ) throws Exception{ 98 if (FtpUtil.connectFtp(f)) { 99 100 try { 101 FTPFile[] files = null; 102 boolean changedir = ftp.changeWorkingDirectory(remoteBaseDir); 103 if (changedir) { 104 ftp.setControlEncoding("GBK"); 105 files = ftp.listFiles(); 106 for (int i = 0; i < files.length; i++) { 107 try{ 108 downloadFile(files[i], localBaseDir, remoteBaseDir); 109 }catch(Exception e){ 110 logger.error(e); 111 logger.error("<"+files[i].getName()+">下載失敗"); 112 } 113 } 114 } 115 } catch (Exception e) { 116 logger.error(e); 117 logger.error("下載過程中出現異常"); 118 } 119 }else{ 120 logger.error("連結失敗!"); 121 } 122 123 } 124 125 126 /** 127 * 128 * 下載FTP檔案 129 * 當你需要下載FTP檔案的時候,呼叫此方法 130 * 根據<b>獲取的檔名,本地地址,遠端地址</b>進行下載 131 * 132 * @param ftpFile 133 * @param relativeLocalPath 134 * @param relativeRemotePath 135 */ 136 private static void downloadFile(FTPFile ftpFile, String relativeLocalPath,String relativeRemotePath) { 137 if (ftpFile.isFile()) { 138 if (ftpFile.getName().indexOf("?") == -1) { 139 OutputStream outputStream = null; 140 try { 141 File locaFile= new File(relativeLocalPath+ ftpFile.getName()); 142 //判斷檔案是否存在,存在則返回 143 if(locaFile.exists()){ 144 return; 145 }else{ 146 outputStream = new FileOutputStream(relativeLocalPath+ ftpFile.getName()); 147 ftp.retrieveFile(ftpFile.getName(), outputStream); 148 outputStream.flush(); 149 outputStream.close(); 150 } 151 } catch (Exception e) { 152 logger.error(e); 153 } finally { 154 try { 155 if (outputStream != null){ 156 outputStream.close(); 157 } 158 } catch (IOException e) { 159 logger.error("輸出檔案流異常"); 160 } 161 } 162 } 163 } else { 164 String newlocalRelatePath = relativeLocalPath + ftpFile.getName(); 165 String newRemote = new String(relativeRemotePath+ ftpFile.getName().toString()); 166 File fl = new File(newlocalRelatePath); 167 if (!fl.exists()) { 168 fl.mkdirs(); 169 } 170 try { 171 newlocalRelatePath = newlocalRelatePath + '/'; 172 newRemote = newRemote + "/"; 173 String currentWorkDir = ftpFile.getName().toString(); 174 boolean changedir = ftp.changeWorkingDirectory(currentWorkDir); 175 if (changedir) { 176 FTPFile[] files = null; 177 files = ftp.listFiles(); 178 for (int i = 0; i < files.length; i++) { 179 downloadFile(files[i], newlocalRelatePath, newRemote); 180 } 181 } 182 if (changedir){ 183 ftp.changeToParentDirectory(); 184 } 185 } catch (Exception e) { 186 logger.error(e); 187 } 188 } 189 } 190 191 192 public static void main(String[] args) throws Exception{ 193 Ftp f=new Ftp(); 194 f.setIpAddr("1111"); 195 f.setUserName("root"); 196 f.setPwd("111111"); 197 FtpUtil.connectFtp(f); 198 File file = new File("F:/test/com/test/Testng.java"); 199 FtpUtil.upload(file);//把檔案上傳在ftp上 200 FtpUtil.startDown(f, "e:/", "/xxtest");//下載ftp檔案測試 201 System.out.println("ok"); 202 203 } 204 205 }
複製程式碼