1 package com.mall.business.active;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.OutputStream;
9 import java.text.SimpleDateFormat;
10 import java.util.Date;
11 import java.util.Properties;
12 import java.util.Vector;
13
14 import org.apache.log4j.LogManager;
15 import org.apache.log4j.Logger;
16
17 import com.jcraft.jsch.Channel;
18 import com.jcraft.jsch.ChannelSftp;
19 import com.jcraft.jsch.JSch;
20 import com.jcraft.jsch.JSchException;
21 import com.jcraft.jsch.Session;
22 import com.jcraft.jsch.SftpException;
23 import com.mall.timer.TimerManager;
24
25 public class SshSftp {
26 /**
27 * 利用JSch包实现SFTP下载、上传文件
28 * @param ip
29 * 主机IP
30 * @param user
31 * 主机登陆用户名
32 * @param psw
33 * 主机登陆密码
34 * @param port
35 * 主机ssh2登陆端口
36 * @param url
37 * 保存位置
38 * @param imgUrl
39 * 保存位置(域名)
40 * @param localpath
41 * 本地文件位置
42 * @param newname
43 * 新文件名
44 */
45 private static Logger log=LogManager.getLogger(SshSftp.class);
46
47 public static String sshSftp(String ip, String user, String psw, int port,
48 String url, String imgUrl, String localpath, String newname) {
49 Session session = null;
50 Channel channel = null;
51 ChannelSftp sftp = null;
52
53 JSch jsch = new JSch();
54 try {
55 if (port <= 0) {
56 // 连接服务器,采用默认端口-1
57 session = jsch.getSession(user, ip);
58 } else {
59 // 采用指定的端口连接服务器
60 session = jsch.getSession(user, ip, port);
61 }
62
63 // 如果服务器连接不上,则抛出异常
64 if (session == null) {
65 log.info("未连接服务器:session is null");
66 return null;
67 //throw new Exception("session is null");
68 }
69
70 // 设置登陆主机的密码
71 session.setPassword(psw);// 设置密码
72 // 设置第一次登陆的时候提示,可选值:(ask | yes | no)
73 session.setConfig("StrictHostKeyChecking", "no");
74 // 设置登陆超时时间
75 session.connect(30000);
76
77 // 创建sftp通信通道
78 channel = (Channel) session.openChannel("sftp");
79 channel.connect(10000);
80 sftp = (ChannelSftp) channel;
81 }catch(Exception e){
82 log.info("未连接服务器"+e.getMessage());
83 return null;
84 }
85
86 try {
87 sftp.ls(url); // 首先查看下目录,如果不存在,系统会被错,捕获这个错,生成新的目录。
88 } catch (Exception e) {
89 try {
90 sftp.mkdir(url);
91 } catch (SftpException e1) {
92 log.info("创建文件夹异常,msg:"+e1.getMessage());
93 return null;
94 }
95 }
96
97 Date date = new Date();
98 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
99 try {
100 sftp.ls(url+"/"+format.format(date)); // 首先查看下目录,如果不存在,系统会被错,捕获这个错,生成新的目录。
101 } catch (Exception e) {
102 try {
103 sftp.mkdir(url+"/"+format.format(date));
104 } catch (SftpException e1) {
105 log.info("创建文件夹异常,msg:"+e1.getMessage());
106 return null;
107 }
108 }
109
110 try{
111 // 进入服务器指定的文件夹
112 sftp.cd(url+"/"+format.format(date));
113
114 // 列出服务器指定的文件列表
115 /*
116 * Vector v = sftp.ls("*.jsp"); for(int i=0;i<v.size();i++){
117 * System.out.println(v.get(i)); }
118 */
119
120 // 以下代码实现从本地上传一个文件到服务器,如果要实现下载,对换以下流就可以了
121 OutputStream outstream = sftp.put(newname);
122 // InputStream instream = new FileInputStream(new
123 // File("H:/downloadFile/1223.jpg"));
124 InputStream instream = new FileInputStream(new File(localpath));
125
126 byte b[] = new byte[1024];
127 int n;
128 while ((n = instream.read(b)) != -1) {
129 outstream.write(b, 0, n);
130 }
131
132 outstream.flush();
133 outstream.close();
134 instream.close();
135
136 return imgUrl+format.format(date)+"/"+newname;
137 }catch (Exception e) {
138 e.printStackTrace();
139 return null;
140 } finally {
141 session.disconnect();
142 channel.disconnect();
143 }
144 }
145
146 public static void main(String[] args) {
147 String ip = "***";
148 String user = "**";
149 String psw = "**";
150 int port = 22;
151 String url = "**";
152 String imgUrl = "***";
153 String localpath = "H:/downloadFile/接口测试文档.doc";
154 String newname = "aaa.doc";
155
156 SshSftp sftp = new SshSftp();
157 sftp.sshSftp(ip,user,psw,port,url,imgUrl,localpath,newname);
158
159 }
160
161 }