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.nfvo.emsdriver.commons.ftp;
18 import java.io.BufferedOutputStream;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.util.TimeZone;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.commons.net.ftp.FTP;
27 import org.apache.commons.net.ftp.FTPClient;
28 import org.apache.commons.net.ftp.FTPClientConfig;
29 import org.apache.commons.net.ftp.FTPFile;
30 import org.apache.commons.net.ftp.FTPReply;
31 import org.onap.nfvo.emsdriver.commons.utils.StringUtil;
34 public class FTPSrv implements FTPInterface{
35 private Log log = LogFactory.getLog(FTPSrv.class);
36 private FTPClient ftpClient = null;
49 public void login(String host, int port, String user, String pwd, String encode, boolean isPassiveMode,int timeout) throws Exception {
50 ftpClient = new FTPClient();
52 FTPClientConfig ftpClientConfig = new FTPClientConfig();
53 ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
54 this.ftpClient.setControlEncoding("GBK");
55 this.ftpClient.configure(ftpClientConfig);
56 ftpClient.setParserFactory(new ExtendsDefaultFTPFileEntryParserFactory());
58 if(encode!=null && encode.length()>0){
59 ftpClient.setControlEncoding(encode);
62 ftpClient.connect(host, port);
63 int reply = this.ftpClient.getReplyCode();
64 if (!FTPReply.isPositiveCompletion(reply)) {
65 this.ftpClient.disconnect();
69 if(!ftpClient.login(user, pwd)){
70 throw new Exception("login["+host+"],port["+port+"] fail, please check user and password");
73 ftpClient.enterLocalPassiveMode();
75 ftpClient.enterLocalActiveMode();
77 ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
78 this.ftpClient.setBufferSize(1024 * 2);
79 this.ftpClient.setDataTimeout(3*60 * 1000);
81 this.ftpClient.setSoTimeout(timeout);
92 if(ftpClient != null){
95 ftpClient.disconnect();
103 public boolean chdir(String dir) {
104 boolean sucess = false;
106 if(ftpClient.changeWorkingDirectory(dir)){
111 } catch (IOException e) {
112 log.error("chdir dir ="+dir+" is error"+StringUtil.getStackTrace(e));
120 public boolean downloadFile(String remoteFile, String localFile) {
121 boolean sucess = false;
122 BufferedOutputStream toLfileOutput = null;
124 toLfileOutput = new BufferedOutputStream(new FileOutputStream(localFile));
125 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
126 if (ftpClient.retrieveFile(remoteFile, toLfileOutput)){
131 } catch (Exception ioe) {
133 log.error("downloadFile remoteFile ="+remoteFile +" is fail ",ioe);
135 if (toLfileOutput != null)
137 toLfileOutput.close();
138 } catch (IOException e) {
146 public RemoteFile[] list() {
147 AFtpRemoteFile[] ftpRemoteFiles = null;
148 String currdir = null;
150 currdir = ftpClient.printWorkingDirectory();
151 if (currdir.endsWith("/") == false) {
152 currdir = currdir + "/";
154 FTPFile[] rfileList = null;
155 rfileList = ftpClient.listFiles(currdir);
156 ftpRemoteFiles = new AFtpRemoteFile[rfileList.length];
157 for (int i=0; i<rfileList.length; i++){
158 ftpRemoteFiles[i] = new AFtpRemoteFile(rfileList[i], ftpClient, currdir);
160 } catch (IOException e) {
161 log.error("Ftp list currdir = "+currdir+" is fail "+StringUtil.getStackTrace(e));
163 return ftpRemoteFiles;
167 public RemoteFile[] list(String dir) {
172 public boolean store(String localFile, String remoteFile) {
174 boolean sucess = false;
175 FileInputStream lfileInput = null;
177 lfileInput = new FileInputStream(localFile);
178 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
180 if (ftpClient.storeFile(remoteFile, lfileInput)){
185 } catch (Exception ioe) {
187 log.error("store localFile = "+localFile+" is fail "+StringUtil.getStackTrace(ioe));
189 if (lfileInput != null)
192 } catch (IOException e) {