123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- package com.chinacreator.process.util.ftp;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.net.SocketException;
- import java.util.Calendar;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPReply;
- public class FtpUtil {
-
- private Log logger = LogFactory.getLog(FtpUtil.class);
-
- private FTPClient ftpClient = null;
-
- private String errorcode="0000";
- private String errorinfo="OK";
-
- /**
- * 获取FTPClient对象
- *
- * @param ftpHost
- * FTP主机服务器
- * @param ftpPassword
- * FTP 登录密码
- * @param ftpUserName
- * FTP登录用户名
- * @param ftpPort
- * FTP端口 默认为21
- * @return
- */
- private FTPClient connectFTP(String host, String user, String pwd, int port) {
- try {
- ftpClient = new FTPClient();
- ftpClient.connect(host, port);// 连接FTP服务器
- ftpClient.login(user, pwd);// 登陆FTP服务器
- int reply = ftpClient.getReplyCode();
- //ftpClient.setDataTimeout(120000);
- if (!FTPReply.isPositiveCompletion(reply)) {
- logger.debug("FTP服务"+host+"拒绝连接!");
- ftpClient.disconnect();
- } else {
- return ftpClient;
- }
- } catch (SocketException e) {
- e.printStackTrace();
- logger.info("登录ftp服务器 " + host + " 失败,连接超时!");
- } catch (IOException e) {
- e.printStackTrace();
- logger.info("登录ftp服务器 " + host + " 失败,FTP服务器无法打开!");
- }
- return null;
- }
-
- /**
- * 关闭连接
- */
- private void closeConnect() {
- try {
- if (ftpClient != null) {
- ftpClient.logout();
- ftpClient.disconnect();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- /**
- * 上传对账文件
- *
- * @param url
- * @param user
- * @param pwd
- * @param port
- * @param ftppath
- * @param filepath
- * @param filenames
- * @return
- */
- public boolean UpBillFiles(String url,String user,String pwd,String port,String ftppath,String filepath,String filenames){
- try {
- //链接FTP
- int p = Integer.parseInt(port);
- FTPClient client = this.connectFTP(url, user, pwd, p);
- if(client == null){
- errorcode = "0101";
- errorinfo = "FTP服务器连接失败";
- return false;
- }
-
-
- //进入对应目录
- if(ftppath!=null && !"".equals(ftppath.trim())){
- String paths [] = ftppath.split("/");
- for(String fpath:paths){
- boolean dirFlag = client.changeWorkingDirectory(fpath);
- if(!dirFlag){
- dirFlag = client.makeDirectory(fpath);
- if(dirFlag){
- dirFlag = client.changeWorkingDirectory(fpath);
- }
- if(!dirFlag){
- logger.info("FTP服务器 " + url + "目录"+ftppath+"不存在,且创建失败!");
- errorcode = "0201";
- errorinfo = "进入FTP服务器目录"+ftppath+"失败";
- return false;
- }
- }
- }
- }
-
- //对账文件保存路径
- if(filepath == null || "".equals(filepath.trim()) || filenames == null || "".equals(filenames.trim())){
- logger.info("文件目录或文件名异常");
- errorcode = "0301";
- errorinfo = "文件目录或文件名异常";
- return false;
- }
-
- //上传文件
- client.setFileType(FTP.BINARY_FILE_TYPE);
- String [] fnames = filenames.split(",");
- for(String fname :fnames){
- File billfile = new File(filepath+fname);
- if(!billfile.exists()){
- logger.info("文件"+filepath+fname+"不存在");
- errorcode = "0302";
- errorinfo = "文件"+filepath+fname+"不存在";
- return false;
- }
- FileInputStream input=new FileInputStream(billfile);
- client.enterLocalPassiveMode();
- boolean flag = client.storeFile(fname, input);
- if(!flag){
- logger.info("上传文件"+filepath+fname+"失败");
- errorcode = "0303";
- errorinfo = "上传文件"+filepath+fname+"失败";
- return false;
- }
- }
-
- //关闭ftp
- this.closeConnect();
-
- return true;
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- errorcode = "8888";
- errorinfo = "系统异常";
- return false;
- }
-
-
-
- public String getErrorcode() {
- return errorcode;
- }
-
- public void setErrorcode(String errorcode) {
- this.errorcode = errorcode;
- }
-
- public String getErrorinfo() {
- return errorinfo;
- }
-
- public void setErrorinfo(String errorinfo) {
- this.errorinfo = errorinfo;
- }
-
- public static void main(String[] args) {
- try {
- // String paths [] = "tencents/201905".split("/");
- // System.out.println(paths[0]);
- // System.out.println(paths[1]);
-
- // Calendar cal = Calendar.getInstance();
- // int day = cal.get(Calendar.DAY_OF_MONTH);
- // int hour = cal.get(Calendar.HOUR_OF_DAY);
- //
- // System.out.println(day);
- // System.out.println(hour);
-
-
- String AppSecret = "MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAp8iizhCSw53AIbIpRN6LQ2IAybTtYaBA0C/1mqmifd+IrAPbTuPh/6R/6f4Y9Vu0b/s8oECIwJ7yCy0NFLEcAwIDAQABAkBPidGEFl881J+sWaUuvMEQJHlzJY4S5cFT8ChiNWCOgpAey5sfh3I0JLvr8IMLu2FJr5RzzCQuCvezf8CB845hAiEA4tot/J3V9D/hLwotQ2GzZQORH/15g3wzobvepB28qk0CIQC9V4X1ij1HI84gUZcH55YkEoaJ5zr5UVi1vX71wW1njwIhALJA8MvEln9zxpU48PI2jkl8sQerHFWWPdgDkOHyv/ItAiAT6VxhEgSXsqA+rdXgMu6LJJeZcQO2rGNT2XW8inbi6QIhAKEG9c5ftAkikKmS8gXy4yc/4AY2+M/yzDGuf+cgO9Xd";
- String AppSecret2= "MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAp8iizhCSw53AIbIpRN6LQ2IAybTtYaBA0C/1mqmifd+IrAPbTuPh/6R/6f4Y9Vu0b/s8oECIwJ7yCy0NFLEcAwIDAQABAkBPidGEFl881J+sWaUuvMEQJHlzJY4S5cFT8ChiNWCOgpAey5sfh3I0JLvr8IMLu2FJr5RzzCQuCvezf8CB845hAiEA4tot/J3V9D/hLwotQ2GzZQORH/15g3wzobvepB28qk0CIQC9V4X1ij1HI84gUZcH55YkEoaJ5zr5UVi1vX71wW1njwIhALJA8MvEln9zxpU48PI2jkl8sQerHFWWPdgDkOHyv/ItAiAT6VxhEgSXsqA+rdXgMu6LJJeZcQO2rGNT2XW8inbi6QIhAKEG9c5ftAkikKmS8gXy4yc/4AY2+M/yzDGuf+cgO9Xd";
-
- System.out.println(AppSecret.equals(AppSecret2));
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
|