java sftp_Java 通过SFTP上传图片功能

public classSFTPUtils {privateChannelSftp sftp;privateSession session;privateString sftpPath;publicSFTPUtils() {this.connectServer("服务器IP", 22, "用户名", "密码", "需要保存文件的文件夹路径");

}public SFTPUtils(String ftpHost, intftpPort, String ftpUserName, String ftpPassword, String sftpPath) {this.connectServer(ftpHost, ftpPort, ftpUserName, ftpPassword, sftpPath);

}private void connectServer(String ftpHost, intftpPort, String ftpUserName, String ftpPassword, String sftpPath) {try{this.sftpPath =sftpPath;//创建JSch对象

JSch jsch = newJSch();//根据用户名,主机ip,端口获取一个Session对象

session =jsch.getSession(ftpUserName, ftpHost, ftpPort);if (ftpPassword != null) {//设置密码

session.setPassword(ftpPassword);

}

Properties configTemp= newProperties();

configTemp.put("StrictHostKeyChecking", "no");//为Session对象设置properties

session.setConfig(configTemp);//设置timeout时间

session.setTimeout(60000);

session.connect();//通过Session建立链接//打开SFTP通道

sftp = (ChannelSftp) session.openChannel("sftp");//建立SFTP通道的连接

sftp.connect();

}catch(JSchException e) {

e.printStackTrace();

}

}/*** 断开SFTP Channel、Session连接*/

public voidcloseChannel() {try{if (sftp != null) {

sftp.disconnect();

}if (session != null) {

session.disconnect();

}

}catch(Exception e) {

e.printStackTrace();

}

}/*** 上传文件

*

*@paramlocalFile 本地文件

*@paramremotePath 远程文件

*@paramfileName 文件名称*/

public voidupload(String localFile, String remotePath, String fileName) {try{if (remotePath != null && !"".equals(remotePath)) {

remotePath= sftpPath +remotePath;

createDir(remotePath);

sftp.put(localFile, (remotePath+fileName), ChannelSftp.OVERWRITE);

sftp.quit();

}

}catch(SftpException e) {

e.printStackTrace();

}

}/*** 下载文件

*

*@paramremotePath 远程文件

*@paramfileName 文件名称

*@paramlocalFile 本地文件*/

public voiddownload(String remotePath, String fileName, String localFile) {try{

remotePath= sftpPath +remotePath;if (remotePath != null && !"".equals(remotePath)) {

sftp.cd(remotePath);

}

sftp.get((remotePath+fileName), localFile);

sftp.quit();

}catch(SftpException e) {

e.printStackTrace();

}

}/*** 删除文件

*

*@paramremotePath 要删除文件所在目录*/

public voiddelete(String remotePath) {try{if (remotePath != null && !"".equals(remotePath)) {

remotePath= sftpPath +remotePath;

sftp.rm(remotePath);

}

}catch(Exception e) {

e.printStackTrace();

}

}/*** 创建一个文件目录*/

public voidcreateDir(String createpath) {try{if(isDirExist(createpath)) {this.sftp.cd(createpath);return;

}

String pathArry[]= createpath.split("/");

StringBuffer filePath= new StringBuffer("/");for(String path : pathArry) {if (path.equals("")) {continue;

}

filePath.append(path+ "/");if(isDirExist(filePath.toString())) {

sftp.cd(filePath.toString());

}else{//建立目录

sftp.mkdir(filePath.toString());//进入并设置为当前目录

sftp.cd(filePath.toString());

}

}this.sftp.cd(createpath);

}catch(SftpException e) {throw new SystemException("创建路径错误:" +createpath);

}

}/*** 判断目录是否存在*/

public booleanisDirExist(String directory) {boolean isDirExistFlag = false;try{

SftpATTRS sftpATTRS=sftp.lstat(directory);

isDirExistFlag= true;returnsftpATTRS.isDir();

}catch(Exception e) {if (e.getMessage().toLowerCase().equals("no such file")) {

isDirExistFlag= false;

}

}returnisDirExistFlag;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值