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