SFTP服务配置以及命令/代码操作
POM
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>com.qzc.helpers</groupId> | |
| <artifactId>j_sftp</artifactId> | |
| <packaging>jar</packaging> | |
| <version>1.0-SNAPSHOT</version> | |
| <name>j_sftp</name> | |
| <url>http://maven.apache.org</url> | |
| <dependencies> | |
| <!-- https://mvnrepository.com/artifact/com.jcraft/jsch --> | |
| <dependency> | |
| <groupId>com.jcraft</groupId> | |
| <artifactId>jsch</artifactId> | |
| <version>0.1.54</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>org.apache.commons</groupId> | |
| <artifactId>commons-lang3</artifactId> | |
| <version>3.5</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>junit</groupId> | |
| <artifactId>junit</artifactId> | |
| <version>4.12</version> | |
| <scope>test</scope> | |
| </dependency> | |
| </dependencies> | |
| </project> |
| package com.qzc.helpers; | |
| import com.jcraft.jsch.ChannelSftp; | |
| import com.qzc.helpers.sftp.SFTPClient; | |
| import com.qzc.helpers.sftp.SFTPConfig; | |
| import com.qzc.helpers.sftp.SFTPException; | |
| import com.qzc.helpers.sftp.SFTPParams; | |
| import com.qzc.helpers.sftp.monitor.TimerSFTPProgressMonitor; | |
| import org.apache.commons.lang3.StringUtils; | |
| import java.io.InputStream; | |
| import java.io.OutputStream; | |
| import java.util.List; | |
| /** | |
| * Create by qiezhichao on 2018/5/10 0010 19:44 | |
| */ | |
| public class SFTPHelper { | |
| private final static String sftpHost = "192.168.118.129"; | |
| private final static int sftpPort = 22; | |
| private final static String sftpUserName = "sftpuser"; | |
| private final static String sftpPassword = "19920508"; | |
| private static SFTPClient getSFTPClient() { | |
| return new SFTPClient(new SFTPConfig(sftpHost, sftpPort, sftpUserName, sftpPassword)); | |
| } | |
| /*************************************upload*************************************/ | |
| public static void uploadOverwrite(String localFilePath, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(localFilePath, remotePath); | |
| client.upload(params, ChannelSftp.OVERWRITE); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void uploadAppend(String localFilePath, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(localFilePath, remotePath); | |
| client.upload(params, ChannelSftp.APPEND); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void uploadResume(String localFilePath, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(localFilePath, remotePath); | |
| client.upload(params, ChannelSftp.RESUME); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void uploadOverwriteWithTimerMonitor(String localFilePath, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(localFilePath, remotePath); | |
| client.upload(params, ChannelSftp.OVERWRITE, new TimerSFTPProgressMonitor()); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void uploadAppendWithTimerMonitor(String localFilePath, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(localFilePath, remotePath); | |
| client.upload(params, ChannelSftp.APPEND, new TimerSFTPProgressMonitor()); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void uploadResumeWithTimerMonitor(String localFilePath, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(localFilePath, remotePath); | |
| client.upload(params, ChannelSftp.RESUME, new TimerSFTPProgressMonitor()); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void uploadOverwrite(InputStream src, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(null, remotePath); | |
| client.upload(params, ChannelSftp.OVERWRITE, src); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void uploadAppend(InputStream src, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(null, remotePath); | |
| client.upload(params, ChannelSftp.APPEND, src); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void uploadResume(InputStream src, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(null, remotePath); | |
| client.upload(params, ChannelSftp.RESUME, src); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void uploadOverwrite(InputStream src, int bufferSize, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(null, remotePath); | |
| client.upload(params, ChannelSftp.OVERWRITE, src, bufferSize); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void uploadAppend(InputStream src, int bufferSize, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(null, remotePath); | |
| client.upload(params, ChannelSftp.APPEND, src, bufferSize); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void uploadResume(InputStream src, int bufferSize, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(null, remotePath); | |
| client.upload(params, ChannelSftp.RESUME, src, bufferSize); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| /*************************************download*************************************/ | |
| public static void download(String remotePath, String localFilePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(localFilePath, remotePath); | |
| client.download(params); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void downloadWithTimerMonitor(String remotePath, String localFilePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(localFilePath, remotePath); | |
| client.download(params, new TimerSFTPProgressMonitor()); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void download(OutputStream os, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(null, remotePath); | |
| client.download(params, os); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| public static void downloadWithTimerMonitor(OutputStream os, String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = buildSFTPParams(null, remotePath); | |
| client.download(params, new TimerSFTPProgressMonitor(), os); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| } | |
| /** | |
| * 该方法使用时会出现异常:java.io.IOException: Pipe closed | |
| * 原因是在 finally 中执行了 client.close(); | |
| * | |
| * @param remotePath | |
| * @return | |
| */ | |
| @Deprecated | |
| public static InputStream download2InputStream(String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| SFTPParams params = new SFTPParams(); | |
| params.setRemoteFilepath(remotePath); | |
| return client.download2InputStream(params); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| return null; | |
| } | |
| /*************************************other*************************************/ | |
| public static List<String> ls(String remotePath) { | |
| SFTPClient client = getSFTPClient(); | |
| try { | |
| if (StringUtils.isEmpty(remotePath.trim())) { | |
| return null; | |
| } | |
| return client.ls(remotePath); | |
| } catch (SFTPException e) { | |
| e.printStackTrace(); | |
| } finally { | |
| client.close(); | |
| } | |
| return null; | |
| } | |
| /*************************************private*************************************/ | |
| /* | |
| * 构造SFTPParams参数 | |
| * | |
| * @param remotePath | |
| * @param localFilePath | |
| * @return | |
| */ | |
| private static SFTPParams buildSFTPParams(String localFilePath, String remotePath) { | |
| SFTPParams params = new SFTPParams(); | |
| if (StringUtils.isNotEmpty(localFilePath)) { | |
| params.setLocalFilepath(localFilePath); | |
| } | |
| if (StringUtils.isNotEmpty(remotePath)) { | |
| params.setRemoteFilepath(remotePath); | |
| } | |
| return params; | |
| } | |
| } |
| package com.qzc.helpers.sftp; | |
| import com.jcraft.jsch.*; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.OutputStream; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.Vector; | |
| /** | |
| * Create by qiezhichao on 2018/5/10 0011 20:36 | |
| */ | |
| public class SFTPClient { | |
| private Session session; | |
| private ChannelSftp channelSftp; | |
| private SFTPConfig sftpConfig; | |
| public SFTPClient(SFTPConfig sftpConfig) { | |
| this.sftpConfig = sftpConfig; | |
| this.connect(sftpConfig); | |
| } | |
| private void connect(SFTPConfig sftpConfig) { | |
| try { | |
| session = new JSch().getSession(sftpConfig.getSftpUserName(), | |
| sftpConfig.getSftpHost(), | |
| sftpConfig.getSftpPort()); | |
| if (null != sftpConfig.getSftpPassword()) { | |
| session.setPassword(sftpConfig.getSftpPassword()); | |
| } | |
| if (null != sftpConfig.getTimeout()) { | |
| session.setTimeout(sftpConfig.getTimeout()); | |
| } | |
| session.setConfig("StrictHostKeyChecking", "no"); // 让ssh客户端自动接受新主机的hostkey | |
| session.connect(); | |
| this.channelSftp = (ChannelSftp) session.openChannel("sftp"); | |
| this.channelSftp.connect(); | |
| } catch (JSchException e) { | |
| this.close(); | |
| e.printStackTrace(); | |
| } | |
| } | |
| public void close() { | |
| channelSftp.quit(); | |
| if (null != channelSftp) { | |
| channelSftp.disconnect(); | |
| } | |
| if (null != session) { | |
| session.disconnect(); | |
| } | |
| } | |
| /*============================================upload============================================*/ | |
| /** | |
| * @param sftpParams | |
| * @param channelSftpModel 调用的模式: ChannelSftp.OVERWRITE,ChannelSftp.APPEND,ChannelSftp.RESUME | |
| * @throws SFTPException | |
| */ | |
| public void upload(SFTPParams sftpParams, int channelSftpModel) throws SFTPException { | |
| try { | |
| channelSftp.put(sftpParams.getLocalFilepath(), sftpParams.getRemoteFilepath(), channelSftpModel); | |
| } catch (SftpException e) { | |
| throw new SFTPException("Upload [" + sftpParams.getLocalFilepath() + "] to SFTP " | |
| + sftpConfig.getSftpHost() + ":" + sftpConfig.getSftpPort() | |
| + "[" + sftpParams.getRemoteFilepath() + "]" + " error.", e); | |
| } | |
| } | |
| /** | |
| * @param sftpParams | |
| * @param channelSftpModel 调用的模式: ChannelSftp.OVERWRITE,ChannelSftp.APPEND,ChannelSftp.RESUME | |
| * @param monitor 监视器 | |
| * @throws SFTPException | |
| */ | |
| public void upload(SFTPParams sftpParams, int channelSftpModel, SftpProgressMonitor monitor) throws SFTPException { | |
| try { | |
| channelSftp.put(sftpParams.getLocalFilepath(), sftpParams.getRemoteFilepath(), monitor, channelSftpModel); | |
| } catch (SftpException e) { | |
| throw new SFTPException("Upload [" + sftpParams.getLocalFilepath() + "] to SFTP " | |
| + sftpConfig.getSftpHost() + ":" + sftpConfig.getSftpPort() | |
| + "[" + sftpParams.getRemoteFilepath() + "]" + " error.", e); | |
| } | |
| } | |
| /** | |
| * @param sftpParams | |
| * @param channelSftpModel 调用的模式: ChannelSftp.OVERWRITE,ChannelSftp.APPEND,ChannelSftp.RESUME | |
| * @param src 输入流 | |
| * @throws SFTPException | |
| */ | |
| public void upload(SFTPParams sftpParams, int channelSftpModel, InputStream src) throws SFTPException { | |
| try { | |
| channelSftp.put(src, sftpParams.getRemoteFilepath(), channelSftpModel); | |
| } catch (SftpException e) { | |
| throw new SFTPException("Upload [" + sftpParams.getLocalFilepath() + "] to SFTP " | |
| + sftpConfig.getSftpHost() + ":" + sftpConfig.getSftpPort() | |
| + "[" + sftpParams.getRemoteFilepath() + "]" + " error.", e); | |
| } | |
| } | |
| /** | |
| * @param sftpParams | |
| * @param channelSftpModel 调用的模式: ChannelSftp.OVERWRITE,ChannelSftp.APPEND,ChannelSftp.RESUME | |
| * @param src 输入流 | |
| * @param bufferSize 数据块大小 | |
| * @throws SFTPException | |
| */ | |
| public void upload(SFTPParams sftpParams, int channelSftpModel, InputStream src, int bufferSize) throws SFTPException { | |
| OutputStream out = null; | |
| try { | |
| out = channelSftp.put(sftpParams.getRemoteFilepath(), channelSftpModel); | |
| byte[] buff = new byte[bufferSize]; // 设定每次传输的数据块大小 | |
| int read; | |
| if (out != null) { | |
| do { | |
| read = src.read(buff, 0, buff.length); | |
| if (read > 0) { | |
| out.write(buff, 0, read); | |
| } | |
| out.flush(); | |
| } while (read >= 0); | |
| } | |
| } catch (IOException e) { | |
| throw new SFTPException("Upload [" + sftpParams.getLocalFilepath() + "] to SFTP " | |
| + sftpConfig.getSftpHost() + ":" + sftpConfig.getSftpPort() | |
| + "[" + sftpParams.getRemoteFilepath() + "]" + " error.", e); | |
| } catch (SftpException e) { | |
| throw new SFTPException("Upload [" + sftpParams.getLocalFilepath() + "] to SFTP " | |
| + sftpConfig.getSftpHost() + ":" + sftpConfig.getSftpPort() | |
| + "[" + sftpParams.getRemoteFilepath() + "]" + " error.", e); | |
| } finally { | |
| if (null != out) { | |
| try { | |
| out.close(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| } | |
| /*============================================download============================================*/ | |
| /** | |
| * @param sftpParams | |
| * @throws SFTPException | |
| */ | |
| public void download(SFTPParams sftpParams) throws SFTPException { | |
| try { | |
| channelSftp.get(sftpParams.getRemoteFilepath(), sftpParams.getLocalFilepath()); | |
| } catch (SftpException e) { | |
| throw new SFTPException("Download [" + sftpParams.getRemoteFilepath() + "] from SFTP " | |
| + sftpConfig.getSftpHost() + ":" + sftpConfig.getSftpPort() + " error.", e); | |
| } | |
| } | |
| /** | |
| * @param sftpParams | |
| * @param os | |
| * @throws SFTPException | |
| */ | |
| public void download(SFTPParams sftpParams, OutputStream os) throws SFTPException { | |
| try { | |
| channelSftp.get(sftpParams.getRemoteFilepath(), os); | |
| } catch (SftpException e) { | |
| throw new SFTPException("Download [" + sftpParams.getRemoteFilepath() + "] from SFTP " | |
| + sftpConfig.getSftpHost() + ":" + sftpConfig.getSftpPort() + " error.", e); | |
| } | |
| } | |
| /** | |
| * @param sftpParams | |
| * @param monitor | |
| * @throws SFTPException | |
| */ | |
| public void download(SFTPParams sftpParams, SftpProgressMonitor monitor) throws SFTPException { | |
| try { | |
| channelSftp.get(sftpParams.getRemoteFilepath(), sftpParams.getLocalFilepath(), monitor); | |
| } catch (SftpException e) { | |
| throw new SFTPException("Download [" + sftpParams.getRemoteFilepath() + "] from SFTP " | |
| + sftpConfig.getSftpHost() + ":" + sftpConfig.getSftpPort() + " error.", e); | |
| } | |
| } | |
| /** | |
| * @param sftpParams | |
| * @param monitor | |
| * @param os | |
| * @throws SFTPException | |
| */ | |
| public void download(SFTPParams sftpParams, SftpProgressMonitor monitor, OutputStream os) throws SFTPException { | |
| try { | |
| channelSftp.get(sftpParams.getRemoteFilepath(), os, monitor); | |
| } catch (SftpException e) { | |
| throw new SFTPException("Download [" + sftpParams.getRemoteFilepath() + "] from SFTP " | |
| + sftpConfig.getSftpHost() + ":" + sftpConfig.getSftpPort() + " error.", e); | |
| } | |
| } | |
| /** | |
| * @param sftpParams | |
| * @return | |
| * @throws SFTPException | |
| */ | |
| public InputStream download2InputStream(SFTPParams sftpParams) throws SFTPException { | |
| try { | |
| return channelSftp.get(sftpParams.getRemoteFilepath()); | |
| } catch (SftpException e) { | |
| throw new SFTPException("Download [" + sftpParams.getRemoteFilepath() + "] from SFTP " | |
| + sftpConfig.getSftpHost() + ":" + sftpConfig.getSftpPort() + " error.", e); | |
| } | |
| } | |
| /*============================================other============================================*/ | |
| /** | |
| * @param remotePath | |
| * @return | |
| * @throws SFTPException | |
| */ | |
| public List<String> ls(String remotePath) throws SFTPException { | |
| try { | |
| List<String> remoteFileNameList = new ArrayList<String>(); | |
| Vector ls = channelSftp.ls(remotePath); | |
| for(Object obj:ls){ | |
| ChannelSftp.LsEntry lsEntry = (ChannelSftp.LsEntry) obj; | |
| remoteFileNameList.add(lsEntry.getFilename()); | |
| } | |
| return remoteFileNameList; | |
| } catch (SftpException e) { | |
| throw new SFTPException("List [" + remotePath + "] from SFTP " | |
| + sftpConfig.getSftpHost() + ":" + sftpConfig.getSftpPort() + " error.", e); | |
| } | |
| } | |
| } |
| package com.qzc.helpers.sftp; | |
| /** | |
| * SFTP的基础配置 | |
| * <p> | |
| * Create by qiezhichao on 2018/5/12 0012 21:19 | |
| */ | |
| public class SFTPConfig { | |
| private String sftpHost; // sftp主机地址 | |
| private int sftpPort; // sftp端口 | |
| private String sftpUserName; // sftp用户名 | |
| private String sftpPassword; // sftp密码 | |
| private Integer timeout; // 过期时间 | |
| public SFTPConfig(String sftpHost, int sftpPort, String sftpUserName, String sftpPassword) { | |
| this.sftpHost = sftpHost; | |
| this.sftpPort = sftpPort; | |
| this.sftpUserName = sftpUserName; | |
| this.sftpPassword = sftpPassword; | |
| } | |
| public String getSftpHost() { | |
| return sftpHost; | |
| } | |
| public void setSftpHost(String sftpHost) { | |
| this.sftpHost = sftpHost; | |
| } | |
| public int getSftpPort() { | |
| return sftpPort; | |
| } | |
| public void setSftpPort(int sftpPort) { | |
| this.sftpPort = sftpPort; | |
| } | |
| public String getSftpUserName() { | |
| return sftpUserName; | |
| } | |
| public void setSftpUserName(String sftpUserName) { | |
| this.sftpUserName = sftpUserName; | |
| } | |
| public String getSftpPassword() { | |
| return sftpPassword; | |
| } | |
| public void setSftpPassword(String sftpPassword) { | |
| this.sftpPassword = sftpPassword; | |
| } | |
| public Integer getTimeout() { | |
| return timeout; | |
| } | |
| public void setTimeout(Integer timeout) { | |
| this.timeout = timeout; | |
| } | |
| @Override | |
| public String toString() { | |
| return "SFTPConfig{" + | |
| "sftpHost='" + sftpHost + '\'' + | |
| ", sftpPort=" + sftpPort + | |
| ", sftpUserName='" + sftpUserName + '\'' + | |
| ", sftpPassword='" + sftpPassword + '\'' + | |
| ", timeout=" + timeout + | |
| '}'; | |
| } | |
| } |
| package com.qzc.helpers.sftp; | |
| /** | |
| * Create by qiezhichao on 2018/5/11 0011 21:37 | |
| */ | |
| public class SFTPException extends Exception { | |
| private static final long serialVersionUID = -4539012617666225876L; | |
| private Throwable cause; | |
| public SFTPException(String message) { | |
| super(message); | |
| } | |
| public SFTPException(String message, Throwable e) { | |
| super(message); | |
| this.cause = e; | |
| } | |
| public Throwable getCause() { | |
| return this.cause; | |
| } | |
| } |
| package com.qzc.helpers.sftp; | |
| /** | |
| * SFTP相关参数 | |
| * <p> | |
| * Create by qiezhichao on 2018/5/10 0010 19:36 | |
| */ | |
| public class SFTPParams { | |
| private String localFilepath; // 包含全路径的本地文件名 | |
| private String remoteFilepath; // 包含全路径的远程文件名 | |
| public String getLocalFilepath() { | |
| return localFilepath; | |
| } | |
| public void setLocalFilepath(String localFilepath) { | |
| this.localFilepath = localFilepath; | |
| } | |
| public String getRemoteFilepath() { | |
| return remoteFilepath; | |
| } | |
| public void setRemoteFilepath(String remoteFilepath) { | |
| this.remoteFilepath = remoteFilepath; | |
| } | |
| } |
| package com.qzc.helpers.sftp.monitor; | |
| import com.jcraft.jsch.SftpProgressMonitor; | |
| import java.text.DecimalFormat; | |
| import java.util.concurrent.Executors; | |
| import java.util.concurrent.ScheduledExecutorService; | |
| import java.util.concurrent.TimeUnit; | |
| /** | |
| * Create by qiezhichao on 2018/5/12 0012 12:04 | |
| */ | |
| public class TimerSFTPProgressMonitor implements SftpProgressMonitor { | |
| private boolean isTransEnd = false; // 是否传输完成 | |
| private long fileTotalSize; // 需要传输文件的大小 | |
| private long fileTransferedSize; // 已传输的大小 | |
| private ScheduledExecutorService service = Executors.newScheduledThreadPool(1); | |
| public void init(int op, String src, String dest, long max) { | |
| System.out.println("Begin transferring."); | |
| fileTotalSize = max; | |
| if (fileTotalSize != 0) { | |
| final DecimalFormat df = new DecimalFormat("#.##"); | |
| service.scheduleAtFixedRate(new Runnable() { | |
| public void run() { | |
| if (!isTransEnd) { | |
| if (fileTransferedSize != fileTotalSize) { | |
| double d = ((double) fileTransferedSize * 100) / (double) fileTotalSize; | |
| System.out.println("Current progress: " + df.format(d) + "%"); | |
| } else { | |
| isTransEnd = true; // 已传输大小等于文件总大小,则已完成 | |
| } | |
| } | |
| } | |
| }, 0, 1, TimeUnit.SECONDS); | |
| } | |
| } | |
| public boolean count(final long count) { | |
| fileTransferedSize = fileTransferedSize + count; | |
| return true; | |
| } | |
| public void end() { | |
| service.shutdown(); | |
| System.out.println("End transferring, transferedSize : " + fileTransferedSize); | |
| } | |
| } |
| package com.qzc.helpers; | |
| import org.junit.Test; | |
| import java.io.*; | |
| import java.util.List; | |
| /** | |
| * Create by qiezhichao on 2018/5/12 0012 23:17 | |
| */ | |
| public class TestSFTPHelper { | |
| @Test | |
| public void test1() { | |
| SFTPHelper.uploadOverwrite("X:\\1234.txt", "/upload"); | |
| } | |
| @Test | |
| public void test2() { | |
| SFTPHelper.uploadOverwriteWithTimerMonitor("X:\\004_software\\cn_office_professional_plus_2007_dvd_X12-38713.iso", "/upload/"); | |
| } | |
| @Test | |
| public void test3() throws FileNotFoundException { | |
| SFTPHelper.uploadOverwrite(new FileInputStream(new File("X:\\1234.txt")), "/upload/4321.txt"); | |
| } | |
| @Test | |
| public void test4() throws FileNotFoundException { | |
| SFTPHelper.uploadOverwrite(new FileInputStream(new File("X:\\1234.txt")), 256 * 1024, "/upload/4321.txt"); | |
| } | |
| @Test | |
| public void test5() { | |
| SFTPHelper.uploadAppend("X:\\1234.txt", "/upload/1234.txt"); | |
| } | |
| @Test | |
| public void test6() throws FileNotFoundException { | |
| SFTPHelper.uploadAppend(new FileInputStream(new File("X:\\1234.txt")), "/upload/1234.txt"); | |
| } | |
| @Test | |
| public void test7() throws FileNotFoundException { | |
| SFTPHelper.uploadAppend(new FileInputStream(new File("X:\\1234.txt")), 256 * 1024, "/upload/1234.txt"); | |
| } | |
| @Test | |
| public void test8() { | |
| SFTPHelper.uploadAppendWithTimerMonitor("X:\\1234.txt", "/upload/1234.txt"); | |
| } | |
| @Test | |
| public void test9() { | |
| SFTPHelper.download("/upload/1234.txt", "X:\\4321.txt"); | |
| } | |
| @Test | |
| public void test10() throws IOException { | |
| FileOutputStream fos = new FileOutputStream(new File("X:\\4321.txt")); | |
| SFTPHelper.download(fos, "/upload/1234.txt"); | |
| fos.close(); | |
| } | |
| @Test | |
| public void test11() throws IOException { | |
| FileOutputStream fos = new FileOutputStream(new File("X:\\4321.txt")); | |
| SFTPHelper.downloadWithTimerMonitor(fos, "/upload/1234.txt"); | |
| fos.close(); | |
| } | |
| @Test | |
| public void test12() throws IOException { | |
| FileOutputStream fos = new FileOutputStream(new File("X:\\4321.txt")); | |
| SFTPHelper.downloadWithTimerMonitor("/upload/1234.txt", "X:\\4321.txt"); | |
| fos.close(); | |
| } | |
| @Test | |
| public void test13(){ | |
| List<String> list = SFTPHelper.ls("/upload/"); | |
| for (String s:list){ | |
| System.out.println(s); | |
| } | |
| } | |
| } |
SFTP服务配置以及命令/代码操作的更多相关文章
- rsync 服务配置_rsync命令使用方法
rsync介绍 rsync用来定时备份服务器中的文件或者目录,有三种工作模式,本地复制,使用系统用户认证,守护进程方式,开源高效.同步工具,把一台机器上的文件同步都另一台机器 .默认使用873端口 选 ...
- Linux下 sftp服务配置
查看openssh的版本,使用ssh -V 命令来查看openssh的版本,版本必须大于4.8p1,低于的这个版本需要升级. 参考博客:https://yq.aliyun.com/articles/6 ...
- Linux之sftp服务
Linux之sftp服务 一.sftp介绍转自:[1]Linux如何开启SFTP https://www.cnblogs.com/xuliangxing/p/7120205.htmlSFTP是Secu ...
- Centos6.5---samba文件共享服务配置(二)
Linux-----samba服务配置(二) 需求: 某公司销售部门提出一个文件共享需求,要求部门共享目录有三个,第一个共享目录所有销售部门人员都具有可读可写权限:第二个共享目录所有销售人员只读权限, ...
- OCM_第三天课程:Section1 —》表空间的操作和管理、服务配置
注:本文为原著(其内容来自 腾科教育培训课堂).阅读本文注意事项如下: 1:所有文章的转载请标注本文出处. 2:本文非本人不得用于商业用途.违者将承当相应法律责任. 3:该系列文章目录列表: 一:&l ...
- MySQL 事务配置命令行操作和持久化
MySQL 事务配置命令行操作和持久化 参考 MySQL 官方参考手册 提供 5.5 5.6 5.7 8.0 版本的参考手册 https://dev.mysql.com/doc/refman/5.5/ ...
- Linux设置SFTP服务用户目录权限
我们有时会遇到这样的需求,限制一个Linux用户,让他只能在指定的目录下进行添加.修改.删除操作,并且只能使用sftp登录服务器,不能用ssh操作.这些可以通过配置sftp服务实现. 提供sftp服务 ...
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(九)Linux下安装redis及redis的常用命令和操作
redis简介 Redis是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis与其他key-value缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存 ...
- linux搭建svn服务并手动同步代码到web目录和自动更新
1.安装svn服务端 yum -y install subversion 2.查看安装路径等信息 rpm -ql subversion 3.查看svn帮助信息 svn help 4.创建svn版本库目 ...
随机推荐
- IDEA 相关设置汇总
1.自动提示.代码补全 有时候希望使用自动补全,因为不偷懒的程序员不是好程序员.但是Idea的默认快捷键是 Ctrl + 空格. 对于安装中文输入法的普通人来说那就是杯具了,你懂的. 修改方法如下: ...
- debug错误总结
1, 2,就是一个大括号的问题..让你总是得不了满分..明明和别人的代码差不多. 3,就比如P1914,这种藏坑的题,或者说这一类藏坑的题. 坑是什么呢?就是位数不够往后推的时候.. 你不填坑你就得不 ...
- Tensorflow细节-P89-collection的使用
知识总结 (1)再次注意summary的使用 (2)x = rdm.rand(dataset_size, 2) y_ = [[x1**2 + x2**2] for (x1, x2) in x]这里的问 ...
- C# 打开 EXE 文件
命名空间是using System.Diagnostics; 在编写程序时经常会使用到调用可执行程序的情况,本文将简单介绍C#调用exe的方法.在C#中,通过Process类来进行进程操作. Proc ...
- 2017.10.3 国庆清北 D3T2 公交车
题目描述 LYK在玩一个游戏. 有k群小怪兽想乘坐公交车.第i群小怪兽想从xi出发乘坐公交车到yi.但公交车的容量只有M,而且这辆公交车只会从1号点行驶到n号点. LYK想让小怪兽们尽可能的到达自己想 ...
- [昆仑会员卡系统]老会员数据导入 从临时表插入会员至member_info_svc表 SQL
第一版无UUID版本 从临时表插入会员至member_info_svc表 insert into member_info_svc ( gh_no,chname,sex,birthday,tel,ema ...
- 《挑战30天C++入门极限》C++中类的多态与虚函数的使用
C++中类的多态与虚函数的使用 类的多态特性是支持面向对象的语言最主要的特性,有过非面向对象语言开发经历的人,通常对这一章节的内容会觉得不习惯,因为很多人错误的认为,支持类的封装的语言就是支持 ...
- Maven项目启动失败:class path resource [spring/] cannot be resolved to URL because it does not exist
目录 Maven项目启动失败:class path resource [spring/] cannot be resolved to URL because it does not exist 解决方 ...
- Android 照片上传
解释全在代码中: // 拍照上传 private OnClickListener mUploadClickListener = new OnClickListener() { public void ...
- spark学习记录-1
mapreduce的限制 适合“一趟”计算操作 很难组合和嵌套操作符号 无法表示迭代操作 ======== 由于复制.序列化和磁盘IO导致mapreduce慢 复杂的应用.流计算.内部查询都因为map ...