1470251aaecde2ca88ba8e922c7dd7f671ffdba6.svn-base 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package com.chinacreator.process.util.ftp;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.util.Properties;
  5. import org.apache.commons.logging.Log;
  6. import org.apache.commons.logging.LogFactory;
  7. import com.jcraft.jsch.Channel;
  8. import com.jcraft.jsch.ChannelSftp;
  9. import com.jcraft.jsch.JSch;
  10. import com.jcraft.jsch.Session;
  11. public class SFtpUtil {
  12. private Log logger = LogFactory.getLog(FtpUtil.class);
  13. private ChannelSftp ftpClient = null;
  14. private Session sshSession = null;
  15. private String errorcode="0000";
  16. private String errorinfo="OK";
  17. private ChannelSftp connectFTP(String host, String user, String pwd, int port){
  18. try {
  19. JSch jsch = new JSch();
  20. //获取sshSession 账号-ip-端口
  21. sshSession =jsch.getSession(user, host,port);
  22. //添加密码
  23. sshSession.setPassword(pwd);
  24. Properties sshConfig = new Properties();
  25. //严格主机密钥检查
  26. sshConfig.put("StrictHostKeyChecking", "no");
  27. sshSession.setConfig(sshConfig);
  28. //开启sshSession链接
  29. sshSession.connect();
  30. //获取sftp通道
  31. Channel channel = sshSession.openChannel("sftp");
  32. //开启
  33. channel.connect();
  34. ftpClient = (ChannelSftp) channel;
  35. return ftpClient;
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. logger.info("登录ftp服务器 " + host + " 失败,连接超时!");
  39. }
  40. return null;
  41. }
  42. /**
  43. * 关闭连接
  44. */
  45. private void closeConnect() {
  46. try {
  47. if(ftpClient!=null && ftpClient.isConnected()){
  48. ftpClient.disconnect();
  49. }
  50. if(sshSession!=null && sshSession.isConnected()){
  51. sshSession.disconnect();
  52. }
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. /**
  58. * 上传对账文件
  59. *
  60. * @param url
  61. * @param user
  62. * @param pwd
  63. * @param port
  64. * @param ftppath
  65. * @param filepath
  66. * @param filenames
  67. * @return
  68. */
  69. public boolean UpBillFiles(String url,String user,String pwd,String port,String ftppath,String filepath,String filenames){
  70. try {
  71. //链接FTP
  72. int p = Integer.parseInt(port);
  73. ChannelSftp client = this.connectFTP(url, user, pwd, p);
  74. if(client == null){
  75. errorcode = "0101";
  76. errorinfo = "FTP服务器 " + url + "连接失败";
  77. return false;
  78. }
  79. //进入对应目录
  80. if(ftppath!=null && !"".equals(ftppath.trim())){
  81. try {
  82. client.cd(ftppath);
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. logger.info("FTP服务器 " + url + "目录"+ftppath+"进入失败");
  86. errorcode = "0201";
  87. errorinfo = "进入FTP服务器" + url + "目录"+ftppath+"失败";
  88. return false;
  89. }
  90. }
  91. //对账文件保存路径
  92. if(filepath == null || "".equals(filepath.trim()) || filenames == null || "".equals(filenames.trim())){
  93. logger.info("文件目录或文件名异常");
  94. errorcode = "0301";
  95. errorinfo = "文件目录或文件名异常";
  96. return false;
  97. }
  98. //上传文件
  99. String [] fnames = filenames.split(",");
  100. for(String fname :fnames){
  101. File billfile = new File(filepath+fname);
  102. if(!billfile.exists()){
  103. logger.info("文件"+filepath+fname+"不存在");
  104. errorcode = "0302";
  105. errorinfo = "文件"+filepath+fname+"不存在";
  106. return false;
  107. }
  108. try {
  109. FileInputStream input=new FileInputStream(billfile);
  110. client.put(input, fname);
  111. } catch (Exception e) {
  112. e.printStackTrace();
  113. logger.info("上传文件"+filepath+fname+"失败");
  114. errorcode = "0303";
  115. errorinfo = "上传文件"+filepath+fname+"失败";
  116. return false;
  117. }
  118. }
  119. //关闭ftp
  120. this.closeConnect();
  121. return true;
  122. } catch (Exception e) {
  123. e.printStackTrace();
  124. }
  125. return false;
  126. }
  127. public String getErrorcode() {
  128. return errorcode;
  129. }
  130. public void setErrorcode(String errorcode) {
  131. this.errorcode = errorcode;
  132. }
  133. public String getErrorinfo() {
  134. return errorinfo;
  135. }
  136. public void setErrorinfo(String errorinfo) {
  137. this.errorinfo = errorinfo;
  138. }
  139. }