c0de2ff6f49e52978072b6c835dd06ccb1e78d58.svn-base 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.chinacreator.process.util.ftp;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.net.SocketException;
  6. import java.util.Calendar;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import org.apache.commons.net.ftp.FTP;
  10. import org.apache.commons.net.ftp.FTPClient;
  11. import org.apache.commons.net.ftp.FTPReply;
  12. public class FtpUtil {
  13. private Log logger = LogFactory.getLog(FtpUtil.class);
  14. private FTPClient ftpClient = null;
  15. private String errorcode="0000";
  16. private String errorinfo="OK";
  17. /**
  18. * 获取FTPClient对象
  19. *
  20. * @param ftpHost
  21. * FTP主机服务器
  22. * @param ftpPassword
  23. * FTP 登录密码
  24. * @param ftpUserName
  25. * FTP登录用户名
  26. * @param ftpPort
  27. * FTP端口 默认为21
  28. * @return
  29. */
  30. private FTPClient connectFTP(String host, String user, String pwd, int port) {
  31. try {
  32. ftpClient = new FTPClient();
  33. ftpClient.connect(host, port);// 连接FTP服务器
  34. ftpClient.login(user, pwd);// 登陆FTP服务器
  35. int reply = ftpClient.getReplyCode();
  36. //ftpClient.setDataTimeout(120000);
  37. if (!FTPReply.isPositiveCompletion(reply)) {
  38. logger.debug("FTP服务"+host+"拒绝连接!");
  39. ftpClient.disconnect();
  40. } else {
  41. return ftpClient;
  42. }
  43. } catch (SocketException e) {
  44. e.printStackTrace();
  45. logger.info("登录ftp服务器 " + host + " 失败,连接超时!");
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. logger.info("登录ftp服务器 " + host + " 失败,FTP服务器无法打开!");
  49. }
  50. return null;
  51. }
  52. /**
  53. * 关闭连接
  54. */
  55. private void closeConnect() {
  56. try {
  57. if (ftpClient != null) {
  58. ftpClient.logout();
  59. ftpClient.disconnect();
  60. }
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. /**
  66. * 上传对账文件
  67. *
  68. * @param url
  69. * @param user
  70. * @param pwd
  71. * @param port
  72. * @param ftppath
  73. * @param filepath
  74. * @param filenames
  75. * @return
  76. */
  77. public boolean UpBillFiles(String url,String user,String pwd,String port,String ftppath,String filepath,String filenames){
  78. try {
  79. //链接FTP
  80. int p = Integer.parseInt(port);
  81. FTPClient client = this.connectFTP(url, user, pwd, p);
  82. if(client == null){
  83. errorcode = "0101";
  84. errorinfo = "FTP服务器连接失败";
  85. return false;
  86. }
  87. //进入对应目录
  88. if(ftppath!=null && !"".equals(ftppath.trim())){
  89. String paths [] = ftppath.split("/");
  90. for(String fpath:paths){
  91. boolean dirFlag = client.changeWorkingDirectory(fpath);
  92. if(!dirFlag){
  93. dirFlag = client.makeDirectory(fpath);
  94. if(dirFlag){
  95. dirFlag = client.changeWorkingDirectory(fpath);
  96. }
  97. if(!dirFlag){
  98. logger.info("FTP服务器 " + url + "目录"+ftppath+"不存在,且创建失败!");
  99. errorcode = "0201";
  100. errorinfo = "进入FTP服务器目录"+ftppath+"失败";
  101. return false;
  102. }
  103. }
  104. }
  105. }
  106. //对账文件保存路径
  107. if(filepath == null || "".equals(filepath.trim()) || filenames == null || "".equals(filenames.trim())){
  108. logger.info("文件目录或文件名异常");
  109. errorcode = "0301";
  110. errorinfo = "文件目录或文件名异常";
  111. return false;
  112. }
  113. //上传文件
  114. client.setFileType(FTP.BINARY_FILE_TYPE);
  115. String [] fnames = filenames.split(",");
  116. for(String fname :fnames){
  117. File billfile = new File(filepath+fname);
  118. if(!billfile.exists()){
  119. logger.info("文件"+filepath+fname+"不存在");
  120. errorcode = "0302";
  121. errorinfo = "文件"+filepath+fname+"不存在";
  122. return false;
  123. }
  124. FileInputStream input=new FileInputStream(billfile);
  125. client.enterLocalPassiveMode();
  126. boolean flag = client.storeFile(fname, input);
  127. if(!flag){
  128. logger.info("上传文件"+filepath+fname+"失败");
  129. errorcode = "0303";
  130. errorinfo = "上传文件"+filepath+fname+"失败";
  131. return false;
  132. }
  133. }
  134. //关闭ftp
  135. this.closeConnect();
  136. return true;
  137. } catch (Exception e) {
  138. e.printStackTrace();
  139. }
  140. errorcode = "8888";
  141. errorinfo = "系统异常";
  142. return false;
  143. }
  144. public String getErrorcode() {
  145. return errorcode;
  146. }
  147. public void setErrorcode(String errorcode) {
  148. this.errorcode = errorcode;
  149. }
  150. public String getErrorinfo() {
  151. return errorinfo;
  152. }
  153. public void setErrorinfo(String errorinfo) {
  154. this.errorinfo = errorinfo;
  155. }
  156. public static void main(String[] args) {
  157. try {
  158. // String paths [] = "tencents/201905".split("/");
  159. // System.out.println(paths[0]);
  160. // System.out.println(paths[1]);
  161. // Calendar cal = Calendar.getInstance();
  162. // int day = cal.get(Calendar.DAY_OF_MONTH);
  163. // int hour = cal.get(Calendar.HOUR_OF_DAY);
  164. //
  165. // System.out.println(day);
  166. // System.out.println(hour);
  167. String AppSecret = "MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAp8iizhCSw53AIbIpRN6LQ2IAybTtYaBA0C/1mqmifd+IrAPbTuPh/6R/6f4Y9Vu0b/s8oECIwJ7yCy0NFLEcAwIDAQABAkBPidGEFl881J+sWaUuvMEQJHlzJY4S5cFT8ChiNWCOgpAey5sfh3I0JLvr8IMLu2FJr5RzzCQuCvezf8CB845hAiEA4tot/J3V9D/hLwotQ2GzZQORH/15g3wzobvepB28qk0CIQC9V4X1ij1HI84gUZcH55YkEoaJ5zr5UVi1vX71wW1njwIhALJA8MvEln9zxpU48PI2jkl8sQerHFWWPdgDkOHyv/ItAiAT6VxhEgSXsqA+rdXgMu6LJJeZcQO2rGNT2XW8inbi6QIhAKEG9c5ftAkikKmS8gXy4yc/4AY2+M/yzDGuf+cgO9Xd";
  168. String AppSecret2= "MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAp8iizhCSw53AIbIpRN6LQ2IAybTtYaBA0C/1mqmifd+IrAPbTuPh/6R/6f4Y9Vu0b/s8oECIwJ7yCy0NFLEcAwIDAQABAkBPidGEFl881J+sWaUuvMEQJHlzJY4S5cFT8ChiNWCOgpAey5sfh3I0JLvr8IMLu2FJr5RzzCQuCvezf8CB845hAiEA4tot/J3V9D/hLwotQ2GzZQORH/15g3wzobvepB28qk0CIQC9V4X1ij1HI84gUZcH55YkEoaJ5zr5UVi1vX71wW1njwIhALJA8MvEln9zxpU48PI2jkl8sQerHFWWPdgDkOHyv/ItAiAT6VxhEgSXsqA+rdXgMu6LJJeZcQO2rGNT2XW8inbi6QIhAKEG9c5ftAkikKmS8gXy4yc/4AY2+M/yzDGuf+cgO9Xd";
  169. System.out.println(AppSecret.equals(AppSecret2));
  170. } catch (Exception e) {
  171. e.printStackTrace();
  172. }
  173. }
  174. }