SFTP服务器的文件管理

导入包jsch-0.1.43.jar

package com.network.manage.device.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

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;

public class SFTPUtil {
private static JSch jSch = new JSch();
private static ChannelSftp sftp = null;
private static Channel channel = null;
private static Session session = null;

public static boolean login(String hostname, int port, String username,
String password) {
try {
session = jSch.getSession(username, hostname, port);
session.setPassword(password);

Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
session.connect();

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

sftp = (ChannelSftp) channel;
return true;
} catch (JSchException e) {
System.err.println("SSH方式连接FTP服务器时有JSchException异常!");
System.err.println(e.getMessage());
return false;
}
}

public static boolean downloadFile(String hostname, int port,
String username, String password, String remotePath,
String remoteFilename, String localFilename) {
FileOutputStream output = null;
boolean success = false;
try {
// SSH方式登录FTP服务器
success = login(hostname, port, username, password);

//登录失败
if (!success) {
return success;
}

if (null != remotePath && remotePath.trim() != "") {
sftp.cd(remotePath);
}

File localFile = new File(localFilename);
//有文件和下载文件重名
if (localFile.exists()) {
sftp.disconnect();
System.err.println("文件: " + localFilename + " 已经存在!");
return success;
}

output = new FileOutputStream(localFile);
sftp.get(remoteFilename, output);
success = true;
} catch (SftpException e) {
System.err.println("接收文件时有SftpException异常!");
e.printStackTrace();
System.err.println(e.getMessage());
return success;
} catch (IOException e) {
System.err.println("接收文件时有I/O异常!");
System.err.println(e.getMessage());
return success;
} finally {
try {
if (null != output) {
output.close();
}
} catch (IOException e) {
System.err.println("关闭文件时出错!");
System.err.println(e.getMessage());
}
if (sftp.isConnected()) {
sftp.disconnect();
}
if (channel.isConnected()) {
channel.disconnect();
}
if (session.isConnected()) {
session.disconnect();
}
}
return success;
}

public static boolean uploadFile(String hostname, int port,
String username, String password, String remotePath,
String remoteFilename, InputStream input) {
boolean success = false;
try {
// SSH方式登录FTP服务器
success = login(hostname, port, username, password);

//登录失败
if (!success) {
return success;
}

// 更改服务器目录
if (null != remotePath && remotePath.trim() != "") {
sftp.cd(remotePath);
}

// 发送文件
sftp.put(input, remoteFilename);
success = true;
} catch (SftpException e) {
System.err.println("发送文件时有SftpException异常!");
e.printStackTrace();
System.err.println(e.getMessage());
return success;
} catch (Exception e) {
System.err.println("发送文件时有异常!");
System.err.println(e.getMessage());
return success;
} finally {
try {
if (null != input) {
input.close();
}
} catch (IOException e) {
System.err.println("关闭文件时出错!");
System.err.println(e.getMessage());
}
if (sftp.isConnected()) {
sftp.disconnect();
}
if (channel.isConnected()) {
channel.disconnect();
}
if (session.isConnected()) {
session.disconnect();
}
}
return success;
}

public static boolean deleteFile(String hostname, int port, String username,
String password, String remotePath, String remoteFilename) {
boolean success = false;
try {
// SSH方式登录FTP服务器
success = login(hostname, port, username, password);

//登录失败
if (!success) {
return success;
}

// 更改服务器目录
if (null != remotePath && remotePath.trim() != "") {
sftp.cd(remotePath);
}

// 删除文件
sftp.rm(remoteFilename);
success = true;
} catch (SftpException e) {
System.err.println("删除文件时有SftpException异常!");
e.printStackTrace();
System.err.println(e.getMessage());
return success;
} catch (Exception e) {
System.err.println("删除文件时有异常!");
System.err.println(e.getMessage());
return success;
} finally {
if (sftp.isConnected()) {
sftp.disconnect();
}
if (channel.isConnected()) {
channel.disconnect();
}
if (session.isConnected()) {
session.disconnect();
}
}
return success;
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值