JAVA SFTP文件上传、下载及批量下载实例

本篇文章主要介绍了JAVA SFTP文件上传、下载及批量下载实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

1.jsch官方API查看地址(附件为需要的jar)

http://www.jcraft.com/jsch/

2.jsch简介

JSch(Java Secure Channel)是一个SSH2的纯Java实现。它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功能到你自己的应用程序。

SFTP(Secure File Transfer Protocol)安全文件传送协议。可以为传输文件提供一种安全的加密方法。SFTP 为 SSH的一部份,是一种传输文件到服务器的安全方式,但是传输效率比普通的FTP要低。

3.api常用的方法:

  • put():      文件上传
  • get():      文件下载
  • cd():       进入指定目录
  • ls():       得到指定目录下的文件列表
  • rename():   重命名指定文件或目录
  • rm():       删除指定文件
  • mkdir():    创建目录
  • rmdir():    删除目录
  • put和get都有多个重载方法,自己看源代码

4.对常用方法的使用,封装成一个util类

 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Properties; import java.util.Vector; import org.apache.log4j.Logger; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpATTRS; import com.jcraft.jsch.SftpException; import com.jcraft.jsch.ChannelSftp.LsEntry; /** * sftp工具类 * * @author xxx * @date 2014-6-17 * @time 下午1:39:44 * @version 1.0 */ public class SFTPUtils { private static Logger log = Logger.getLogger(SFTPUtils.class.getName()); private String host;//服务器连接ip private String username;//用户名 private String password;//密码 private int port = 22;//端口号 private ChannelSftp sftp = null; private Session sshSession = null; public SFTPUtils(){} public SFTPUtils(String host, int port, String username, String password) { this.host = host; this.username = username; this.password = password; this.port = port; } public SFTPUtils(String host, String username, String password) { this.host = host; this.username = username; this.password = password; } /** * 通过SFTP连接服务器 */ public void connect() { try { JSch jsch = new JSch(); jsch.getSession(username, host, port); sshSession = jsch.getSession(username, host, port); if (log.isInfoEnabled()) { log.info("Session created."); } sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); if (log.isInfoEnabled()) { log.info("Session connected."); } Channel channel = sshSession.openChannel("sftp"); channel.connect(); if (log.isInfoEnabled()) { log.info("Opening Channel."); } sftp = (ChannelSftp) channel; if (log.isInfoEnabled()) { log.info("Connected to " + host + "."); } } catch (Exception e) { e.printStackTrace(); } } /** * 关闭连接 */ public void disconnect() { if (this.sftp != null) { if (this.sftp.isConnected()) { this.sftp.disconnect(); if (log.isInfoEnabled()) { log.info("sftp is closed already"); } } } if (this.sshSession != null) { if (this.sshSession.isConnected()) { this.sshSession.disconnect(); if (log.isInfoEnabled()) { log.info("sshSession is closed already"); } } } } /** * 批量下载文件 * @param remotPath:远程下载目录(以路径符号结束,可以为相对路径eg:/assess/sftp/jiesuan_2/2014/) * @param localPath:本地保存目录(以路径符号结束,D:\Duansha\sftp\) * @param fileFormat:下载文件格式(以特定字符开头,为空不做检验) * @param fileEndFormat:下载文件格式(文件格式) * @param del:下载后是否删除sftp文件 * @return */ public List batchDownLoadFile(String remotePath, String localPath, String fileFormat, String fileEndFormat, boolean del) { List filenames = new ArrayList(); try { // connect(); Vector v = listFiles(remotePath); // sftp.cd(remotePath); if (v.size() > 0) { System.out.println("本次处理文件个数不为零,开始下载...fileSize=" + v.size()); Iterator it = v.iterator(); while (it.hasNext()) { LsEntry entry = (LsEntry) it.next(); String filename = entry.getFilename(); SftpATTRS attrs = entry.getAttrs(); if (!attrs.isDir()) { boolean flag = false; String localFileName = localPath + filename; fileFormat = fileFormat == null ? "" : fileFormat .trim(); fileEndFormat = fileEndFormat == null ? "" : fileEndFormat.trim(); // 三种情况 if (fileFormat.length() > 0 && fileEndFormat.length() > 0) { if (filename.startsWith(fileFormat) && filename.endsWith(fileEndFormat)) { flag = downloadFile(remotePath, filename,localPath, filename); if (flag) { filenames.add(localFileName); if (flag && del) { deleteSFTP(remotePath, filename); } } } } else if (fileFormat.length() > 0 && "".equals(fileEndFormat)) { if (filename.startsWith(fileFormat)) { flag = downloadFile(remotePath, filename, localPath, filename); if (flag) { filenames.add(localFileName); if (flag && del) { deleteSFTP(remotePath, filename); } } } } else if (fileEndFormat.length() > 0 && "".equals(fileFormat)) { if (filename.endsWith(fileEndFormat)) { flag = downloadFile(remotePath, filename,localPath, filename); if (flag) { filenames.add(localFileName); if (flag && del) { deleteSFTP(remotePath, filename); } } } } else { flag = downloadFile(remotePath, filename,localPath, filename); if (flag) { filenames.add(localFileName); if (flag && del) { deleteSFTP(remotePath, filename); } } } } } } if (log.isInfoEnabled()) { log.info("download file is success:remotePath=" + remotePath + "and localPath=" + localPath + ",file size is" + v.size()); } } catch (SftpException e) { e.printStackTrace(); } finally { // this.disconnect(); } return filenames; } /** * 下载单个文件 * @param remotPath:远程下载目录(以路径符号结束) * @param remoteFileName:下载文件名 * @param localPath:本地保存目录(以路径符号结束) * @param localFileName:保存文件名 * @return */ public boolean downloadFile(String remotePath, String remoteFileName,String localPath, String localFileName) { FileOutputStream fieloutput = null; try { // sftp.cd(remotePath); File file = new File(localPath + localFileName); // mkdirs(localPath + localFileName); fieloutput = new FileOutputStream(file); sftp.get(remotePath + remoteFileName, fieloutput); if (log.isInfoEnabled()) { log.info("===DownloadFile:" + remoteFileName + " success from sftp."); } return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } finally { if (null != fieloutput) { try { fieloutput.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; } /** * 上传单个文件 * @param remotePath:远程保存目录 * @param remoteFileName:保存文件名 * @param localPath:本地上传目录(以路径符号结束) * @param localFileName:上传的文件名 * @return */ public boolean uploadFile(String remotePath, String remoteFileName,String localPath, String localFileName) { FileInputStream in = null; try { createDir(remotePath); File file = new File(localPath + localFileName); in = new FileInputStream(file); sftp.put(in, remoteFileName); return true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SftpException e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } return false; } /** * 批量上传文件 * @param remotePath:远程保存目录 * @param localPath:本地上传目录(以路径符号结束) * @param del:上传后是否删除本地文件 * @return */ public boolean bacthUploadFile(String remotePath, String localPath, boolean del) { try { connect(); File file = new File(localPath); File[] files = file.listFiles(); for (int i = 0; i  filePathList = new ArrayList(); try { sftp = new SFTPUtils("10.163.201.115", "tdcp", "tdcp"); sftp.connect(); // 下载 sftp.batchDownLoadFile(sftpPath, localPath, "ASSESS", ".txt", true); } catch (Exception e) { e.printStackTrace(); } finally { sftp.disconnect(); } } } 

5.需要的时间辅助类,顺带记下,下次可以直接拿来用

 /** * 时间处理工具类(简单的) * @author Aaron * @date 2014-6-17 * @time 下午1:39:44 * @version 1.0 */ public class DateUtil { /** * 默认时间字符串的格式 */ public static final String DEFAULT_FORMAT_STR = "yyyyMMddHHmmss"; public static final String DATE_FORMAT_STR = "yyyyMMdd"; /** * 获取系统时间的昨天 * @return */ public static String getSysTime(){ Calendar ca = Calendar.getInstance(); ca.set(Calendar.DATE, ca.get(Calendar.DATE)-1); Date d = ca.getTime(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String a = sdf.format(d); return a; } /** * 获取当前时间 * @param date * @return */ public static String getCurrentDate(String formatStr) { if (null == formatStr) { formatStr=DEFAULT_FORMAT_STR; } return date2String(new Date(), formatStr); } /** * 返回年月日 * @return yyyyMMdd */ public static String getTodayChar8(String dateFormat){ return DateFormatUtils.format(new Date(), dateFormat); } /** * 将Date日期转换为String * @param date * @param formatStr * @return */ public static String date2String(Date date, String formatStr) { if (null == date || null == formatStr) { return ""; } SimpleDateFormat df = new SimpleDateFormat(formatStr); return df.format(date); } } 

以上就是JAVA SFTP文件上传、下载及批量下载实例的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java