1 packagetest;2
3 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.util.ArrayList;6 importjava.util.List;7 importjava.util.Properties;8 importjava.util.Vector;9
10 importorg.slf4j.Logger;11 importorg.slf4j.LoggerFactory;12
13 importcom.jcraft.jsch.Channel;14 importcom.jcraft.jsch.ChannelSftp;15 importcom.jcraft.jsch.ChannelSftp.LsEntry;16 importcom.jcraft.jsch.JSch;17 importcom.jcraft.jsch.JSchException;18 importcom.jcraft.jsch.Session;19 importcom.jcraft.jsch.SftpException;20 importcom.opensymphony.xwork2.util.finder.ClassFinder.Info;21
22 public classFTPUtil {23 private static final Logger LOG = LoggerFactory.getLogger(FTPUtil.class);24
25 /**
26 * 参考实例27 *28 *@paramargs29 */
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 publicChannel 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 returnchannel;52 }53
54 public Session getSession(String host, intport, String username,55 finalString password) {56 Session session = null;57 try{58 JSch jsch = newJSch();59 jsch.getSession(username, host, port);60 session =jsch.getSession(username, host, port);61 session.setPassword(password);62 Properties sshConfig = newProperties();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 returnsession;71 }72
73 /**
74 * 创建文件夹75 *76 *@paramsftp77 *@paramdir78 * 文件夹名称79 */
80 public voidmkdir(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 *@paramsftp92 *@paramdir93 * 上传目录94 *@paramfile95 * 上传文件96 *@return
97 */
98 publicString 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(newFileInputStream(file), file.getName());105 result = "上传成功!";106 } else{107 result = "文件为空!不能上传!";108 }109 } catch(Exception e) {110 LOG.info("上传失败!", e);111 result = "上传失败!";112 }113 returnresult;114 }115
116 /**
117 * 下载文件118 *119 *@paramdirectory120 * 下载目录121 *@paramdownloadFile122 * 下载的文件123 *@paramsaveFile124 * 存在本地的路径125 *@paramsftp126 */
127 publicString 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 returnresult;140 }141
142 /**
143 * 删除文件144 *145 *@paramdirectory146 * 要删除文件所在目录147 *@paramdeleteFile148 * 要删除的文件149 *@paramsftp150 */
151 publicString 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 returnresult;162 }163
164 private voidcloseChannel(Channel channel) {165 if (channel != null) {166 if(channel.isConnected()) {167 channel.disconnect();168 }169 }170 }171
172 private voidcloseSession(Session session) {173 if (session != null) {174 if(session.isConnected()) {175 session.disconnect();176 }177 }178 }179
180 public voidcloseAll(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 }