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 java.io.BufferedOutputStream;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.util.TimeZone;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.commons.net.ftp.FTP;
26 import org.apache.commons.net.ftp.FTPClient;
27 import org.apache.commons.net.ftp.FTPClientConfig;
28 import org.apache.commons.net.ftp.FTPFile;
29 import org.apache.commons.net.ftp.FTPReply;
30 import org.onap.vfc.nfvo.emsdriver.commons.utils.StringUtil;
33 public class FTPSrv implements FTPInterface{
34 private Log log = LogFactory.getLog(FTPSrv.class);
35 private FTPClient ftpClient = null;
48 public void login(String host, int port, String user, String pwd, String encode, boolean isPassiveMode,int timeout) throws Exception {
49 ftpClient = new FTPClient();
51 FTPClientConfig ftpClientConfig = new FTPClientConfig();
52 ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
53 this.ftpClient.setControlEncoding("GBK");
54 this.ftpClient.configure(ftpClientConfig);
55 // ftpClient.setParserFactory(new ExtendsDefaultFTPFileEntryParserFactory());
57 if(encode!=null && encode.length()>0){
58 ftpClient.setControlEncoding(encode);
61 ftpClient.connect(host, port);
62 int reply = this.ftpClient.getReplyCode();
63 if (!FTPReply.isPositiveCompletion(reply)) {
64 this.ftpClient.disconnect();
68 if(!ftpClient.login(user, pwd)){
69 throw new Exception("login["+host+"],port["+port+"] fail, please check user and password");
72 ftpClient.enterLocalPassiveMode();
74 ftpClient.enterLocalActiveMode();
76 ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
77 this.ftpClient.setBufferSize(1024 * 2);
78 this.ftpClient.setDataTimeout(3*60 * 1000);
80 this.ftpClient.setSoTimeout(timeout);
91 if(ftpClient != null){
94 ftpClient.disconnect();
102 public boolean chdir(String dir) {
103 boolean sucess = false;
105 if(ftpClient.changeWorkingDirectory(dir)){
110 } catch (IOException e) {
111 log.error("chdir dir ="+dir+" is error"+StringUtil.getStackTrace(e));
119 public boolean downloadFile(String remoteFile, String localFile) {
120 boolean sucess = false;
121 BufferedOutputStream toLfileOutput = null;
123 toLfileOutput = new BufferedOutputStream(new FileOutputStream(localFile));
124 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
125 if (ftpClient.retrieveFile(remoteFile, toLfileOutput)){
130 } catch (Exception ioe) {
132 log.error("downloadFile remoteFile ="+remoteFile +" is fail ",ioe);
134 if (toLfileOutput != null)
136 toLfileOutput.close();
137 } catch (IOException e) {
145 public RemoteFile[] list() {
146 AFtpRemoteFile[] ftpRemoteFiles = null;
147 String currdir = null;
149 currdir = ftpClient.printWorkingDirectory();
150 if (currdir.endsWith("/") == false) {
151 currdir = currdir + "/";
153 FTPFile[] rfileList = null;
154 rfileList = ftpClient.listFiles(currdir);
155 ftpRemoteFiles = new AFtpRemoteFile[rfileList.length];
156 for (int i=0; i<rfileList.length; i++){
157 ftpRemoteFiles[i] = new AFtpRemoteFile(rfileList[i], ftpClient, currdir);
159 } catch (IOException e) {
160 log.error("Ftp list currdir = "+currdir+" is fail "+StringUtil.getStackTrace(e));
162 return ftpRemoteFiles;
167 public String pwd() throws IOException {
168 String returnValue = ftpClient.printWorkingDirectory();
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)
191 // lfileInput.close();
192 // } catch (IOException e) {