package com.files.upload;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
/**
* SFTP channel对象,即ChannelSftp的实例对象,
* 在应用程序中就可以使用该对象来调用SFTP的各种操作方法。
* @author Frank.dai
*
*/
public class SFTPChannel {
Session session = null;
Channel channel = null;
private static final Logger LOG = Logger.getLogger(SFTPChannel.class.getName());
public ChannelSftp getChannel(Map sftpDetails, int timeout) throws JSchException {
String ftpHost = sftpDetails.get(SFTPConstants.SFTP_REQ_HOST);
String port = sftpDetails.get(SFTPConstants.SFTP_REQ_PORT);
String ftpUserName = sftpDetails.get(SFTPConstants.SFTP_REQ_USERNAME);
String ftpPassword = sftpDetails.get(SFTPConstants.SFTP_REQ_PASSWORD);
int ftpPort = SFTPConstants.SFTP_DEFAULT_PORT;
if (port != null && !port.equals("")) {
ftpPort = Integer.valueOf(port);
}
JSch jsch = new JSch(); // 创建JSch对象
session = jsch.getSession(ftpUserName, ftpHost, ftpPort); // 根据用户名,主机ip,端口获取一个Session对象
//LOG.debug("Session created.");
if (ftpPassword != null) {
session.setPassword(ftpPassword); // 设置密码
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config); // 为Session对象设置properties
session.setTimeout(timeout); // 设置timeout时间
session.connect(); // 通过Session建立链接
// LOG.debug("Session connected.");
// LOG.debug("Opening Channel.");
channel = session.openChannel("sftp"); // 打开SFTP通道
channel.connect(); // 建立SFTP通道的连接
// LOG.debug("Connected successfully to ftpHost = " + ftpHost + ",as ftpUserName = " + ftpUserName
// + ", returning: " + channel);
return (ChannelSftp) channel;
}
public void closeChannel() throws Exception {
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
二:
package com.files.upload;
/**
* 定义常量
* @author Frank.dai
*
*/
public class SFTPConstants {
public static final String SFTP_REQ_HOST = "host";
public static final String SFTP_REQ_PORT = "port";
public static final String SFTP_REQ_USERNAME = "username";
public static final String SFTP_REQ_PASSWORD = "password";
public static final int SFTP_DEFAULT_PORT = 22;
public static final String SFTP_REQ_LOC = "location";
}
三:
package com.files.upload; import java.io.File; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashMap; import java.util.Map; import java.util.Properties; import com.jcraft.jsch.ChannelSftp; public class SFTPTest { public SFTPChannel getSFTPChannel() { return new SFTPChannel(); } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Properties properties = new Properties(); InputStream in = Thread.currentThread().getClass().getResourceAsStream("/database.properties"); properties.load(in); Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, Integer.valueOf(properties.getProperty("indexday"))); String yesteDay = new SimpleDateFormat("yyyyMMdd").format(calendar.getTime()); SFTPTest test = new SFTPTest(); Map sftpDetails = new HashMap(); // 设置主机ip,端口,用户名,密码 sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, "10.1.234.23"); sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME, "dwaml"); sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD, "1234qwer"); sftpDetails.put(SFTPConstants.SFTP_REQ_PORT, "22"); SFTPChannel channel = test.getSFTPChannel(); ChannelSftp chSftp = channel.getChannel(sftpDetails, 60000); String pathINV = properties.getProperty("pathINV"); String filePath = pathINV + yesteDay; String a = "/home/adb/JKLIFE/INVEST/"; String dst = a+yesteDay; // 目标文件名 int countDirectory = 0;//文件个数 File folder = new File(filePath); // 自定义文件路径 if(folder.exists() && folder.isDirectory()){ File files[] = folder.listFiles(); for(File fileIndex : files){ countDirectory++; String src = fileIndex.toString(); chSftp.put(src, dst, ChannelSftp.OVERWRITE); // 代码段2 System.out.println(countDirectory); System.out.println("上传成功"); } }else{ System.out.println("文件不存在"); } chSftp.quit(); channel.closeChannel(); /** * 代码段1 OutputStream out = chSftp.put(dst, ChannelSftp.OVERWRITE); // 使用OVERWRITE模式 byte[] buff = new byte[1024 * 256]; // 设定每次传输的数据块大小为256KB int read; if (out != null) { System.out.println("Start to read input stream"); InputStream is = new FileInputStream(src); do { read = is.read(buff, 0, buff.length); if (read > 0) { out.write(buff, 0, read); } out.flush(); } while (read >= 0); System.out.println("input stream read done."); } **/ /* chSftp.put(src, dst, ChannelSftp.OVERWRITE); // 代码段2 // chSftp.put(new FileInputStream(src), dst, ChannelSftp.OVERWRITE); // 代码段3 // chSftp.put(new FileInputStream(src), dst, new FileProgressMonitor(fileSize), ChannelSftp.OVERWRITE); // 代码段3 System.out.println("上传成功"); chSftp.quit(); channel.closeChannel();*/ } }