sftpUtils自己使用

问题描述

import com.google.common.collect.Maps;
import com.google.common.io.CharStreams;
import com.jcraft.jsch.*;
import com.jcraft.jsch.ChannelSftp.LsEntry;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.StringUtil;
import org.springframework.util.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

@Slf4j
public class SftpFileUtil {

    private String host;//服务器连接ip
    private String username;//用户名
    private String password;//密码
    private int port = 22;//端口号

    private ChannelSftp sftp = null;
    private Session sshSession = null;


    public SftpFileUtil(){}

    public SftpFileUtil(String host, int port, String username, String password)
    {
        this.host = host;
        this.username = username;
        this.password = password;
        this.port = port;

        this.connect();
    }

    public SftpFileUtil(String host, String username, String password)
    {
        this.host = host;
        this.username = username;
        this.password = password;
    }

    /**
     * 通过SFTP连接服务器
     */
    public void connect()
    {
        try
        {
            JSch jsch = new JSch();
            jsch.getSession(username, host, port);
            sshSession = jsch.getSession(username, host, port);
            if (log.isInfoEnabled())
            {
                log.info("Session created.");
            }
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            if (log.isInfoEnabled())
            {
                log.info("Session connected.");
            }
            Channel channel = sshSession.openChannel("sftp");
            channel.connect();
            if (log.isInfoEnabled())
            {
                log.info("Opening Channel.");
            }
            sftp = (ChannelSftp) channel;
            if (log.isInfoEnabled())
            {
                log.info("Connected to " + host + ".");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    public void disconnect()
    {
        if (this.sftp != null)
        {
            if (this.sftp.isConnected())
            {
                this.sftp.disconnect();
                if (log.isInfoEnabled())
                {
                    log.info("sftp is closed already");
                }
            }
        }
        if (this.sshSession != null)
        {
            if (this.sshSession.isConnected())
            {
                this.sshSession.disconnect();
                if (log.isInfoEnabled())
                {
                    log.info("sshSession is closed already");
                }
            }
        }
    }

    /**
     * 批量下载文件
     * @param remotePath:远程下载目录(以路径符号结束,可以为相对路径eg:/assess/sftp/jiesuan_2/2014/)
     * @param localPath:本地保存目录(以路径符号结束,D:\Duansha\sftp\)
     * @param fileFormat:下载文件格式(以特定字符开头,为空不做检验)
     * @param fileEndFormat:下载文件格式(文件格式)
     * @param del:下载后是否删除sftp文件
     * @return
     */
    public List<String> batchDownLoadFile(String remotePath, String localPath,
                                          String fileFormat, String fileEndFormat, boolean del)
    {
        List<String> filenames = new ArrayList<String>();
        try
        {
            connect();
            Vector v = listFiles(remotePath);
            // sftp.cd(remotePath);
            if (v.size() > 0)
            {
                System.out.println("本次处理文件个数不为零,开始下载...fileSize=" + v.size());
                Iterator it = v.iterator();
                while (it.hasNext())
                {
                    LsEntry entry = (LsEntry) it.next();
                    String filename = entry.getFilename();
                    SftpATTRS attrs = entry.getAttrs();
                    if (!attrs.isDir())
                    {
                        boolean flag = false;
                        String localFileName = localPath + filename;
                        fileFormat = fileFormat == null ? "" : fileFormat
                                .trim();
                        fileEndFormat = fileEndFormat == null ? ""
                                : fileEndFormat.trim();
                        // 三种情况
                        if (fileFormat.length() > 0 && fileEndFormat.length() > 0)
                        {
                            if (filename.startsWith(fileFormat) && filename.endsWith(fileEndFormat))
                            {
                                flag = downloadFile(remotePath, filename,localPath, filename);
                                if (flag)
                                {
                                    filenames.add(localFileName);
                                    if (flag && del)
                                    {
                                        deleteSFTP(remotePath, filename);
                                    }
                                }
                            }
                        }
                        else if (fileFormat.length() > 0 && "".equals(fileEndFormat))
                        {
                            if (filename.startsWith(fileFormat))
                            {
                                flag = downloadFile(remotePath, filename, localPath, filename);
                                if (flag)
                                {
                                    filenames.add(localFileName);
                                    if (flag && del)
                                    {
                                        deleteSFTP(remotePath, filename);
                                    }
                                }
                            }
                        }
                        else if (fileEndFormat.length() > 0 && "".equals(fileFormat))
                        {
                            if (filename.endsWith(fileEndFormat))
                            {
                                flag = downloadFile(remotePath, filename,localPath, filename);
                                if (flag)
                                {
                                    filenames.add(localFileName);
                                    if (flag && del)
                                    {
                                        deleteSFTP(remotePath, filename);
                                    }
                                }
                            }
                        }
                        else
                        {
                            flag = downloadFile(remotePath, filename,localPath, filename);
                            if (flag)
                            {
                                filenames.add(localFileName);
                                if (flag && del)
                                {
                                    deleteSFTP(remotePath, filename);
                                }
                            }
                        }
                    }
                }
            }
            if (log.isInfoEnabled())
            {
                log.info("download file is success:remotePath=" + remotePath
                        + "and localPath=" + localPath + ",file size is"
                        + v.size());
            }
        }
        catch (SftpException e)
        {
            e.printStackTrace();
        }
        finally
        {
             this.disconnect();
        }
        return filenames;
    }

    /**
     * 批量下载文件
     * @param remotePath:远程下载目录(以路径符号结束,可以为相对路径eg:/assess/sftp/jiesuan_2/2014/)
     * @param fileFormat:下载文件格式(以特定字符开头,为空不做检验)
     * @param fileEndFormat:下载文件格式(文件格式)
     * @param del:下载后是否删除sftp文件
     * @return
     */
    public Map<String, String> batchDownLoadFile(String remotePath, String fileFormat, String fileEndFormat, boolean del)
    {
        Map<String, String> files = Maps.newHashMap();
        try
        {
            connect();
            Vector v = listFiles(remotePath);
            // sftp.cd(remotePath);
            if (v.size() > 0)
            {
                System.out.println("本次处理文件个数不为零,开始下载...fileSize=" + v.size());
                Iterator it = v.iterator();
                while (it.hasNext())
                {
                    LsEntry entry = (LsEntry) it.next();
                    String filename = entry.getFilename();
                    SftpATTRS attrs = entry.getAttrs();
                    if (!attrs.isDir())
                    {
                        String file = null;
//                        String localFileName = localPath + filename;
                        fileFormat = fileFormat == null ? "" : fileFormat
                                .trim();
                        fileEndFormat = fileEndFormat == null ? ""
                                : fileEndFormat.trim();
                        // 三种情况
                        if (fileFormat.length() > 0 && fileEndFormat.length() > 0)
                        {
                            if (filename.startsWith(fileFormat) && filename.endsWith(fileEndFormat))
                            {
                                file = downloadFile(remotePath, filename);
                                if (file != null)
                                {
                                    files.put(filename, file);
                                    if (del)
                                    {
                                        deleteSFTP(remotePath, filename);
                                    }
                                }
                            }
                        }
                        else if (fileFormat.length() > 0 && "".equals(fileEndFormat))
                        {
                            if (filename.startsWith(fileFormat))
                            {
                                file = downloadFile(remotePath, filename);
                                if (file != null)
                                {
                                    files.put(filename, file);
                                    if (del)
                                    {
                                        deleteSFTP(remotePath, filename);
                                    }
                                }
                            }
                        }
                        else if (fileEndFormat.length() > 0 && "".equals(fileFormat))
                        {
                            if (filename.endsWith(fileEndFormat))
                            {
                                file = downloadFile(remotePath, filename);
                                if (file != null)
                                {
                                    files.put(filename, file);
                                    if (del)
                                    {
                                        deleteSFTP(remotePath, filename);
                                    }
                                }
                            }
                        }
                        else
                        {
                            file = downloadFile(remotePath, filename);
                            if (file != null)
                            {
                                files.put(filename, file);
                                if (del)
                                {
                                    deleteSFTP(remotePath, filename);
                                }
                            }
                        }
                    }
                }
            }
            if (log.isInfoEnabled())
            {
                log.info("download file is success:remotePath=" + remotePath
                        + ",file size is"
                        + v.size());
            }
        }
        catch (SftpException e)
        {
            e.printStackTrace();
        }
        finally
        {
             this.disconnect();
        }
        return files;
    }

    /**
     * 下载单个文件
     * @param remotePath:远程下载目录(以路径符号结束)
     * @param remoteFileName:下载文件名
     * @param localPath:本地保存目录(以路径符号结束)
     * @param localFileName:保存文件名
     * @return
     */
    public boolean downloadFile(String remotePath, String remoteFileName,String localPath, String localFileName)
    {
        FileOutputStream fieloutput = null;
        try
        {
            // sftp.cd(remotePath);
            File file = new File(convertFilePath(localPath,localFileName));
            mkdirs(convertFilePath(localPath,localFileName)); //创建本地目录
            fieloutput = new FileOutputStream(file);
            sftp.get(convertFilePath(remotePath,remoteFileName), fieloutput);
            if (log.isInfoEnabled())
            {
                log.info("===DownloadFile:" + remoteFileName + " success from sftp.");
            }
            return true;
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (SftpException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != fieloutput)
            {
                try
                {
                    fieloutput.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 下载单个文件
     * @param remotePath:远程下载目录(以路径符号结束)
     * @param remoteFileName:下载文件名
     * @return 文件内容
     */
    public String downloadFile(String remotePath, String remoteFileName)
    {
        String str = null;
        FileOutputStream fieloutput = null;
        try
        {
            // sftp.cd(remotePath);
            InputStream is = sftp.get(convertFilePath(remotePath ,remoteFileName));
            str = CharStreams.toString(new InputStreamReader(is, StandardCharsets.UTF_8));
            if (log.isInfoEnabled())
            {
                log.info("===DownloadFile:" + remoteFileName + " success from sftp.");
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (SftpException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != fieloutput)
            {
                try
                {
                    fieloutput.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return str;
    }

    /**
     * 上传单个文件
     * @param remotePath:远程保存目录
     * @param remoteFileName:保存文件名
     * @param localPath:本地上传目录(以路径符号结束)
     * @param localFileName:上传的文件名
     * @return
     */
    public boolean uploadFile(String remotePath, String remoteFileName,String localPath, String localFileName)
    {
        FileInputStream in = null;
        try{
            createDir(remotePath);
            File file = new File(convertFilePath(localPath,localFileName));
            in = new FileInputStream(file);
            sftp.put(in, remoteFileName);
            return true;
        }catch (FileNotFoundException e){
            log.info("FileNotFoundException--异常");
            e.printStackTrace();
        }catch (SftpException e){
            log.info("SftpException--异常");
            e.printStackTrace();
        }catch (Exception e){
            log.info("Exception--异常");
            e.printStackTrace();
        }finally{
            if (in != null){
                try{
                    in.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return false;
    }

    /**
     * 批量上传文件
     * @param remotePath:远程保存目录
     * @param localPath:本地上传目录(以路径符号结束)
     * @param del:上传后是否删除本地文件
     * @return
     */
    public boolean bacthUploadFile(String remotePath, String localPath,boolean del){
        boolean flag = true;
        try{
            connect();
            File file = new File(localPath);
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++){
                if (files[i].isFile() && files[i].getName().indexOf("bak") == -1){
                    //通过SFTP上传文件
                    if (this.uploadFile(remotePath, files[i].getName(),localPath, files[i].getName())){
                        //删除单个文件
                        if(del)deleteFile(localPath + files[i].getName());
                    }else{
                        flag = false;
                    }
                }
            }
//            if(flag){
//                //删除对应目录和目录下文件
//                String folder = localPath.substring(0,localPath.indexOf("/",0));
//                if(StringUtils.isNotBlank(folder)){
//                    deleteDirectory(folder);
//                }
//            }

            if (log.isInfoEnabled())
            {
                log.info("upload file is success:remotePath=" + remotePath
                        + "and localPath=" + localPath + ",file size is "
                        + files.length);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            this.disconnect();
        }
        return flag;

    }

    /**
     * 删除本地文件,单个文件
     * @param filePath
     * @return
     */
    public boolean deleteFile(String filePath)
    {
        File file = new File(filePath);
        if (!file.exists())
        {
            return false;
        }

        if (!file.isFile())
        {
            return false;
        }
        boolean rs = file.delete();
        if (rs && log.isInfoEnabled())
        {
            log.info("delete file success from local.");
        }
        return rs;
    }

    /**
     * 删除目录(文件夹)以及目录下的文件
     * @param sPath 被删除目录的文件路径
     * @return 目录删除成功返回true,否则返回false
     */
    public boolean deleteDirectory(String sPath) {
        //如果sPath不以文件分隔符结尾,自动添加文件分隔符
        if (!sPath.endsWith(File.separator)) {
            sPath = sPath + File.separator;
        }
        File dirFile = new File(sPath);
        //如果dir对应的文件不存在,或者不是一个目录,则退出
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        boolean flag = true;
        //删除文件夹下的所有文件(包括子目录)
        File[] files = dirFile.listFiles();
        for (int i = 0; i < files.length; i++) {
            //删除子文件
            if (files[i].isFile()) {
                flag = deleteFile(files[i].getAbsolutePath());
                if (!flag) break;
            } //删除子目录
            else {
                flag = deleteDirectory(files[i].getAbsolutePath());
                if (!flag) break;
            }
        }
        if (!flag) return false;
        //删除当前目录
        if (dirFile.delete()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 创建目录
     * @param createpath
     * @return
     */
    public boolean createDir(String createpath)
    {
        try
        {
            if (isDirExist(createpath))
            {
                this.sftp.cd(createpath);
                return true;
            }
            String pathArry[] = createpath.split("/");
            StringBuffer filePath = new StringBuffer("/");
            for (String path : pathArry)
            {
                if (path.equals(""))
                {
                    continue;
                }
                filePath.append(path + "/");
                if (isDirExist(filePath.toString()))
                {
                    sftp.cd(filePath.toString());
                }
                else
                {
                    // 建立目录
                    sftp.mkdir(filePath.toString());
                    // 进入并设置为当前目录
                    sftp.cd(filePath.toString());
                }

            }
            this.sftp.cd(createpath);
            return true;
        }
        catch (SftpException e)
        {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断目录是否存在
     * @param directory
     * @return
     */
    public boolean isDirExist(String directory)
    {
        boolean isDirExistFlag = false;
        try
        {
            SftpATTRS sftpATTRS = sftp.lstat(directory);
            isDirExistFlag = true;
            return sftpATTRS.isDir();
        }
        catch (Exception e)
        {
            if (e.getMessage().toLowerCase().equals("no such file"))
            {
                isDirExistFlag = false;
            }
        }
        return isDirExistFlag;
    }

    /**
     * 删除stfp文件
     * @param directory:要删除文件所在目录
     * @param deleteFile:要删除的文件
     */
    public void deleteSFTP(String directory, String deleteFile)
    {
        try
        {
            // sftp.cd(directory);
            sftp.rm(directory + deleteFile);
            if (log.isInfoEnabled())
            {
                log.info("delete file success from sftp.");
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 如果目录不存在就创建本地目录
     * @param path
     */
    public void mkdirs(String path)
    {
        File f = new File(path);

        String fs = f.getParent();

        f = new File(fs);

        if (!f.exists())
        {
            f.mkdirs();
        }
    }

    /**
     * 将目录和文件名称按标准拼接
     * @param path
     * @param fileName
     * @return
     */
    private String convertFilePath(String path,String fileName){
        String filePath = null;
        if(!StringUtils.isEmpty(path)){
            if(path.endsWith("/")){
                filePath =  path+fileName;
            }else {
                filePath =  path+"/"+fileName;            }
        }
        return filePath;
    }

    /**
     *  文件移动
     * @param srcSftpFilePath 源文件 路径和名称
     * @param distSftpFilePath 目标文件夹
     * @param fileNameTag  目标文件名称
     * @param separator  分隔符
     * @throws SftpException
     * @throws IOException
     */
    public void moveFile(String srcSftpFilePath, String distSftpFilePath, String fileNameTag,String separator)
            throws SftpException, IOException {
        boolean dirExist;
        boolean fileExist;
        fileExist = isFileExist(srcSftpFilePath);
        dirExist = isFileExist(distSftpFilePath);
        if (!fileExist) {
            //文件不存在直接反回.
            return;
        }
        if (!(dirExist)) {
            //1建立目录
            createDir(distSftpFilePath);
        }

        String fileName = srcSftpFilePath.substring(srcSftpFilePath.lastIndexOf(separator));
        ByteArrayInputStream srcFtpFileStreams = getByteArrayInputStreamFile(srcSftpFilePath);
        if (StringUtils.isEmpty(fileNameTag)) {
            fileName = String.format("%s.%s", fileName, fileNameTag);
        }
        //二进制流写文件
        this.sftp.put(srcFtpFileStreams, distSftpFilePath + fileName);
        this.sftp.rm(srcSftpFilePath);
    }
    /**
     * 获取远程文件字节流。
     *
     * @param sftpFilePath 远程路径.
     * @return ByteArrayInputStream
     * @throws SftpException 异常.
     * @throws IOException 异常.
     */
    public ByteArrayInputStream getByteArrayInputStreamFile(String sftpFilePath) throws SftpException, IOException {
        if (isFileExist(sftpFilePath)) {
            byte[] srcFtpFileByte = inputStreamToByte(getFile(sftpFilePath));
            ByteArrayInputStream srcFtpFileStreams = new ByteArrayInputStream(srcFtpFileByte);
            return srcFtpFileStreams;
        }
        return null;
    }
    /**
     * 下载文件后返回流文件
     */
    public InputStream getFile(String sftpFilePath) throws SftpException {
        if (isFileExist(sftpFilePath)) {
            return sftp.get(sftpFilePath);
        }
        return null;
    }

    public boolean isFileExist(String srcSftpFilePath) {
        boolean isExitFlag = false;
        // 文件大于等于0则存在文件
        if (getFileSize(srcSftpFilePath) >= 0) {
            isExitFlag = true;
        }
        return isExitFlag;
    }
    /**
     * 得到远程文件大小
     *
     * @param srcSftpFilePath 文件路径.
     * @return 返回文件大小,如返回-2 文件不存在,-1文件读取异常
     */
    public long getFileSize(String srcSftpFilePath) {
        long fileSize;//文件大于等于0则存在
        try {
            SftpATTRS sftpATTRS = sftp.lstat(srcSftpFilePath);
            fileSize = sftpATTRS.getSize();
        } catch (Exception e) {
            fileSize = -1;//获取文件大小异常
            if (e.getMessage().toLowerCase().equals("no such file")) {
                fileSize = -2;//文件不存在
            }
        }
        return fileSize;
    }
    /**
     * inputStream类型转换为byte类型.
     *
     * @param inputStream inputStream
     * @return byte
     * @throws IOException 异常.
     */
    public byte[] inputStreamToByte(InputStream inputStream) throws IOException {
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = inputStream.read()) != -1) {
            bytestream.write(ch);
        }
        byte[] imgData = bytestream.toByteArray();
        bytestream.close();
        return imgData;
    }

    /**
     * 列出目录下的文件
     *
     * @param directory:要列出的目录
     * @return
     * @throws SftpException
     */
    public Vector listFiles(String directory) throws SftpException
    {
        return sftp.ls(directory);
    }

    public String getHost()
    {
        return host;
    }

    public void setHost(String host)
    {
        this.host = host;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public int getPort()
    {
        return port;
    }

    public void setPort(int port)
    {
        this.port = port;
    }

    public ChannelSftp getSftp()
    {
        return sftp;
    }

    public void setSftp(ChannelSftp sftp)
    {
        this.sftp = sftp;
    }

    /**测试*/
    public static void main(String[] args)
    {
        SftpFileUtil sftp = null;
        // 本地存放地址
        String localPath = "/tomcat5/webapps/ASSESS/DocumentsDir/DocumentTempDir/txtData/";
        // Sftp下载路径
        String sftpPath = "/home/assess/sftp/jiesuan_2/2014/";
        List<String> filePathList = new ArrayList<String>();
        try
        {
            String folder = localPath.substring(0,localPath.indexOf("/",0));

            sftp = new SftpFileUtil("10.163.201.115", "tdcp", "tdcp");
            sftp.connect();
            // 下载
            sftp.batchDownLoadFile(sftpPath, localPath, "ASSESS", ".txt", true);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            sftp.disconnect();
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)