Sftp连接

public class SftpUtil {
    /**
     * FTP服务器IP地址
     */
    private String host = "192.168.1.147";

    /**
     * FTP服务器端口
     */
    private Integer port = 9999;

    /**
     * FTP服务器登录用户名
     */
    private String username = "test";

    /**
     * FTP服务器登录密码
     */
    private String password = "123456";

    /**
     * FTP连接超时时间
     */
    private Integer timeout = 5000;

//	private String ftpPicUrl="http://192.168.1.147:8086/ciipImg/";

    private String ftpPicPath = "/user/img/";

    public SftpUtil() {
    }

    private Session getSession() {
        JSch jsch = new JSch();
        Session session = null;
        try {
            jsch.getSession(username, host, port);
            session = jsch.getSession(username, host, port);
            session.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            session.setConfig(sshConfig);
            session.connect(timeout);
        } catch (JSchException e) {
            throw new BusinessException("SFTP Session连接JSch异常 !", e);
        } catch (Exception e) {
            throw new BusinessException("SFTP Session连接异常! ", e);
        }
        return session;
    }

    private ChannelSftp getChannel(Session session) {
        ChannelSftp sftp = null;
        try {
            Channel channel = session.openChannel("sftp");
            channel.connect(timeout);
            sftp = (ChannelSftp) channel;
        } catch (JSchException e) {
            throw new BusinessException("SFTP ", e);
        } catch (Exception e) {
            throw new BusinessException("", e);
        }
        return sftp;
    }


    public void upload(InputStream input, AttachFile attach) throws Exception {
        Session session = getSession();
        ChannelSftp sftp = getChannel(session);
        String dateDir = DateFormatUtils.format(new Date(), "yyyy/MM/dd");
        try {
            String fileName = attach.getFileName();
            fileName = attach.getFileId() + fileName.substring(fileName.lastIndexOf("."));
            sftp.cd(ftpPicPath);
            cd(ftpPicPath, dateDir, sftp);
            sftp.put(input, fileName);
            attach.setFileSavePath(ftpPicPath + dateDir + "/" + fileName);
            attach.setFileUrl(dateDir + "/" + fileName);
        } catch (SftpException e) {
            throw new BusinessException("文件上传失败!", e);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            throw e;
        } finally {
            IOUtils.closeQuietly(input);
            session.disconnect();
            sftp.disconnect();
        }
    }

    /**
     * 长传完整名称文件
     *
     * @param input
     * @param attach
     * @throws Exception
     */
    public void uploadWholeName(InputStream input, AttachFile attach) throws Exception {
        Session session = getSession();
        ChannelSftp sftp = getChannel(session);
        String dateDir = DateFormatUtils.format(new Date(), "yyyy/MM/dd");
        try {
            String fileName = java.net.URLEncoder.encode(attach.getFileName(), "utf-8");
//			fileName=attach.getFileId()+fileName.substring(fileName.lastIndexOf("."));
            sftp.cd(ftpPicPath);
            cd(ftpPicPath, dateDir, sftp);
            sftp.put(input, fileName);
            attach.setFileSavePath(ftpPicPath + dateDir + "/" + fileName);
            attach.setFileUrl(dateDir + "/" + fileName);
        } catch (SftpException e) {
            throw new BusinessException("文件上传失败!", e);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            throw e;
        } finally {
            IOUtils.closeQuietly(input);
            session.disconnect();
            sftp.disconnect();
        }
    }


    public void delete(String fileSavePath) {
        Session session = getSession();
        ChannelSftp sftp = getChannel(session);
        try {
            sftp.rm(fileSavePath);
        } catch (SftpException e) {
            throw new BusinessException("文件删除失败!", e);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            throw new BusinessException("Exception", e);
        } finally {
            try {
                session.disconnect();
                sftp.disconnect();
            } catch (Exception e) {
            }
        }
    }

    private void cd(String baseDir, String destDir, ChannelSftp sftp) {
        String[] dirs = destDir.split("/");
        for (String dir : dirs) {
            baseDir = baseDir + dir + "/";
            try {
                sftp.cd(baseDir);
            } catch (SftpException e) {
                try {
                    sftp.mkdir(baseDir);
                    sftp.cd(baseDir);
                } catch (SftpException e1) {
                }
            } catch (Exception e) {
            }
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new SftpUtil().delete("/usr/local/nginx/test.jpg");
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值