From cd849db3c3b5f8aee2737ab36f7b248df5febca9 Mon Sep 17 00:00:00 2001 From: lixiangrong Date: Tue, 19 Apr 2016 18:21:33 +0800 Subject: [PATCH] =?UTF-8?q?SFTP=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 + src/main/java/osc/git/eh3/sftp/SFTPUtil.java | 131 ++++++++++++++++++ .../java/osc/git/eh3/sftp/sftp.properties | 4 + .../eh3/test/OperateKPIBudgetRedisData.java | 2 +- 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/main/java/osc/git/eh3/sftp/SFTPUtil.java create mode 100644 src/main/java/osc/git/eh3/sftp/sftp.properties diff --git a/pom.xml b/pom.xml index 9508467..344bcc3 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,11 @@ + + com.jcraft + jsch + 0.1.53 + org.bouncycastle bcprov-jdk16 diff --git a/src/main/java/osc/git/eh3/sftp/SFTPUtil.java b/src/main/java/osc/git/eh3/sftp/SFTPUtil.java new file mode 100644 index 0000000..b1c3c3c --- /dev/null +++ b/src/main/java/osc/git/eh3/sftp/SFTPUtil.java @@ -0,0 +1,131 @@ +package osc.git.eh3.sftp; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Properties; + +import com.jcraft.jsch.Channel; +import com.jcraft.jsch.ChannelSftp; +import com.jcraft.jsch.JSch; +import com.jcraft.jsch.Session; + +/** + * SFTP工具类 + * @author lixiangrong + * + */ +public class SFTPUtil { + private static String ip; + private static String user; + private static String psw; + private static int port; + + private static Session session = null; + private static Channel channel = null; + + static { + try { + InputStream propFile = SFTPUtil.class.getResourceAsStream("sftp.properties"); + if (propFile != null) { + Properties p = new Properties(); + p.load(propFile); + ip = p.getProperty("sftp.ip"); + user = p.getProperty("sftp.user"); + psw = p.getProperty("sftp.psw"); + String portStr = p.getProperty("sftp.port"); + port = (portStr != null ? Integer.parseInt(portStr) : -1); + propFile.close(); + propFile = null; + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static ChannelSftp getSFTP() throws Exception { + ChannelSftp sftp = null; + JSch jsch = new JSch(); + if (port <= 0) { + // 连接服务器,采用默认端口 + session = jsch.getSession(user, ip); + } else { + // 采用指定的端口连接服务器 + session = jsch.getSession(user, ip, port); + } + + // 如果服务器连接不上,则抛出异常 + if (session == null) { + throw new Exception("session is null"); + } + + // 设置登陆主机的密码 + session.setPassword(psw);// 设置密码 + // 设置第一次登陆的时候提示,可选值:(ask | yes | no) + session.setConfig("StrictHostKeyChecking", "no"); + // 设置登陆超时时间 + session.connect(30000); + + try { + // 创建sftp通信通道 + channel = (Channel) session.openChannel("sftp"); + channel.connect(1000); + sftp = (ChannelSftp) channel; + + } catch (Exception e) { + e.printStackTrace(); + } + return sftp; + } + + private static void closeSFTP() { + if (session != null && session.isConnected()) { + session.disconnect(); + } + if (channel != null && channel.isConnected()) { + channel.disconnect(); + } + } + + /** + * SFTP上传文件 + * @param desPath ftp服务器目录 + * @param desFileName 上传后的文件名 + * @param resFile 要上传的文件 + * @throws Exception + */ + public static void putFile(String desPath, String desFileName, File resFile) throws Exception { + try { + ChannelSftp sftp = getSFTP(); + + // 进入服务器指定的文件夹 + sftp.cd(desPath); + + OutputStream outstream = sftp.put(desFileName); + InputStream instream = new FileInputStream(resFile); + + byte b[] = new byte[1024]; + int n; + while ((n = instream.read(b)) != -1) { + outstream.write(b, 0, n); + } + outstream.flush(); + outstream.close(); + instream.close(); + + closeSFTP(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + try { + SFTPUtil.putFile("/data", "212321.txt", new File("D:/1.txt")); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/osc/git/eh3/sftp/sftp.properties b/src/main/java/osc/git/eh3/sftp/sftp.properties new file mode 100644 index 0000000..2bde1f6 --- /dev/null +++ b/src/main/java/osc/git/eh3/sftp/sftp.properties @@ -0,0 +1,4 @@ +sftp.ip=192.168.3.166 +sftp.user=root +sftp.psw=PowerXene123 +sftp.port=22 \ No newline at end of file diff --git a/src/main/java/osc/git/eh3/test/OperateKPIBudgetRedisData.java b/src/main/java/osc/git/eh3/test/OperateKPIBudgetRedisData.java index 17375cf..bb49210 100644 --- a/src/main/java/osc/git/eh3/test/OperateKPIBudgetRedisData.java +++ b/src/main/java/osc/git/eh3/test/OperateKPIBudgetRedisData.java @@ -9,7 +9,7 @@ public class OperateKPIBudgetRedisData { public static void main(String[] args) throws IOException { - showSom("022ea1a5-3f21-40dd-9c24-c0edfa82bfda"); + showSom("09e5f11c-5c6d-44e4-a599-1d2981d283c3"); // delete("c46b8885-4c1d-48f9-befb-3ba3a0f33d4a"); }