java 跨服务器上传文件_java 文件跨服务器上传--JSch

本文介绍了一个Java类`FileUploadUtil`,该类利用JSch库实现在不同服务器之间的文件上传。通过设置用户名、主机IP、密码以及目标服务器路径,程序能够创建SSH会话,打开SFTP通道,并将输入流中的文件上传到指定位置。
摘要由CSDN通过智能技术生成

importjava.io.IOException;importjava.io.InputStream;importjava.util.Properties;importcom.jcraft.jsch.ChannelSftp;importcom.jcraft.jsch.ChannelShell;importcom.jcraft.jsch.JSch;importcom.jcraft.jsch.Session;public classFileUploadUtil {private static final int DEFAULT_PORT = 22;//端口

private static final int DEF_WAIT_SECONDS = 30;//请求时间

private static String username = "*****";//用户名

private static String host = "192.168.195.129";//ip地址

private static String password = "*****";//密码//放在服务器的目标位置

private static String dstfilepath = "/home/huyanlon/harbinFile";public static voidstoreFile(String sourcefileName, InputStream inpustStream) {//sourcefileName 文件名称 inpustStream 文件流

Session session=openSession(host, username, password, DEF_WAIT_SECONDS);

ChannelShell openChannelShell=openChannelShell(session);

openChannelShell.setInputStream(System.in);

openChannelShell.setOutputStream(System.out);

ChannelSftp openChannelSftp=openChannelSftp(session);try{

openChannelSftp.put(inpustStream, dstfilepath+"/"+sourcefileName);//删除文件可用如下方法,进入某文件所在的目录后删除该文件//openChannelSftp.cd(dstfilepath);//openChannelSftp.rm(sourcefile);

} catch(Exception e) {

e.printStackTrace();

}

}/*** 创建服务器连接

*

*@paramhost

* 主机

*@paramuser

* 用户

*@parampassword

* 密码

*@paramwaitSeconds

* 等待秒数

*@return

*/

private static Session openSession(String host, String user, String password, intwaitSeconds) {

Session session= null;try{

JSch jsch= newJSch();

session=jsch.getSession(user, host, DEFAULT_PORT);

noCheckHostKey(session);

session.setPassword(password);//这个设置很重要,必须要设置等待时长为大于等于2分钟

session.connect(waitSeconds * 1000);if (!session.isConnected()) {throw new IOException("We can't connection to[" + host + "]");

}

}catch(Exception e) {

e.printStackTrace();

}returnsession;

}/*** 不作检查主机键值

*

*@paramsession*/

private static voidnoCheckHostKey(Session session) {

Properties config= newProperties();

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

session.setConfig(config);

}/*** 连接shell

*

*@paramsession

* session

*@return{@linkChannelShell}*/

private staticChannelShell openChannelShell(Session session) {

ChannelShell channel= null;try{

channel= (ChannelShell) session.openChannel("shell");

channel.setEnv("LANG", "en_US.UTF-8");

channel.setAgentForwarding(false);

channel.setPtySize(500, 500, 1000, 1000);

channel.connect();

}catch(Exception e) {

e.printStackTrace();

}if (channel == null) {throw new IllegalArgumentException("The channle init was wrong");

}returnchannel;

}/*** 连接sftp

*

*@paramsession

*@return{@linkChannelSftp}*/

private staticChannelSftp openChannelSftp(Session session) {

ChannelSftp channel= null;try{

channel= (ChannelSftp) session.openChannel("sftp");

channel.connect();

}catch(Exception e) {

e.printStackTrace();

}returnchannel;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值