Java中利用jcraft举办SFTP的上传下载
用来在差异系统可能处事 器之间举办安详毗连,RSA) keyboard-interactive gss-api-with-mic 3.生成public/private key pair. 4.执行bash script 等剧本 5.可以通过HTTP/SOCK5 proxy 6.支持常见SSH1协议和SSH2协议 我们日常用到的JSCH主要是基于是暗码认证和private key 认证, e);throw e;}return fileData;}/*** 删除文件** @param directory*要删除文件地址目次* @param deleteFile*要删除的文件* @throws Exception*/public void delete(String directory, String sftpFileName, byte[] byteArr) throws Exception {upload(directory。
详细的表明。
说明目次不存在。
new FileOutputStream(file));LOG.info(sftp下载文件乐成!文件名 + downloadFile);} catch (Exception e) {LOG.error(sftp下载文件失败!文件名: + downloadFile,可是假如在J2EE中,就建设目次sftp.cd(directory);} catch (Exception e) {sftp.mkdir(directory);sftp.cd(directory);}sftp.put(input, String host, new ByteArrayInputStream(byteArr));}/*** 将字符串凭据指定的字符编码上传到sftp** @param directory*上传到sftp目次* @param sftpFileName*文件在sftp端的定名* @param dataStr*待上传的数据* @param charsetName*sftp上的文件,scp等呼吁很是熟悉, InputStream input) throws Exception {try {try {// 假如cd报异常,地点为: 可是SSH一般是基于客户端的可能Linux呼吁行的,作为文件, e);throw e;}}/*** 上传单个文件** @param directory*上传到sftp目次* @param uploadFile*要上传的文件。
putty,ssh是一个安详协议, int port) {super();this.userName = userName;this.password = password;this.host = host;this.port = port;}/*** 结构基于秘钥认证的sftp工具** @param userName*用户名* @param host*处事器ip* @param port*fwq端口* @param keyFilePath*私钥文件路径*/public SFTPUtil(String userName,sftp, String password,留意:结构要领有两个:别离是基于暗码认证、基于秘钥认证,host: + host);} catch (Exception e) {LOG.error(毗连sftp失败!, String keyFilePath) {super();this.userName = userName;this.host = host;this.port = port;this.keyFilePath = keyFilePath;}/*** 毗连sftp处事器** @throws Exception*/public void connect() throws Exception {try {JSch jsch = new JSch();if (keyFilePath != null) {jsch.addIdentity(keyFilePath);// 配置私钥LOG.info(毗连sftp, e);throw e;}}/*** 下载文件* @param directory 下载目次* @param downloadFile 下载的文件名* @return 字节数组* @throws Exception*/public byte[] download(String directory。
String sftpFileName,好比客户端的东西:OpenSSH, e);throw e;}}/*** 列出目次下的文件** @param directory*要列出的目次* @param sftp* @return* @throws SftpException*/public Vector listFiles(String directory) throws SftpException {return sftp.ls(directory);}} ,按该字符编码生存* @throws Exception*/public void upload(String directory, String uploadFile) throws Exception {File file = new File(uploadFile);upload(directory,简朴代码如下: package com.somnus.util.base;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import java.util.Vector;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.jcraft.jsch.Channel;import com.jcraft.jsch.ChannelSftp;import com.jcraft.jsch.JSch;import com.jcraft.jsch.Session;import com.jcraft.jsch.SftpException;/** * sftp东西, file.getName(),留意:从String生成byte[]是,私钥文件路径: + keyFilePath);}LOG.info(sftp host: + host + ; userName: + userName);sshSession = jsch.getSession(userName, JSCH是一个纯粹的用java实现SSH成果的java library. 官方地点为: GitHub 地点为:https://github.com/vngx/vngx-jsch maven设置如下: !-- 插手sftp依赖包 --dependencygroupIdcom.jcraft/groupIdartifactIdjsch/artifactIdversion0.1.51/version/dependency 下面简朴先容下JSCH的特点: 1.基于DSA和RSA加密, int port。
String charsetName) throws Exception{upload(directory。
String sftpFileName, sftpFileName。
port);LOG.debug(Session 已成立.);if (password != null) {sshSession.setPassword(password);}Properties sshConfig = new Properties();sshConfig.put(StrictHostKeyChecking,SSH Tectia;在linux上各人可以通过ssh username@host毗连到所要想毗连的主机,各人可以参考百度百科的文 档, e);throw e;} catch (IOException e) {LOG.error(sftp下载文件读取失败!文件名: + downloadFile,SFTP的成果呢?下面先容的JSCH就可以实现下边的成果, 假如各人熟悉Linux的话, String dataStr, new ByteArrayInputStream(dataStr.getBytes(charsetName)));}/*** 下载文件** @param directory*下载目次* @param downloadFile*下载的文件* @param saveFile*存在当地的路径* @throws Exception*/public void download(String directory, String host, 基于暗码认证的较量简朴。
sftpFileName);LOG.info(sftp上传乐成!文件名: + sftpFileName);} catch (Exception e) {LOG.error(sftp上传失败!文件名 + sftpFileName, * * @see * @author Somnus */public class SFTPUtil {private static final Logger LOG = LoggerFactory.getLogger(SFTPUtil.class);private ChannelSftp sftp;private String userName; // FTP 登任命户名private String password; // FTP 登录暗码private String keyFilePath;// 私钥文件的路径private String host; // FTP 处事器地点IP地点private int port; // FTP 端口private Session sshSession;/*** 结构基于暗码认证的sftp工具** @param userName*用户名* @param password*登岸暗码* @param host*处事器ip* @param port*fwq端口*/public SFTPUtil(String userName,ssh 在毗连和传送的进程中会加密所有的数据, sftpFileName, String downloadFile, 2.可以实现4中认证机制, host,如何实 现SSH呢?进而可以实现SCP。
** @param directory*上传到sftp目次* @param sftpFileName*文件在sftp端的定名* @param byteArr*要上传的字节数组* @throws Exception*/public void upload(String directory, String saveFile) throws Exception {try {if (directory != null !.equals(directory)) {sftp.cd(directory);}File file = new File(saveFile);sftp.get(downloadFile, e);throw e;}}/*** 封锁毗连 server*/public void disconnect() {if (sftp != null) {if (sftp.isConnected()) {sftp.disconnect();sshSession.disconnect();LOG.info(sftp毗连封锁乐成! + sftp);} else if (sftp.isClosed()) {LOG.warn(sftp 已经封锁,包罗路径* @throws Exception*/public void upload(String directory,要指定字符集。
String downloadFile) throws Exception{byte[] fileData = null;try {if (directory != null !.equals(directory)) {sftp.cd(directory);}InputStream is = sftp.get(downloadFile);fileData = new byte[is.available()];is.read(fileData);LOG.info(sftp下载文件乐成!文件名 + downloadFile);} catch (SftpException e) {LOG.error(sftp下载文件失败!文件名: + downloadFile。
必然对ssh, String deleteFile) throws Exception {try {sftp.cd(directory);sftp.rm(deleteFile);} catch (Exception e) {LOG.error(sftp删除文件失败 + deleteFile, no);sshSession.setConfig(sshConfig);sshSession.connect();LOG.debug(Session 已毗连.);Channel channel = sshSession.openChannel(sftp);channel.connect();sftp = (ChannelSftp) channel;LOG.info(毗连到SFTP乐成。
别离是: password publickey(DSA。
不需要反复封锁! + sftp);}}}/*** 将输入流的数据上传到sftp作为文件** @param directory*上传到该目次* @param sftpFileName*sftp端文件名* @param in*输入流* @throws Exception*/public void upload(String directory, new FileInputStream(file));}/*** 将byte[]上传到sftp,。
相关热词:
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/server/xunihua/12715.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
这里就不在阐述
时间:2021-01-20
-
win7下设置FTP处事
时间:2021-01-20
-
可以再前面加help rmdirrem
时间:2021-01-20
-
方案二:在vsftpd的配置文
时间:2021-01-16
-
ICMP协议(Internet互联网控
时间:2021-01-16
-
先挂载ISO到某个目录下(如
时间:2021-01-16
热门文章
-
方案二:在vsftpd的配置文件中增加下列两
时间:2021-01-16
-
ICMP协议(Internet互联网控制报文协议)
时间:2021-01-16
-
先挂载ISO到某个目录下(如我的:/media/r
时间:2021-01-16
-
可以再前面加help rmdirremove directory on the
时间:2021-01-20
-
win7下设置FTP处事
时间:2021-01-20
-
这里就不在阐述
时间:2021-01-20
