Java用SFTP实现上传下载功能
来源:互联网 发布:0信誉淘宝店 编辑:程序博客网 时间:2024/05/21 11:17
请先提前下载jar包jsch-0.1.54.jar,官网下载地址
http://www.jcraft.com/jsch/
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.Properties;import com.jcraft.jsch.Channel;import com.jcraft.jsch.ChannelSftp;import com.jcraft.jsch.JSch;import com.jcraft.jsch.Session;/** * SFTP工具类 */public class SFTPUtils {static Session sshSession = null;public static void main(String[] args) {ChannelSftp ftp = getConnectIP("223.105.1.30", "22", "username", "password");upload("/home/cmsoftu/detail/", "E:\\recordfilecopy\\record1.txt", ftp);// download("/home/cmsoftu/detail_history/", "record1.txt", "E:\\recordfile\\record1.txt", ftp);}/** * 获取ChannelSftp gst:2017-9-12 * * @param host * 主机 * @param sOnlineSftpPort * 端口 * @param username * 用户名 * @param password * 密码 * @return */public static ChannelSftp getConnectIP(String host, String sOnlineSftpPort, String username, String password) {int port = 0;if (!("".equals(sOnlineSftpPort)) && null != sOnlineSftpPort) {port = Integer.parseInt(sOnlineSftpPort);}ChannelSftp sftp = null;try {JSch jsch = new JSch();jsch.getSession(username, host, port);sshSession = jsch.getSession(username, host, port);sshSession.setPassword(password);Properties sshConfig = new Properties();sshConfig.put("StrictHostKeyChecking", "no");sshSession.setConfig(sshConfig);sshSession.connect();Channel channel = sshSession.openChannel("sftp");channel.connect();sftp = (ChannelSftp) channel;} catch (Exception e) {e.printStackTrace();}return sftp;}/** * 上传 gst:2017-9-12 * * @param directory * sftp 服务器目录 * @param uploadFile * 上传文件路径 * @param sftp */public static void upload(String directory, String uploadFile, ChannelSftp sftp) {try {sftp.cd(directory);File file = new File(uploadFile);sftp.put(new FileInputStream(file), file.getName());} catch (Exception e) {e.printStackTrace();} finally {if (sftp.isConnected()) {sshSession.disconnect();sftp.disconnect();}}}/** * 下载 gst:2017-9-12 * * @param directory * sftp服务器目录 * @param downloadFile * 目录下的文件名称 * @param saveFile * 本地保存文件路径 * @param sftp */public static void download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) {try {sftp.cd(directory);File file = new File(saveFile);sftp.get(downloadFile, new FileOutputStream(file));} catch (Exception e) {e.printStackTrace();} finally {if (sftp.isConnected()) {sshSession.disconnect();sftp.disconnect();}}}/** * 删除 gst:2017-9-12 * * @param directory * @param deleteFile * @param sftp */public static void delete(String directory, String deleteFile, ChannelSftp sftp) {try {sftp.cd(directory);sftp.rm(deleteFile);} catch (Exception e) {e.printStackTrace();} finally {if (sftp.isConnected()) {sshSession.disconnect();sftp.disconnect();}}}}
阅读全文
1 0
- Java用SFTP实现上传下载功能
- .net 实现sftp 上传下载功能
- 用java实现上传下载功能
- 用java实现上传下载功能
- jsch java实现sftp文件上传下载
- Java 使用 SFTP 实现文件上传下载(一)
- Java 使用 SFTP 实现文件上传下载(二)
- JAVA SFTP上传下载
- SFTP 上传下载 java
- Java实现FTP上传下载功能
- Java实现FTP上传下载功能
- Java实现FTP上传下载功能
- Java实现FTP上传下载功能
- Java实现FTP上传下载功能
- Java实现FTP上传下载功能
- Java实现FTP上传下载功能
- Java实现文件上传下载功能
- java实现文件上传下载功能
- 字符串排序
- 【最短路】题型小总结
- android开发环境搭建
- linux下安装mysql和mysql主从配置
- 构造函数和一般函数的区别
- Java用SFTP实现上传下载功能
- 2、DES加密算法
- log4j2入门教程(一)简介(xml文件配置)
- dubbo工作原理
- 浅学Gson
- jsp九大内置对象与四大域对象
- 两个字符串的最长公共子序列
- 数据挖掘之异常点检测
- CentOS中安装eclipse详细步骤