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.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
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 Logger log = LoggerFactory.getLogger(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 IOException {
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);
53 if (encode != null && encode.length() > 0) {
54 ftpClient.setControlEncoding(encode);
57 ftpClient.connect(host, port);
58 int reply = this.ftpClient.getReplyCode();
59 if (!FTPReply.isPositiveCompletion(reply)) {
60 this.ftpClient.disconnect();
64 if (!ftpClient.login(user, pwd)) {
65 throw new IOException("login[" + host + "],port[" + port + "] fail, please check user and password");
68 ftpClient.enterLocalPassiveMode();
70 ftpClient.enterLocalActiveMode();
72 ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
73 this.ftpClient.setBufferSize(1024 * 2);
74 this.ftpClient.setDataTimeout(3 * 60 * 1000);
76 this.ftpClient.setSoTimeout(timeout);
77 } catch (Exception e) {
78 log.error(" StackTrace :", e);
86 public void logout() {
87 if (ftpClient != null) {
90 ftpClient.disconnect();
91 } catch (Exception e) {
92 log.error(" StackTrace :", e);
99 public boolean chdir(String dir) {
100 boolean sucess = false;
102 if (ftpClient.changeWorkingDirectory(dir)) {
107 } catch (IOException e) {
108 log.error("chdir dir =" + dir + " is error" + StringUtil.getStackTrace(e));
116 public boolean downloadFile(String remoteFile, String localFile) {
117 boolean sucess = false;
119 BufferedOutputStream toLfileOutput = new BufferedOutputStream(new FileOutputStream(localFile))){
120 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
121 if (ftpClient.retrieveFile(remoteFile, toLfileOutput)) {
126 } catch (Exception ioe) {
128 log.error("downloadFile remoteFile =" + remoteFile + " is fail ", ioe);
135 public RemoteFile[] list() {
136 AFtpRemoteFile[] ftpRemoteFiles = null;
137 String currdir = null;
139 currdir = ftpClient.printWorkingDirectory();
140 if (!currdir.endsWith("/")) {
141 currdir = currdir + "/";
143 FTPFile[] rfileList = null;
144 rfileList = ftpClient.listFiles(currdir);
145 ftpRemoteFiles = new AFtpRemoteFile[rfileList.length];
146 for (int i = 0; i < rfileList.length; i++) {
147 ftpRemoteFiles[i] = new AFtpRemoteFile(rfileList[i], ftpClient, currdir);
149 } catch (IOException e) {
150 log.error("Ftp list currdir = " + currdir + " is fail " + StringUtil.getStackTrace(e));
152 return ftpRemoteFiles;
157 public String pwd() throws IOException {
158 return ftpClient.printWorkingDirectory();