代码如下 | 复制代码 |
/** * 将文件上传到服务器 * * @param filePath * 文件路径 * @param channelSftp * channelSftp对象 * @return */ publicstaticbooleanuploadFile(String filePath, ChannelSftp channelSftp) { OutputStream outstream =null; InputStream instream =null; booleansuccessFlag =false; try{ File isfile =newFile(filePath); if(isfile.isFile()) { outstream = channelSftp.put(isfile.getName()); File file =newFile(filePath); if(file.exists()) { instream =newFileInputStream(file); byteb[] =newbyte[1024]; intn; while((n = instream.read(b)) != -1) { outstream.write(b,0, n); } outstream.flush(); } successFlag =true; } }catch(Exception e) { e.printStackTrace(); }finally{ try{ if(instream !=null) { instream.close(); } if(outstream !=null) { outstream.close(); } }catch(IOException e) { e.printStackTrace(); } } returnsuccessFlag; } privatestaticSession initJschSession() throwsJSchException { intftpPort =0; String ftpHost =""; String port ="00";//sftp的端口号 String ftpUserName ="";//用户名 String ftpPassword ="";//链接的密码 String privateKey ="";// String passphrase =""; if(port !=null&& !port.equals("")) { ftpPort = Integer.valueOf(port); } JSch jsch =newJSch();// 创建JSch对象 if(StringUtils.isNotBlank(privateKey) && StringUtils.isNotBlank(passphrase)) { jsch.addIdentity(privateKey, passphrase); } if(StringUtils.isNotBlank(privateKey) && StringUtils.isBlank(passphrase)) { jsch.addIdentity(privateKey); } jsch.getSession(ftpUserName, ftpHost, ftpPort); Session session = jsch.getSession(ftpUserName, ftpHost, ftpPort);// 根据用户名,主机ip,端口获取一个Session对象 if(StringUtils.isNotBlank(ftpPassword)) { session.setPassword(ftpPassword);// 设置密码 } returnsession; } /** * 获取ChannelSftp链接 * * @param timeout * 超时时间 * @return 返回ChannelSftp对象 * @throws JSchException */ publicstaticChannelSftp getChannelSftp(Session session,inttimeout) throwsJSchException { Channel channel =null; Properties config =newProperties(); config.put("StrictHostKeyChecking","no"); session.setConfig(config);// 为Session对象设置properties session.setTimeout(timeout);// 设置timeout时间 session.connect();// 通过Session建立链接 channel = session.openChannel("sftp");// 打开SFTP通道 channel.connect();// 建立SFTP通道的连接 return(ChannelSftp) channel; } /** * 断开sftp链接 * * @param session * 会话 * @param channel * 通道 */ publicstaticvoidcloseConnection(Channel channel, Session session) { try{ if(session !=null) { session.disconnect();//关闭session链接 } if(channel !=null) { channel.disconnect();//断开连接 } }catch(Exception e) { e.printStackTrace(); } } |
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级