本 文为实现对linux服务器文件的操作。windows服务器不支持。 

引 入jar包:jsch-0.1.42.jar 
Java代码  收藏代码
  1. package com.csvreader.sftp;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileOutputStream;  
  6. import java.util.List;  
  7. import java.util.Properties;  
  8. import java.util.Vector;  
  9.   
  10. import org.junit.Test;  
  11.   
  12. import com.jcraft.jsch.Channel;  
  13. import com.jcraft.jsch.ChannelSftp;  
  14. import com.jcraft.jsch.JSch;  
  15. import com.jcraft.jsch.JSchException;  
  16. import com.jcraft.jsch.Session;  
  17. import com.jcraft.jsch.SftpException;  
  18.   
  19. public class SftpUtil {  
  20.     /** 
  21.      *  
  22.      * @param host 
  23.      * @param port 
  24.      * @param username 
  25.      * @param password 
  26.      * @return 
  27.      * @throws JSchException 
  28.      */  
  29.     public ChannelSftp connect(String host, int port, String username,String password) throws JSchException {  
  30.         // 1. 声明连接Sftp的通道  
  31.         ChannelSftp nChannelSftp = null;  
  32.         // 2. 实例化JSch  
  33.         JSch nJSch = new JSch();  
  34.         // 3. 获取session  
  35.         Session nSShSession = nJSch.getSession(username, host, port);  
  36.         System.out.println("Session创建成功");  
  37.         // 4. 设置密码  
  38.         nSShSession.setPassword(password);  
  39.         // 5. 实例化Properties  
  40.         Properties nSSHConfig = new Properties();  
  41.         // 6. 设置配置信息  
  42.         nSSHConfig.put("StrictHostKeyChecking""no");  
  43.         // 7.session 中设置配置信息  
  44.         nSShSession.setConfig(nSSHConfig);  
  45.         // 8.session 连接  
  46.         nSShSession.connect();  
  47.         System.out.println("Session已连接");  
  48.         // 9. 打开sftp通道  
  49.         Channel channel = nSShSession.openChannel("sftp");  
  50.         // 10. 开始连接  
  51.         channel.connect();  
  52.         nChannelSftp = (ChannelSftp) channel;  
  53.         System.out.println("连接到主机" + host + ".");  
  54.         return nChannelSftp;  
  55.     }  
  56.   
  57.     /** 
  58.      *  文件重命名 
  59.      * @param directory 
  60.      * @param oldname 
  61.      * @param newname 
  62.      * @param sftp 
  63.      */  
  64.     public void renameFile(String directory, String oldname, String newname,ChannelSftp sftp) {  
  65.         try {  
  66.             sftp.cd(directory);  
  67.             sftp.rename(oldname, newname);  
  68.         } catch (Exception e) {  
  69.             e.printStackTrace();  
  70.         }  
  71.     }  
  72.   
  73.     /** 
  74.      *  上传文件 
  75.      * @param directory 
  76.      * @param uploadFile 
  77.      * @param sftp 
  78.      */  
  79.     public void upload(String directory, String uploadFile, ChannelSftp sftp) {  
  80.         try {  
  81.             sftp.cd(directory);  
  82.             File file = new File(uploadFile);  
  83.             sftp.put(new FileInputStream(file), file.getName());  
  84.         } catch (Exception e) {  
  85.             e.printStackTrace();  
  86.         }  
  87.     }  
  88.   
  89.     /** 
  90.      *  下载文件 
  91.      * @param directory 
  92.      * @param downloadFile 
  93.      * @param saveFile 
  94.      * @param sftp 
  95.      */  
  96.     public void download(String directory, String downloadFile,String saveFile, ChannelSftp sftp) {  
  97.         try {  
  98.             sftp.cd(directory);  
  99.             File file = new File(saveFile);  
  100.             sftp.get(downloadFile, new FileOutputStream(file));  
  101.   
  102.         } catch (Exception e) {  
  103.             e.printStackTrace();  
  104.         }  
  105.     }  
  106.   
  107.     /** 
  108.      *  删除文件 
  109.      * @param directory 
  110.      * @param deleteFile 
  111.      * @param sftp 
  112.      */  
  113.     public void delete(String directory, String deleteFile, ChannelSftp sftp) {  
  114.         try {  
  115.             sftp.cd(directory);  
  116.             sftp.rm(deleteFile);  
  117.             System.out.println("删除成功");  
  118.         } catch (Exception e) {  
  119.             System.out.println("删除失败");  
  120.             e.printStackTrace();  
  121.         }  
  122.     }  
  123.   
  124.     /** 
  125.      *  列出列表下的文件 
  126.      * @param directory 
  127.      * @param sftp 
  128.      * @return 
  129.      * @throws SftpException 
  130.      */  
  131.     public Vector listFiles(String directory, ChannelSftp sftp)throws SftpException {  
  132.         return sftp.ls(directory);  
  133.     }  
  134.   
  135.     /** 
  136.      *  下载文件夹下面的所有文件 
  137.      *  
  138.      * @param viDirectory  文件夹 
  139.      * @param viHost  主机名 
  140.      * @param viPort  端口号 
  141.      * @param viUserName  用户名 
  142.      * @param viPassWord  用户密码 
  143.      * @param viSaveDir  保存路径 
  144.      * @return 
  145.      */  
  146.     @Test  
  147.     public List<String> downloadDirFile(String viDirectory, String viHost,int viPort, String viUserName, String viPassWord, String viSaveDir) {  
  148.         ChannelSftp nChannelSftp = null;  
  149.         List<String> nFileNameList = null;  
  150.         try {  
  151.             // 1.实例化nSftpUtil 工具类  
  152.             SftpUtil nSftpUtil = new SftpUtil();  
  153.             // 2.建立Sftp通道  
  154.             nChannelSftp = nSftpUtil.connect(viHost, 22, viUserName, viPassWord);  
  155.             // 3.获取目录下面所有文件  
  156.             Vector nVector = nChannelSftp.ls(viDirectory);  
  157.             // 4.循环遍历文件  
  158.             for (int i = 0; i < nVector.size(); i++) {  
  159.                 // 5. 进入服务器文件夹  
  160.                 nChannelSftp.cd(viDirectory);  
  161.                 // 6. 实例化文件对象  
  162.                 String nFileName = nVector.get(i).toString().substring(56, nVector.get(i).toString().length());  
  163.                 if (!nFileName.contains("csv")) {  
  164.                     continue;  
  165.                 }  
  166.                 File nFile = new File(viSaveDir + File.separator + nFileName);  
  167.                 // 7. 下载文件  
  168.                 nChannelSftp.get(nFileName, new FileOutputStream(nFile));  
  169.             }  
  170.         } catch (Exception e) {  
  171.             e.printStackTrace();  
  172.         } finally {  
  173.             nChannelSftp.disconnect();  
  174.         }  
  175.         return nFileNameList;  
  176.     }