Java使用sftp协议进行xml的上传下载解析
1 package com.jf.ftp; 2 3 import java.io.BufferedReader; 4 import java.io.ByteArrayInputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileOutputStream; 8 import java.io.IOException; 9 import java.io.InputStream; 10 import java.io.InputStreamReader; 11 import java.util.Properties; 12 import java.util.Vector; 13 14 import net.sf.json.JSON; 15 import net.sf.json.JSONObject; 16 import net.sf.json.xml.XMLSerializer; 17 18 import com.jcraft.jsch.Channel; 19 import com.jcraft.jsch.ChannelSftp; 20 import com.jcraft.jsch.JSch; 21 import com.jcraft.jsch.Session; 22 import com.jcraft.jsch.SftpException; 23 import com.mvc.xml.Body; 24 import com.mvc.xml.Header; 25 26 27 public class SftpUtil { 28 private SftpUtil(){} 29 30 static Session sshSession = null; 31 static Channel channel = null; 32 static ChannelSftp sftp = null; 33 34 /** 35 * 连接sftp服务器 36 * @param host 主机 37 * @param port 端口 38 * @param userName 用户名 39 * @param passWord 密码 40 */ 41 static{ 42 String host = ""; 43 int port = 22; 44 String userName = ""; 45 String passWord = ""; 46 try { 47 JSch jsch = new JSch(); 48 jsch.getSession(userName, host, port); 49 sshSession = jsch.getSession(userName, host, port); 50 System.out.println("Session created."); 51 sshSession.setPassword(passWord); 52 Properties sshConfig = new Properties(); 53 sshConfig.put("StrictHostKeyChecking", "no"); 54 sshSession.setConfig(sshConfig); 55 sshSession.connect(); 56 System.out.println("Session connected."); 57 channel = sshSession.openChannel("sftp"); 58 channel.connect(); 59 System.out.println("Opening Channel."); 60 sftp = (ChannelSftp) channel; 61 System.out.println("Connected to " + host + "."); 62 } catch (Exception e) { 63 64 } 65 } 66 67 /** 68 * 上传文件 69 * @param directory 上传的目录 70 * @param uploadFile 要上传的文件 71 */ 72 public static void upload(String directory, String uploadFile) { 73 try { 74 sftp.cd(directory); 75 File file = new File(uploadFile); 76 sftp.put(new FileInputStream(file), file.getName()); 77 System.out.println("上传完成"); 78 } catch (Exception e) { 79 e.printStackTrace(); 80 } 81 } 82 83 /** 84 * 下载文件 85 * @param directory 下载目录 86 * @param downloadFile 下载的文件 87 * @param saveFile 存在本地的路径 88 */ 89 public static void download(String directory, String downloadFile, String saveFile) { 90 try { 91 sftp.cd(directory); 92 File file = new File(saveFile); 93 sftp.get(downloadFile, new FileOutputStream(file)); 94 System.out.println("下载完成"); 95 } catch (Exception e) { 96 e.printStackTrace(); 97 } 98 } 99 100 /** 101 * 删除文件 102 * @param directory 要删除文件所在目录 103 * @param deleteFile 要删除的文件 104 */ 105 public static void delete(String directory, String deleteFile) { 106 try { 107 sftp.cd(directory); 108 sftp.rm(deleteFile); 109 } catch (Exception e) { 110 e.printStackTrace(); 111 } 112 } 113 114 /** 115 * 列出目录下的文件 116 * @param directory 要列出的目录 117 * @return 118 * @throws SftpException 119 */ 120 @SuppressWarnings("rawtypes") 121 public static Vector listFiles(String directory)throws SftpException { 122 return sftp.ls(directory); 123 } 124 125 /**关闭链接*/ 126 public static void close(){ 127 if (sftp != null) { 128 sftp.quit(); 129 } 130 if (channel != null) { 131 channel.disconnect(); 132 } 133 if (sshSession != null) { 134 sshSession.disconnect(); 135 } 136 } 137 138 /** 139 * 上传流 140 * @param sInputString 要上传的字符串 141 * @param directory 上传的目录 142 * @param dst 目标文件名 143 */ 144 public static void upload(String sInputString,String directory, String dst) { 145 try { 146 sftp.cd(directory); 147 InputStream is = getStringStream(sInputString); 148 sftp.put(is, dst); 149 System.out.println("上传完成"); 150 } catch (Exception e) { 151 e.printStackTrace(); 152 } 153 } 154 155 /** 156 * 下载流 157 * @param directory 下载目录 158 * @param downloadFile 下载的文件 159 */ 160 public static String download(String directory, String downloadFile) { 161 try { 162 sftp.cd(directory); 163 InputStream is = sftp.get(downloadFile); 164 System.out.println("下载完成"); 165 return changeStreamToString(is); 166 } catch (Exception e) { 167 e.printStackTrace(); 168 return null; 169 } 170 } 171 172 /**将字符串转化为InputStream流*/ 173 public static InputStream getStringStream(String sInputString){ 174 ByteArrayInputStream tInputStringStream = null; 175 try{ 176 tInputStringStream = new ByteArrayInputStream(sInputString.getBytes()); 177 return tInputStringStream; 178 }catch (Exception ex){ 179 ex.printStackTrace(); 180 }finally{ 181 try { 182 tInputStringStream.close(); 183 } catch (IOException e) { 184 e.printStackTrace(); 185 } 186 } 187 return null; 188 } 189 190 /**将InputStream流转为String*/ 191 public static String changeStreamToString(InputStream is) { 192 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 193 StringBuilder sb = new StringBuilder(); 194 String line = null; 195 try { 196 while ((line = reader.readLine()) != null) { 197 sb.append(line + "\n"); 198 } 199 } catch (IOException e) { 200 e.printStackTrace(); 201 } finally { 202 try { 203 is.close(); 204 } catch (IOException e) { 205 e.printStackTrace(); 206 } 207 } 208 return sb.toString(); 209 } 210 211 public static void main(String[] args) { 212 /**上传文件*/ 213 //SftpUtil.upload(directory, uploadFile); 214 /**下载文件*/ 215 //SftpUtil.download("/REQUESTOUT/OUT", "TMS_xxx.xml", "D:/tms/TMS_xxx.xml"); 216 /**上传流*/ 217 SftpUtil.upload(message().toString(), "/REQUESTIN/IN", "CREDIT_INPUT_cc.xml"); 218 SftpUtil.close(); 219 /**下载流并转为Object*/ 220 //downLoadAndChangeToObject(); 221 } 222 223 private static void downLoadAndChangeToObject() { 224 String str = SftpUtil.download("/REQUESTOUT/OUT", "TMS_xxx.xml"); 225 SftpUtil.close(); 226 XMLSerializer xmlSerializer=new XMLSerializer(); 227 JSON json=xmlSerializer.read(str); 228 229 //截取掉[],全部转为小写,转化为JSONObject 230 JSONObject jsonObject=JSONObject.fromObject(json.toString().toLowerCase().replace("[", "").replace("]", "")); 231 //实例化 232 JSONObject j1=JSONObject.fromObject(jsonObject.get("header")); 233 @SuppressWarnings("unused") 234 Header header = (Header)JSONObject.toBean(j1,Header.class); 235 JSONObject j2=JSONObject.fromObject(jsonObject.get("body")); 236 @SuppressWarnings("unused") 237 Body body = (Body)JSONObject.toBean(j2,Body.class); 238 } 239 240 public static StringBuffer message(){ 241 StringBuffer sb = new StringBuffer(); 242 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); 243 sb.append("<OSCDH>\n"); 244 sb.append(" <Header>\n"); 245 sb.append(" <MessageID>LINKMANINPUT</MessageID>\n"); 246 sb.append(" <FunctionCode>xx</FunctionCode>\n"); 247 sb.append(" <MessageType>xxx</MessageType>\n"); 248 sb.append(" <SenderID>CDH</SenderID>\n"); 249 sb.append(" <ReceiverID>TMS</ReceiverID>\n"); 250 sb.append(" <SendTime>xxx</SendTime>\n"); 251 sb.append(" </Header>\n"); 252 sb.append(" <Body>\n"); 253 sb.append(" <ST_CDH_LINKMAN_INPUT>\n"); 254 sb.append(" <REQUEST_DATE>2015-11-12T13:15:22</REQUEST_DATE>\n"); 255 sb.append(" <ORIG_SYSTEM>FMS</ORIG_SYSTEM>\n"); 256 sb.append(" <ORIG_SYSTEM_REFERENCE>xx</ORIG_SYSTEM_REFERENCE>\n"); 257 sb.append(" <ORIG_SYSTEM_REF_NAME>有限公司</ORIG_SYSTEM_REF_NAME>\n"); 258 sb.append(" <FIN_ORG_ID>785</FIN_ORG_ID>\n"); 259 sb.append(" <FLAG>A</FLAG>\n"); 260 sb.append(" <LINKMAN_ID>xxx</LINKMAN_ID>\n"); 261 sb.append(" <CV_TYPE>C</CV_TYPE>\n"); 262 sb.append(" <LINKMAN>张三</LINKMAN>\n"); 263 sb.append(" <MOBILE>13811112222</MOBILE>\n"); 264 sb.append(" <DEPARTMENT>销售一部</DEPARTMENT>\n"); 265 sb.append(" <POSITION>销售主管</POSITION>\n"); 266 sb.append(" <TEL>021-12345678</TEL>\n"); 267 sb.append(" <EMAIL>zhangsan@xx.com</EMAIL>\n"); 268 sb.append(" <SOURCE_ID>xxx</SOURCE_ID>\n"); 269 sb.append(" <ATTRIBUTE1/>\n"); 270 sb.append(" <ATTRIBUTE2/>\n"); 271 sb.append(" <ATTRIBUTE3/>\n"); 272 sb.append(" <ATTRIBUTE4/>\n"); 273 sb.append(" <ATTRIBUTE5/>\n"); 274 sb.append(" <ATTRIBUTE6/>\n"); 275 sb.append(" <ATTRIBUTE7/>\n"); 276 sb.append(" <ATTRIBUTE8/>\n"); 277 sb.append(" <ATTRIBUTE9/>\n"); 278 sb.append(" <ATTRIBUTE10/>\n"); 279 sb.append(" </ST_CDH_LINKMAN_INPUT>\n"); 280 sb.append(" </Body>\n"); 281 sb.append("</OSCDH>"); 282 return sb; 283 } 284 }