2 * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.vfc.nfvo.emsdriver.commons.ftp;
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.commons.net.ftp.*;
21 import org.onap.vfc.nfvo.emsdriver.commons.utils.StringUtil;
23 import java.io.BufferedOutputStream;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.util.TimeZone;
29 public class FTPSrv implements FTPInterface {
30 private Log log = LogFactory.getLog(FTPSrv.class);
31 private FTPClient ftpClient = null;
45 public void login(String host, int port, String user, String pwd, String encode, boolean isPassiveMode, int timeout) throws Exception {
46 ftpClient = new FTPClient();
48 FTPClientConfig ftpClientConfig = new FTPClientConfig();
49 ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
50 this.ftpClient.setControlEncoding("GBK");
51 this.ftpClient.configure(ftpClientConfig);
52 // ftpClient.setParserFactory(new ExtendsDefaultFTPFileEntryParserFactory());
54 if (encode != null && encode.length() > 0) {
55 ftpClient.setControlEncoding(encode);
58 ftpClient.connect(host, port);
59 int reply = this.ftpClient.getReplyCode();
60 if (!FTPReply.isPositiveCompletion(reply)) {
61 this.ftpClient.disconnect();
65 if (!ftpClient.login(user, pwd)) {
66 throw new IOException("login[" + host + "],port[" + port + "] fail, please check user and password");
69 ftpClient.enterLocalPassiveMode();
71 ftpClient.enterLocalActiveMode();
73 ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
74 this.ftpClient.setBufferSize(1024 * 2);
75 this.ftpClient.setDataTimeout(3 * 60 * 1000);
77 this.ftpClient.setSoTimeout(timeout);
78 } catch (Exception e) {
79 //e.printStackTrace();
80 log.error(" StackTrace :", e);
88 public void logout() {
89 if (ftpClient != null) {
92 ftpClient.disconnect();
93 } catch (Exception e) {
100 public boolean chdir(String dir) {
101 boolean sucess = false;
103 if (ftpClient.changeWorkingDirectory(dir)) {
108 } catch (IOException e) {
109 log.error("chdir dir =" + dir + " is error" + StringUtil.getStackTrace(e));
117 public boolean downloadFile(String remoteFile, String localFile) {
118 boolean sucess = false;
119 BufferedOutputStream toLfileOutput = null;
121 toLfileOutput = new BufferedOutputStream(new FileOutputStream(localFile));
122 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
123 if (ftpClient.retrieveFile(remoteFile, toLfileOutput)) {
128 } catch (Exception ioe) {
130 log.error("downloadFile remoteFile =" + remoteFile + " is fail ", ioe);
132 if (toLfileOutput != null)
134 toLfileOutput.close();
135 } catch (IOException e) {
143 public RemoteFile[] list() {
144 AFtpRemoteFile[] ftpRemoteFiles = null;
145 String currdir = null;
147 currdir = ftpClient.printWorkingDirectory();
148 if (currdir.endsWith("/") == false) {
149 currdir = currdir + "/";
151 FTPFile[] rfileList = null;
152 rfileList = ftpClient.listFiles(currdir);
153 ftpRemoteFiles = new AFtpRemoteFile[rfileList.length];
154 for (int i = 0; i < rfileList.length; i++) {
155 ftpRemoteFiles[i] = new AFtpRemoteFile(rfileList[i], ftpClient, currdir);
157 } catch (IOException e) {
158 log.error("Ftp list currdir = " + currdir + " is fail " + StringUtil.getStackTrace(e));
160 return ftpRemoteFiles;
165 public String pwd() throws IOException {
166 String returnValue = ftpClient.printWorkingDirectory();
170 // public boolean store(String localFile, String remoteFile) {
172 // boolean sucess = false;
173 // FileInputStream lfileInput = null;
175 // lfileInput = new FileInputStream(localFile);
176 // ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
178 // if (ftpClient.storeFile(remoteFile, lfileInput)){
183 // } catch (Exception ioe) {
185 // log.error("store localFile = "+localFile+" is fail "+StringUtil.getStackTrace(ioe));
187 // if (lfileInput != null)
189 // lfileInput.close();
190 // } catch (IOException e) {