FTP, SFTP, FTPS examples In Java .
来源:互联网 发布:pvc地板 知乎 编辑:程序博客网 时间:2024/06/03 18:25
http://blog.csdn.net/luoshenhappy99/article/details/7448385
The FTP (File Transfer Protocol), FTPS (FTP over SSL), SFTP (FTP over SSH) and SCP (Secure Copy over SSH) .
http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/ftp/FTPSClient.html
The examples of FTP/FTPs, the jar supporting them is org.apache.commons.net.ftp.
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.security.NoSuchAlgorithmException;
- import java.util.Vector;
- import org.apache.commons.net.ftp.FTPClient;
- import org.apache.commons.net.ftp.FTPFile;
- import org.apache.commons.net.ftp.FTPReply;
- import org.apache.commons.net.ftp.FTPSClient;
- import org.apache.log4j.Logger;
- public class FtpClient{
- private static final Logger logger = Logger.getLogger(FtpClient.class);
- private FTPClient client;
- public FtpClient(boolean ftps) {
- if (ftps) {
- try {
- client = new FTPSClient(true);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- }
- } else {
- client = new FTPClient();
- }
- }
- public boolean changeDir(String remotePath) throws Exception {
- return client.changeWorkingDirectory(remotePath);
- }
- public boolean connect(String host, String login, String password, int port) throws Exception {
- logger.debug("FTP request connect to " + host + ":" + port);
- client.connect(host, port);
- int reply = client.getReplyCode();
- if (FTPReply.isPositiveCompletion(reply)) {
- logger.debug("FTP request login as " + login);
- if (client.login(login, password)) {
- client.enterLocalPassiveMode();
- return true;
- }
- }
- disconnect();
- return false;
- }
- public void disconnect() throws Exception {
- logger.debug("FTP request disconnect");
- client.disconnect();
- }
- protected boolean downloadFileAfterCheck(String remotePath, String localFile) throws IOException {
- boolean rst;
- FileOutputStream out = null;
- try {
- File file = new File(localFile);
- if (!file.exists()) {
- out = new FileOutputStream(localFile);
- rst = client.retrieveFile(remotePath, out);
- } else {
- rst = true;
- }
- } finally {
- if (out != null) {
- out.close();
- }
- }
- if (out != null) {
- out.close();
- }
- return rst;
- }
- protected boolean downloadFile(String remotePath, String localFile) throws IOException {
- boolean rst;
- FileOutputStream out = null;
- try {
- out = new FileOutputStream(localFile);
- rst = client.retrieveFile(remotePath, out);
- } finally {
- if (out != null) {
- out.close();
- }
- }
- return rst;
- }
- public Vector<String> listFileInDir(String remoteDir) throws Exception {
- if (changeDir(remoteDir)) {
- FTPFile[] files = client.listFiles();
- Vector<String> v = new Vector<String>();
- for (FTPFile file : files) {
- if (!file.isDirectory()) {
- v.addElement(file.getName());
- }
- }
- return v;
- } else {
- return null;
- }
- }
- public boolean uploadFile(String localFile, String remotePath) throws IOException {
- FileInputStream in = new FileInputStream(localFile);
- boolean rst;
- try {
- rst = client.storeFile(remotePath, in);
- } finally {
- in.close();
- }
- return rst;
- }
- @Override
- public Vector<String> listSubDirInDir(String remoteDir) throws Exception {
- if (changeDir(remoteDir)) {
- FTPFile[] files = client.listFiles();
- Vector<String> v = new Vector<String>();
- for (FTPFile file : files) {
- if (file.isDirectory()) {
- v.addElement(file.getName());
- }
- }
- return v;
- } else {
- return null;
- }
- }
- protected boolean createDirectory(String dirName) {
- try {
- return client.makeDirectory(dirName);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return false;
- }
- public boolean isARemoteDirectory(String path) {
- String cache = "/";
- try {
- cache = client.printWorkingDirectory();
- } catch (NullPointerException e) {
- //nop
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- boolean isDir = changeDir(path);
- changeDir(cache);
- return isDir;
- } catch (IOException e) {
- //e.printStackTrace();
- } catch (Exception e) {
- //e.printStackTrace();
- }
- return false;
- }
- public String getWorkingDirectory() {
- try {
- return client.printWorkingDirectory();
- } catch (IOException e) {
- }
- return null;
- }
- }
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.security.NoSuchAlgorithmException;import java.util.Vector;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPFile;import org.apache.commons.net.ftp.FTPReply;import org.apache.commons.net.ftp.FTPSClient;import org.apache.log4j.Logger;public class FtpClient{private static final Logger logger = Logger.getLogger(FtpClient.class);private FTPClient client;public FtpClient(boolean ftps) {if (ftps) {try {client = new FTPSClient(true);} catch (NoSuchAlgorithmException e) {e.printStackTrace();}} else {client = new FTPClient();}}public boolean changeDir(String remotePath) throws Exception {return client.changeWorkingDirectory(remotePath);}public boolean connect(String host, String login, String password, int port) throws Exception {logger.debug("FTP request connect to " + host + ":" + port);client.connect(host, port);int reply = client.getReplyCode();if (FTPReply.isPositiveCompletion(reply)) {logger.debug("FTP request login as " + login);if (client.login(login, password)) {client.enterLocalPassiveMode();return true;}}disconnect();return false;}public void disconnect() throws Exception {logger.debug("FTP request disconnect");client.disconnect();}protected boolean downloadFileAfterCheck(String remotePath, String localFile) throws IOException {boolean rst;FileOutputStream out = null;try {File file = new File(localFile);if (!file.exists()) {out = new FileOutputStream(localFile);rst = client.retrieveFile(remotePath, out);} else {rst = true;}} finally {if (out != null) {out.close();}}if (out != null) {out.close();}return rst;}protected boolean downloadFile(String remotePath, String localFile) throws IOException {boolean rst;FileOutputStream out = null;try {out = new FileOutputStream(localFile);rst = client.retrieveFile(remotePath, out);} finally {if (out != null) {out.close();}}return rst;}public Vector<String> listFileInDir(String remoteDir) throws Exception {if (changeDir(remoteDir)) {FTPFile[] files = client.listFiles();Vector<String> v = new Vector<String>();for (FTPFile file : files) {if (!file.isDirectory()) {v.addElement(file.getName());}}return v;} else {return null;}}public boolean uploadFile(String localFile, String remotePath) throws IOException {FileInputStream in = new FileInputStream(localFile);boolean rst;try {rst = client.storeFile(remotePath, in);} finally {in.close();}return rst;}@Overridepublic Vector<String> listSubDirInDir(String remoteDir) throws Exception {if (changeDir(remoteDir)) {FTPFile[] files = client.listFiles();Vector<String> v = new Vector<String>();for (FTPFile file : files) {if (file.isDirectory()) {v.addElement(file.getName());}}return v;} else {return null;}}protected boolean createDirectory(String dirName) {try {return client.makeDirectory(dirName);} catch (IOException e) {e.printStackTrace();}return false;}public boolean isARemoteDirectory(String path) {String cache = "/";try {cache = client.printWorkingDirectory();} catch (NullPointerException e) {//nop} catch (IOException e) {e.printStackTrace();}try {boolean isDir = changeDir(path);changeDir(cache);return isDir;} catch (IOException e) {//e.printStackTrace();} catch (Exception e) {//e.printStackTrace();}return false;}public String getWorkingDirectory() {try {return client.printWorkingDirectory();} catch (IOException e) {}return null;}}
the example of SFTP, the jar supporting this is com.jcraft.jsch
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Vector;
- import org.apache.commons.vfs.FileSystemException;
- import org.apache.commons.vfs.FileSystemOptions;
- import org.apache.commons.vfs.provider.sftp.SftpClientFactory;
- import org.apache.commons.vfs.provider.sftp.SftpFileSystemConfigBuilder;
- import com.jcraft.jsch.Channel;
- import com.jcraft.jsch.ChannelSftp;
- import com.jcraft.jsch.JSchException;
- import com.jcraft.jsch.Session;
- import com.jcraft.jsch.SftpException;
- import com.jcraft.jsch.ChannelSftp.LsEntry;
- public class SFTPClient{
- private ChannelSftp command;
- private Session session;
- public SFTPClient() {
- command = null;
- }
- public boolean connect(String host, String login, String password, int port) throws JSchException {
- //If the client is already connected, disconnect
- if (command != null) {
- disconnect();
- }
- FileSystemOptions fso = new FileSystemOptions();
- try {
- SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(fso, "no");
- session = SftpClientFactory.createConnection(host, port, login.toCharArray(), password.toCharArray(), fso);
- Channel channel = session.openChannel("sftp");
- channel.connect();
- command = (ChannelSftp) channel;
- } catch (FileSystemException e) {
- e.printStackTrace();
- return false;
- }
- return command.isConnected();
- }
- public void disconnect() {
- if (command != null) {
- command.exit();
- }
- if (session != null) {
- session.disconnect();
- }
- command = null;
- }
- public Vector<String> listFileInDir(String remoteDir) throws Exception {
- try {
- Vector<LsEntry> rs = command.ls(remoteDir);
- Vector<String> result = new Vector<String>();
- for (int i = 0; i < rs.size(); i++) {
- if (!isARemoteDirectory(rs.get(i).getFilename())) {
- result.add(rs.get(i).getFilename());
- }
- }
- return result;
- } catch (Exception e) {
- e.printStackTrace();
- System.err.println(remoteDir);
- throw new Exception(e);
- }
- }
- public Vector<String> listSubDirInDir(String remoteDir) throws Exception {
- Vector<LsEntry> rs = command.ls(remoteDir);
- Vector<String> result = new Vector<String>();
- for (int i = 0; i < rs.size(); i++) {
- if (isARemoteDirectory(rs.get(i).getFilename())) {
- result.add(rs.get(i).getFilename());
- }
- }
- return result;
- }
- protected boolean createDirectory(String dirName) {
- try {
- command.mkdir(dirName);
- } catch (Exception e) {
- return false;
- }
- return true;
- }
- protected boolean downloadFileAfterCheck(String remotePath, String localPath) throws IOException {
- FileOutputStream outputSrr = null;
- try {
- File file = new File(localPath);
- if (!file.exists()) {
- outputSrr = new FileOutputStream(localPath);
- command.get(remotePath, outputSrr);
- }
- } catch (SftpException e) {
- try {
- System.err.println(remotePath + " not found in " + command.pwd());
- } catch (SftpException e1) {
- e1.printStackTrace();
- }
- e.printStackTrace();
- return false;
- } finally {
- if (outputSrr != null) {
- outputSrr.close();
- }
- }
- return true;
- }
- protected boolean downloadFile(String remotePath, String localPath) throws IOException {
- FileOutputStream outputSrr = new FileOutputStream(localPath);
- try {
- command.get(remotePath, outputSrr);
- } catch (SftpException e) {
- try {
- System.err.println(remotePath + " not found in " + command.pwd());
- } catch (SftpException e1) {
- e1.printStackTrace();
- }
- e.printStackTrace();
- return false;
- } finally {
- if (outputSrr != null) {
- outputSrr.close();
- }
- }
- return true;
- }
- protected boolean uploadFile(String localPath, String remotePath) throws IOException {
- FileInputStream inputSrr = new FileInputStream(localPath);
- try {
- command.put(inputSrr, remotePath);
- } catch (SftpException e) {
- e.printStackTrace();
- return false;
- } finally {
- if (inputSrr != null) {
- inputSrr.close();
- }
- }
- return true;
- }
- public boolean changeDir(String remotePath) throws Exception {
- try {
- command.cd(remotePath);
- } catch (SftpException e) {
- return false;
- }
- return true;
- }
- public boolean isARemoteDirectory(String path) {
- try {
- return command.stat(path).isDir();
- } catch (SftpException e) {
- //e.printStackTrace();
- }
- return false;
- }
- public String getWorkingDirectory() {
- try {
- return command.pwd();
- } catch (SftpException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
- FTP, SFTP, FTPS examples In Java
- FTP, SFTP, FTPS examples In Java .
- FTP, SFTP, FTPS examples In Java .
- FTP、FTPS和SFTP
- FTP,SFTP,FTPS区别
- FTP,SFTP,FTPS区别
- FTP、FTPS和SFTP
- FTP,SFTP,FTPS区别
- sublime_text 2 package ftp ftps sftp
- SFTP, FTPS区别
- Linux--- ftps、sftp协议
- FTPS VS SFTP
- FTP,FTPS,FTPS与防火墙
- java 实现FTP sftp
- FTPS和SFTP的区别
- SFTP 和FTPS的区别
- FTPS 与SFTP大对决
- FTPS 与SFTP大对决
- ibatis 中 $与#的区别
- unicode和中文汉字之间的转换
- fatal error C1010: 在查找预编译头时遇到意外的文件结尾 ||error C2589: “(” : “::”右边的非法标记
- Explicit FTPS versus Implicit FTPS
- js的简单的事件处理和with关键字
- FTP, SFTP, FTPS examples In Java .
- 健康
- java配置环境变量
- Java Set 深入研究
- Hibernate中Criteria的完整用法
- kth sum of two sorted arrays
- 修改Ubuntu默认shell为bash
- Error: could not open `F:\Program Files\Java\jre6\lib\i386\jvm.cfg'
- vs2010-MSDN安装步骤