`

SFTP服务器的文件管理(转)

    博客分类:
  • Java
阅读更多

来源(http://forhope.iteye.com/blog/798754)

 

导入包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;
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics