SFTP的使用

1。登录

 1 JSch jsch = new JSch();
 2             jsch.getSession(username, host, port);
 3             Session sshSession = jsch.getSession(username, host, port); 
 4             sshSession.setPassword(password);
 5             Properties sshConfig = new Properties();
 6             sshConfig.put("StrictHostKeyChecking", "no");
 7             sshSession.setConfig(sshConfig);
 8             sshSession.connect(); 
 9             Channel channel = sshSession.openChannel("sftp");
10             channel.connect();
11             sftp = (ChannelSftp) channel;

 

2。上传文件和修改文件名称

 1 try {  
 2             File file = new File(ExcSystemConfig.getProperties("ExchageUpload","oa"));
 3             if (!file.exists()) {
 4                 file.mkdirs();
 5             } 
 6             File files[] = file.listFiles();
 7             String filePath = "";
 8             String path = exchangeContants.SFTPPATH; 
 9             if (files.length!=0) {
10                 for (int i = 0; i < files.length; i++) { 
11                     if (files[i].isFile()) {
12                         boolean result = false;
13                         String fileNmae = files[i].getName();
14                         String nameStr = files[i].getName()+".sftp";
15                         //得到文件的存放路径。
16                         String fileNamePath = files[i].getPath();
17                         Long size = files[i].length();
18                         filePath = fileNmae.substring(13, 15);
19                         if (filePath.equals("ST")) { 
20                             sftp.cd(path+"ST");
21                             //得到当前 sftp的路径 
22                             Vector vector = sftp.ls(path+"ST");
23                             Object obj[] = vector.toArray();
24                             for (int j = 0; j < obj.length; j++) {
25                                 if (obj[j]!=null) {
26                                     if (!obj[j].toString().contains(fileNmae)) {
27                                         result = true;
28                                     }
29                                 } 
30                             }
31                             if (obj.length==0) {
32                                 result = true;
33                             }
34                         }  
35                         if (filePath.equals("SD")) {
36                             sftp.cd(path+"SD");
37                             //得到当前 sftp的路径 
38                             String sftpPath = sftp.pwd();
39                              
40                             // 设置 上传文件的方式 ,当前方式为 上传的文件为 文件夹时,会在服务器上和当前文件名一样的一个文件夹
41                             // 如果 文件已经存在 将会覆盖原先的文件 。
42                             Vector vector = sftp.ls(path+"SD");
43                             Object obj[] = vector.toArray();
44                             for (int j = 0; j < obj.length; j++) {
45                                 if (obj[j]!=null) {
46                                     if (!obj[j].toString().contains(fileNmae)) {
47                                         result = true;
48                                     }
49                                 } 
50                             }
51                             if (obj.length==0) {
52                                 result = true;
53                             }
54                         }
55                         if (filePath.equals("MA")) {
56                             sftp.cd(path+"MA");
57                             //得到当前 sftp的路径 
58                             String sftpPath = sftp.pwd(); 
59                             Vector vector = sftp.ls(path+"MA");
60                             Object obj[] = vector.toArray();
61                             for (int j = 0; j < obj.length; j++) {
62                                 if (obj[j]!=null) {
63                                     if (!obj[j].toString().contains(fileNmae)) {
64                                         result = true;
65                                     }
66                                 } 
67                             }
68                             //判断 SFTP服务器上 是否存在文件 不存在 可以上传
69                             if (obj.length==0) {
70                                 result = true;
71                             }
72                         } 
73                         if (result) {
74                             int resultCount = FtpFileInfoBean.getFtpFileNameByName(bUserMdoel, fileNmae, "0");
75                             if (resultCount==0) {
76 //                                设置 上传文件的方式 ,当前方式为 上传的文件为 文件夹时,会在服务器上和当前文件名一样的一个文件夹
77                                 // 如果 文件已经存在 将会覆盖原先的文件 。 
78                                 sftp.put(fileNamePath, nameStr,sftp.OVERWRITE);
79                                 sftp.rename(nameStr,fileNmae);
80                                 this.addFtpFile(bUserMdoel,fileNmae,null,size,"0");
81                                 log(bUserMdoel,"文件上传",LogTypeContants.LOG_TYPE_ADD,"文件上传成功","文件上传成功:"+file.getName(), "文件上传成功");
82                             }else {
83                                 log(bUserMdoel,"文件上传",LogTypeContants.LOG_TYPE_ADD,"此文件已经上传",file.getName()+"此文件已经上传", "此文件已经上传");
84                             } 
85                         } 
86                     }
87                 }
88             }
89         }

 

posted @ 2012-12-27 10:24  一个吃货程序员  阅读(766)  评论(0编辑  收藏  举报