java 中 FTP 和 SFTP 的简单使用

1 篇文章 0 订阅

FTP和SFTP的区别和工作原理:

很详细:http://blog.csdn.net/cuker919/article/details/6403925

可以根据自己的实际情况选择使用FTP或者SFTP,使用FTP时要安装FTP,而SFTP不需要安装。

效率:FTP>SFTP

安全:SFTP>FTP

这儿只是比较SFTP 读取image和URL读取image效率:适用文件服务和系统服务是不同服务器。

public class SFTP {
public static void testSftp() throws Exception{
ChannelSftp sftp = ChannelSFTPImpl.getSFTP();
String directory = "/data/apps/c2m/release/images-src";
String downloadFile = "/2S8959_20180114162259.jpg";
long s = System.currentTimeMillis();
InputStream is = sftp.get(directory+downloadFile);
BufferedImage image = ImageIO.read(is);
long e = System.currentTimeMillis();
System.out.println(e-s);
}
public static void testUrl() throws Exception{
String str = "http://h5.jinzhiyun.net/images-src/2S8959_20180114162259.jpg";
URL url = new URL(str);
long s = System.currentTimeMillis();
BufferedImage image = ImageIO.read(url);
long e = System.currentTimeMillis();
System.out.println(e-s);
}
public static void main(String[] args) throws Exception {
testSftp();  //SFTP      198ms
testUrl();   //URL        497ms
}
}

//执行结果


ChannelSFTPImpl.java

public class ChannelSFTPImpl {
private static final String host = "";
private static final int port = 22;
private static final String username = "";
private static String password = "";
public static ChannelSftp sftp = null;
private  ChannelSFTPImpl() {
}
public static ChannelSftp getSFTP() {
if(null == sftp){
MySFTP sf = new MySFTP();
sftp = sf.connect(host, port, username, password);
}
return sftp;
}
}

MySFTP.java   connect()

public ChannelSftp connect(String host, int port, String username, String password) {
ChannelSftp sftp = null;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
com.jcraft.jsch.Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (Exception e) {
}
return sftp;
}

FTP:从FTP服务器下载文件:

public class FtpUtil {
public static void main(String[] args) {
String url="";
int port = 21; 
String username = "";
        String password = "";
        String remotePath ="/data/apps/c2m/release/images-src";
        String fileName = "";
downFile(url,port,username,password,remotePath,fileName);
}
/**
     * Description: 从FTP服务器下载文件
     */
    public static boolean downFile(String url,int port, String username,String password, String remotePath, String fileName) {
          boolean success= false;
          FTPClient ftp= new FTPClient();
          int reply;
          try{
          ftp.enterLocalPassiveMode();//本地模式
               ftp.connect(url,port);
               ftp.login(username,password);
               ftp.setFileType(FTPClient.BINARY_FILE_TYPE);//文件类型为二进制文件
               reply= ftp.getReplyCode();
               if(!FTPReply.isPositiveCompletion(reply)) {
                    ftp.disconnect();
                    return success;
               }
               ftp.changeWorkingDirectory(remotePath);
               FTPFile []fs= ftp.listFiles();
               for(FTPFile ff: fs) {
                    if(ff.getName().equals(fileName)) {
                    }
               }
               ftp.logout();
               success= true;
          }catch(SocketException e) {
               e.printStackTrace();
          }catch(IOException e) {
               e.printStackTrace();
          }finally{
               if(ftp.isConnected()) {
                    try{
                          ftp.disconnect();
                    }catch(IOException e) {
                          e.printStackTrace();
                    }
               }
          }
          return success;
    }
}

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值