java使用JSCH连接FTP(Linux服务器)上传文件到Linux服务器

首先需要用到jsch-0.1.54.jar 包;

链接: https://pan.baidu.com/s/1kZR6MqwpCYht9Pp_D6NKQw 密码: gywx

直接上代码:

  1 package test;
  2 
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.util.ArrayList;
  6 import java.util.List;
  7 import java.util.Properties;
  8 import java.util.Vector;
  9 
 10 import org.slf4j.Logger;
 11 import org.slf4j.LoggerFactory;
 12 
 13 import com.jcraft.jsch.Channel;
 14 import com.jcraft.jsch.ChannelSftp;
 15 import com.jcraft.jsch.ChannelSftp.LsEntry;
 16 import com.jcraft.jsch.JSch;
 17 import com.jcraft.jsch.JSchException;
 18 import com.jcraft.jsch.Session;
 19 import com.jcraft.jsch.SftpException;
 20 import com.opensymphony.xwork2.util.finder.ClassFinder.Info;
 21 
 22 public class FTPUtil {
 23     private static final Logger LOG = LoggerFactory.getLogger(FTPUtil.class);
 24 
 25     /**
 26      * 参考实例
 27      * 
 28      * @param args
 29      */
 30     // public static void main(String[] args) {
 31     // File file = new File("D://36.txt");
 32     // FTPUtil ft = new FTPUtil();
 33     // Session s = ft.getSession(SFTPInfo.SFTP_REQ_HOST,
 34     // SFTPInfo.SFTP_DEFAULT_PORT, SFTPInfo.SFTP_REQ_USERNAME,
 35     // SFTPInfo.SFTP_REQ_PASSWORD);
 36     // Channel channel = ft.getChannel(s);
 37     // ChannelSftp sftp = (ChannelSftp)channel;
 38     // String upload = ft.uploadFile(sftp,"hot_Imgs",file);
 39     // System.out.println(upload);
 40     // ft.closeAll(sftp, channel, s); //关闭连接
 41     // }
 42     public Channel getChannel(Session session) {
 43         Channel channel = null;
 44         try {
 45             channel = session.openChannel("sftp");
 46             channel.connect();
 47             LOG.info("get Channel success!");
 48         } catch (JSchException e) {
 49             LOG.info("get Channel fail!", e);
 50         }
 51         return channel;
 52     }
 53 
 54     public Session getSession(String host, int port, String username,
 55             final String password) {
 56         Session session = null;
 57         try {
 58             JSch jsch = new JSch();
 59             jsch.getSession(username, host, port);
 60             session = jsch.getSession(username, host, port);
 61             session.setPassword(password);
 62             Properties sshConfig = new Properties();
 63             sshConfig.put("StrictHostKeyChecking", "no");
 64             session.setConfig(sshConfig);
 65             session.connect();
 66             LOG.info("Session connected!");
 67         } catch (JSchException e) {
 68             LOG.info("get Channel failed!", e);
 69         }
 70         return session;
 71     }
 72 
 73     /**
 74      * 创建文件夹
 75      * 
 76      * @param sftp
 77      * @param dir
 78      *            文件夹名称
 79      */
 80     public void mkdir(ChannelSftp sftp, String dir) {
 81         try {
 82             sftp.mkdir(dir);
 83             System.out.println("创建文件夹成功!");
 84         } catch (SftpException e) {
 85             System.out.println("创建文件夹失败!");
 86             e.printStackTrace();
 87         }
 88     }
 89 
 90     /**
 91      * @param sftp
 92      * @param dir
 93      *            上传目录
 94      * @param file
 95      *            上传文件
 96      * @return
 97      */
 98     public String uploadFile(ChannelSftp sftp, String dir, File file) {
 99         String result = "";
100         try {
101             sftp.cd(dir);
102             // File file = new File("D://34.txt"); //要上传的本地文件
103             if (file != null) {
104                 sftp.put(new FileInputStream(file), file.getName());
105                 result = "上传成功!";
106             } else {
107                 result = "文件为空!不能上传!";
108             }
109         } catch (Exception e) {
110             LOG.info("上传失败!", e);
111             result = "上传失败!";
112         }
113         return result;
114     }
115 
116     /**
117      * 下载文件
118      * 
119      * @param directory
120      *            下载目录
121      * @param downloadFile
122      *            下载的文件
123      * @param saveFile
124      *            存在本地的路径
125      * @param sftp
126      */
127     public String download(String directory, String downloadFile,
128             String saveFile, ChannelSftp sftp) {
129         String result = "";
130         try {
131             sftp.cd(directory);
132             sftp.get(downloadFile, saveFile);
133             result = "下载成功!";
134         } catch (Exception e) {
135             result = "下载失败!";
136             LOG.info("下载失败!", e);
137             ;
138         }
139         return result;
140     }
141 
142     /**
143      * 删除文件
144      * 
145      * @param directory
146      *            要删除文件所在目录
147      * @param deleteFile
148      *            要删除的文件
149      * @param sftp
150      */
151     public String delete(String directory, String deleteFile, ChannelSftp sftp) {
152         String result = "";
153         try {
154             sftp.cd(directory);
155             sftp.rm(deleteFile);
156             result = "删除成功!";
157         } catch (Exception e) {
158             result = "删除失败!";
159             LOG.info("删除失败!", e);
160         }
161         return result;
162     }
163 
164     private void closeChannel(Channel channel) {
165         if (channel != null) {
166             if (channel.isConnected()) {
167                 channel.disconnect();
168             }
169         }
170     }
171 
172     private void closeSession(Session session) {
173         if (session != null) {
174             if (session.isConnected()) {
175                 session.disconnect();
176             }
177         }
178     }
179 
180     public void closeAll(ChannelSftp sftp, Channel channel, Session session) {
181         try {
182             closeChannel(sftp);
183             closeChannel(channel);
184             closeSession(session);
185         } catch (Exception e) {
186             LOG.info("closeAll", e);
187         }
188     }
189 }
1 package test;
2 
3 public class SFTPInfo {  
4     public static final String SFTP_REQ_HOST = "10.10.2.20";      //ip
5     public static final String SFTP_REQ_USERNAME = "gaowei";      //username
6     public static final String SFTP_REQ_PASSWORD = "gaowei";      //password
7     public static final int SFTP_DEFAULT_PORT = 22;      //端口
8 }  

 

posted @ 2018-03-09 11:11  谱写自己的人生  阅读(8033)  评论(0编辑  收藏  举报