1bb525556b3f3a83ef69ae47a0bad5212fab0d4b.svn-base
9.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package com.thinkgem.jeesite.modules.reg.web.updata;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SftpFileLoad
{
protected static Logger logger = LoggerFactory.getLogger(SftpFileLoad.class);
public String sshSftp(String ip, String user, String psw, int port, String filepath, String filename)
throws Exception
{
logger.info("********开始调用上报数据方法**********");
String tempReturnValue = "";
logger.info("开始用户名密码方式登陆");
Session session = 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);
session.setConfig("StrictHostKeyChecking", "no");
session.connect(30000);
logger.info("开始连接");
tempReturnValue = sftp(session, filepath, filename);
logger.info("******返回结果" + tempReturnValue + "*****");
logger.info("********结束调用上报数据方法**********");
return tempReturnValue;
}
private String sftp(Session session, String unloadfilepath, String uploadFileName) throws Exception
{
Channel channel = null;
String tempReturnValue = "";
try
{
channel = session.openChannel("sftp");
channel.connect(30000);
ChannelSftp sftp = (ChannelSftp)channel;
sftp.cd("/home/610700/gtmap_client/client_source");
OutputStream outstream = sftp.put(uploadFileName);
InputStream instream = new FileInputStream(new File(unloadfilepath));
byte[] b = new byte[1024];
int n;
while ((n = instream.read(b)) != -1) {
outstream.write(b, 0, n);
}
outstream.flush();
outstream.close();
instream.close();
tempReturnValue = sftpDownloadFile(session, unloadfilepath, uploadFileName);
logger.info(tempReturnValue);
} catch (Exception e) {
logger.info("********出错****", e);
e.printStackTrace();
} finally {
session.disconnect();
channel.disconnect();
}
return tempReturnValue;
}
public String sftpDownloadFile(String ip, String user, String psw, int port, String filepath, String filename)
throws Exception
{
logger.info("********开始调用上报数据方法**********");
String tempReturnValue = "";
logger.info("开始用户名密码方式登陆");
Session session = 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);
session.setConfig("StrictHostKeyChecking", "no");
session.connect(30000);
logger.info("开始连接");
tempReturnValue = sftpDownloadFile(session, filepath, filename);
logger.info("******返回结果" + tempReturnValue + "*****");
logger.info("********结束调用上报数据方法**********");
return tempReturnValue;
}
private String sftpDownloadFile(Session session, String unloadfilepath, String uploadFileName) throws Exception {
logger.info("********开始报文下载方法**********");
Channel channel = null;
String tempReturnValue = "";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
channel = session.openChannel("sftp");
channel.connect(50000);
ChannelSftp sftp = (ChannelSftp)channel;
sftp.cd("/home/610700/gtmap_client/client_response");
String tempname = "";
tempname = uploadFileName.replace("Biz", "Rep");
Thread.sleep(1000L);
for (int i = 0; i < 15; ++i) {
try {
logger.info("********下载查询报文开始,时间为:" + df.format(new Date()));
Vector content = sftp.ls(tempname);
logger.info("********找到文件名为:" + tempname + ",的文件了,找到时间为:" + df.format(new Date()));
i = 16;
}
catch (Exception e) {
logger.info("********下载查询报文结束,时间为:" + df.format(new Date()));
logger.info("********下载循环等待出错,最终等待时间为:****" + (200 * (i + 1)) + ",县区名称:" + tempname, e);
Thread.sleep(200 * (i + 1));
}
}
String areacode = uploadFileName.substring(3, 9);
OutputStream outstream = new FileOutputStream(new File("D:/Rep/" + areacode + "/" + tempname));
logger.info("********开始读取返回报文中的内容,时间为:" + df.format(new Date()));
InputStream instream = sftp.get(tempname);
byte[] b = new byte[1024];
int n;
while ((n = instream.read(b)) != -1) {
outstream.write(b, 0, n);
}
outstream.flush();
outstream.close();
instream.close();
tempReturnValue = parserXml("D:/Rep/" + areacode + "/" + tempname);
} catch (Exception e) {
logger.info("********下载出错时间为:" + df.format(new Date()), e);
e.printStackTrace();
logger.info(e.getMessage());
} finally {
session.disconnect();
channel.disconnect();
}
logger.info("********结束报文下载方法了,时间为:" + df.format(new Date()));
return tempReturnValue;
}
public String parserXml(String fileName) throws Exception
{
String tempReturnValue = "";
File inputXml = new File(fileName);
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(inputXml);
Element employees = document.getRootElement();
for (Iterator i = employees.elementIterator(); i.hasNext(); ) {
Element employee = (Element)i.next();
if (employee.getName() != "ResponseInfo")
continue;
tempReturnValue = employee.getText();
return tempReturnValue;
}
}
catch (DocumentException e)
{
logger.info(e.getMessage());
}
return tempReturnValue;
}
public String sshSftpdwoload(String ip, String user, String psw, int port, String filepath, String filename)
throws Exception
{
logger.info("********开始调用上报数据方法**********");
String tempReturnValue = "";
logger.info("开始用户名密码方式登陆");
Session session = 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);
session.setConfig("StrictHostKeyChecking", "no");
session.connect(30000);
logger.info("开始连接");
tempReturnValue = querysftpDownloadFile(session, filepath, filename);
logger.info("******返回结果" + tempReturnValue + "*****");
logger.info("********结束调用上报数据方法**********");
return tempReturnValue;
}
private String querysftpDownloadFile(Session session, String unloadfilepath, String uploadFileName)
throws Exception
{
logger.info("********开始报文下载方法**********");
Channel channel = null;
String tempReturnValue = "";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
channel = session.openChannel("sftp");
channel.connect(50000);
ChannelSftp sftp = (ChannelSftp)channel;
sftp.cd("/home/610700/gtmap_client/client_response");
String tempname = "";
tempname = uploadFileName.replace("Biz", "Rep");
Thread.sleep(1000L);
for (int i = 0; i < 15; ++i) {
try {
logger.info("********下载查询报文开始,时间为:" + df.format(new Date()));
Vector content = sftp.ls(tempname);
logger.info("********找到文件名为:" + tempname + ",的文件了,找到时间为:" + df.format(new Date()));
i = 16;
}
catch (Exception e) {
logger.info("********下载查询报文结束,时间为:" + df.format(new Date()));
logger.info("********下载循环等待出错,最终等待时间为:****" + (200 * (i + 1)) + ",县区名称:" + tempname, e);
Thread.sleep(200 * (i + 1));
}
}
String areacode = uploadFileName.substring(3, 9);
OutputStream outstream = new FileOutputStream(new File("D:/Rep/" + areacode + "/" + tempname));
logger.info("********开始读取返回报文中的内容,时间为:" + df.format(new Date()));
InputStream instream = sftp.get(tempname);
byte[] b = new byte[1024];
int n;
while ((n = instream.read(b)) != -1) {
outstream.write(b, 0, n);
}
outstream.flush();
outstream.close();
instream.close();
tempReturnValue = parserXml("D:/Rep/" + areacode + "/" + tempname);
} catch (Exception e) {
logger.info("********下载出错时间为:" + df.format(new Date()), e);
e.printStackTrace();
logger.info(e.getMessage());
} finally {
session.disconnect();
channel.disconnect();
}
logger.info("********结束报文下载方法了,时间为:" + df.format(new Date()));
return tempReturnValue;
}
}