sftp 上传文件 java_java实现sftp上传文件

工具代码

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelSftp;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.JSchException;

import com.jcraft.jsch.Session;

import com.jcraft.jsch.SftpException;

/**

*@authorzhr

*@date2018-03-29 15:21:29

*/

public class MySftpUtil {

/*

* 需要的依赖包

*

com.jcraft

jsch

0.1.54

*/

/**

* ftp服务的地址 (ip)

*/

public String ip;

/**

* sftp服务器的端口号 默认22

*/

public int port = 22;

/**

* sftp服务器的登入帐号

*/

public String userName;

/**

* sftp服务器的登入密码

*/

public String password;

/**

* 连接超时时间

*/

public int timeout = 60000;

/**

* 将要上传的文件地址(网络地址没试过)

*/

public String src;

/**

* 要保存到服务器的文件地址和名称(最好带后缀,如果不带后缀会变成未知文件)

*/

public String dst;

/**

* 尝试重复几次上传(未支持)

*/

public int retryTime = 3;

public MySftpUtil setIp(String ip) {

this.ip = ip;

return this;

}

public MySftpUtil setPort(int port) {

this.port = port;

return this;

}

public MySftpUtil setUserName(String userName) {

this.userName = userName;

return this;

}

public MySftpUtil setPassword(String password) {

this.password = password;

return this;

}

public MySftpUtil setTimeout(int timeout) {

this.timeout = timeout;

return this;

}

public MySftpUtil setSrc(String src) {

this.src = src;

return this;

}

public MySftpUtil setDst(String dst) {

this.dst = dst;

return this;

}

public MySftpUtil setRetryTime(int retryTime) {

this.retryTime = retryTime;

return this;

}

public Channel channel = null;

public Session session = null;

/**

* 获得SFTP Channel

*

* @return ChannelSftp Instance

* @throws JSchException

*/

private ChannelSftp getChannel() throws JSchException {

String ftpHost = ip;

int ftpPort = port;

String ftpUserName = userName;

String ftpPassword = password;

// 创建JSch对象

JSch jsch = new JSch();

// 根据用户名,主机ip,端口获取一个Session对象

session = jsch.getSession(ftpUserName, ftpHost, ftpPort);

// logger.info("Session created.");

if (ftpPassword != null) {

// 设置密码

session.setPassword(ftpPassword);

}

Properties configTemp = new Properties();

configTemp.put("StrictHostKeyChecking", "no");

// 为Session对象设置properties

session.setConfig(configTemp);

// 设置timeout时间

session.setTimeout(timeout);

session.connect();

// 通过Session建立链接

// 打开SFTP通道

channel = session.openChannel("sftp");

// 建立SFTP通道的连接

channel.connect();

System.out.println("Connected successfully to ftpHost = " + ftpHost + ",as ftpUserName = " + ftpUserName

+ ", returning: " + channel);

return (ChannelSftp) channel;

}

/**

* 断开SFTP Channel、Session连接

*

* @throws Exception

*/

private void closeChannel() {

if (channel != null) {

channel.disconnect();

}

if (session != null) {

session.disconnect();

}

System.out.println("disconnected SFTP successfully!");

}

/**

* 上传

* @return

*/

public String put() {

if (null == ip || !ip.matches("^\\d+\\.\\d+\\.\\d+\\.\\d+$")) {

return "1--请填写服务器地址";

}

if (port < 1) {

return "2--端口号错误";

}

if (null == userName || userName.trim().length() < 1) {

return "3--请填写服务器登入帐号";

}

if (null == password || password.trim().length() < 1) {

return "4--请填写服务器登入密码";

}

if (null == src || src.trim().length() < 1) {

return "5--请填写讲要上传的文件地址";

}

if (null == dst || dst.trim().length() < 1) {

return "6--请填写上传后文件保存的位置";

}

ChannelSftp chSftp = null;

try {

chSftp = getChannel();

} catch (JSchException e1) {

e1.printStackTrace();

}

try {

chSftp.put(src, dst, ChannelSftp.OVERWRITE);

} catch (SftpException e) {

e.printStackTrace();

}

chSftp.quit();

closeChannel();

return "7--上传成功";

}

}

使用例子

public static void main(String[] args) throws Exception {

MySftpUtil msu = new MySftpUtil();

// 本地文件名

String src = "D:\\doc\\测试文件.pdf";

// 目标文件名

String dst = "/data/defult_page/bb.pdf";

System.out.println(

msu.setIp("服务器的ip").setUserName("账号").setPassword("密码").setSrc(src).setDst(dst).put());

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值