- import java.io.IOException;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPClientConfig;
- import org.apache.commons.net.ftp.FTPFile;
- import com.eshore.ccc.CCCLogger;
- public class FTP {
- private String serverIp = "59.41.223.227";
- private int port = 21;
- private String username ="upload";
- private String password = "suntekgc";
- private String rootpath = "/";
- private String ftptype = "1";
- private FTPClient ftpClient;
- public FTP() {
- }
- public FTP(String serverIp, int port, String username, String password, String rootpath, String ftptype) {
- this.serverIp = serverIp;
- this.port = port;
- this.username = username;
- this.password = password;
- this.rootpath = rootpath;
- if(this.rootpath.equals("")) this.rootpath = "/";
- this.ftptype = ftptype;
- }
- private boolean connectFTPServer() {
- this.ftpClient = new FTPClient();
- try {
- //String FTPSERV_TYPE = AppHandle.getHandle("ccf").getProperty("FTPSERV_TYPE", "2");
- FTPClientConfig conf = null;
- if(this.ftptype.equals("1"))
- conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
- else
- conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
- ftpClient.configure(conf);
- ftpClient.setDataTimeout(60000);
- ftpClient.setControlEncoding("GBK");
- ftpClient.setDefaultTimeout(60000);
- ftpClient.connect(this.serverIp, port);
- boolean b = ftpClient.login(username, password);
- if(b)
- CCCLogger.logger.info("登录FTP服务器成功.");
- else
- CCCLogger.logger.info("登录FTP服务器失败.");
- return b;
- } catch (IOException e) {
- CCCLogger.logger.error("登录FTP服务器失败", e);
- return false;
- }
- }
- public boolean remoteV3ToLocal(String voxFile, String localFile) {
- BufferedInputStream bis = null;
- BufferedOutputStream bos = null;
- try {
- if (!connectFTPServer())
- return false;
- ftpClient.enterLocalPassiveMode();
- ftpClient.setFileTransferMode(org.apache.commons.net.ftp.FTP.STREAM_TRANSFER_MODE);
- ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);
- // 转到指定下载目录
- String remoteDir = "/";
- String remoteFileName = voxFile;
- if (voxFile.contains("/")) {
- remoteDir = voxFile.substring(0, voxFile.lastIndexOf("/"));
- remoteFileName = voxFile.substring(voxFile.lastIndexOf("/") + 1);
- }
- if(this.rootpath.endsWith("/"))
- remoteDir = this.rootpath + remoteDir;
- else
- remoteDir = this.rootpath + "/" + remoteDir;
- FTPFile file = null;
- try {
- if(ftpClient.changeWorkingDirectory(remoteDir))
- CCFLogger.logger.info("进入FTP目录:" + remoteDir);
- else {
- CCFLogger.logger.info("进入FTP目录:" + remoteDir + "失败");
- return false;
- }
- FTPFile[] remoteFiles = ftpClient.listFiles();
- // 遍历所有文件,找到指定的文件
- for (int i = 0; i < remoteFiles.length; i++) {
- if (remoteFiles[i].getName().equals(remoteFileName)) {
- file = remoteFiles[i];
- CCFLogger.logger.info("从FTP上找到了文件:" + remoteFileName);
- break;
- }
- }
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- if(file == null) {
- CCFLogger.logger.info("没有从FTP上找到文件:" + remoteFileName);
- return false;
- }
- File outFile = new File(localFile);
- bis = new BufferedInputStream(ftpClient.retrieveFileStream(file.getName()));
- bos = new BufferedOutputStream(new FileOutputStream(outFile));
- byte[] szBuf = new byte[128 * 1024];
- int dwRead = 0;
- while (( dwRead = bis.read(szBuf, 0, 128 * 1024)) != -1) {
- try {
- bos.write(szBuf, 0,dwRead);
- bos.flush();
- } catch (Exception e) {
- }
- }
- if (ftpClient.isConnected()) {
- CCFLogger.logger.info("断开FTP连接!");
- try {
- ftpClient.disconnect();
- } catch (IOException ioe) {}
- }
- try {
- if (bis != null) bis.close();
- if (bos != null) bos.close();
- } catch (IOException e) {}
- return true;
- }
- catch (Exception e) {
- CCFLogger.logger.error("从FTP下载文件到服务器失败!", e);
- return false;
- }
- finally {
- try {
- if (bis != null) bis.close();
- if (bos != null) bos.close();
- } catch (IOException e) {}
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/181220138114.html
来源: http://www.codesnippet.cn/detail/181220138114.html