sftp文件上传以及下载

/**
     * 上传文件
     *
     * @param directory
     *            上传的目录
     * @param uploadFile
     *            要上传的文件
     */
    public static void uploadFileToSFTP(String host, int port, String username,
                                        String password,String directory, String uploadFile,String newFileName)
            throws Exception {
        Session sshSession =null;
        Channel channel =null;
        ChannelSftp sftp = null;
        try {
            JSch jsch = new JSch();
            jsch.getSession(username, host, port);
            sshSession = jsch.getSession(username, host, port);
            //System.out.println("Session created.");
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect(5000);
            //System.out.println("Session connected.");
            //System.out.println("Opening Channel.");
            channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            //System.out.println("Connected to " + host + ".");

            try {
                sftp.cd(directory);
            }catch(SftpException ex){
                ATTACH_SERVICE_LOG.error("FTP服务器[{}]上目录[{}]可能不存在,需要创建!",host,directory);
                String[] dirs = directory.split("/");
                String tempPath = "";
                for (String dir : dirs) {

                    if (null == dir || "".equals(dir))
                        continue;
                    tempPath+="/"+dir;
                    try {
                        ATTACH_SERVICE_LOG.error("检测目录[" + tempPath + "]");
                        sftp.cd(tempPath);
                    } catch (SftpException ex1) {
                        try {
                            ATTACH_SERVICE_LOG.error("创建目录[" + tempPath + "]");
                            sftp.mkdir(tempPath);
                            sftp.cd(tempPath);
                            ATTACH_SERVICE_LOG.error("进入目录[" + tempPath + "]");
                        } catch (SftpException e1) {
                            ATTACH_SERVICE_LOG.error("创建目录[" + tempPath
                                    + "]失败1,异常信息[" + e1.getMessage() + "]");
                            throw new Exception("创建目录[" + tempPath
                                    + "]失败1,异常信息[" + e1.getMessage() + "]");
                        }
                    }
                }



            }

            File file = new File(uploadFile);
            sftp.put(new FileInputStream(file), newFileName);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally{
            if (sftp !=null){
                sftp.exit();
                sftp.disconnect();
            }
            if (channel !=null){
                channel.disconnect();
            }
            if (sshSession !=null){
                sshSession.disconnect();
            }
        }
    }
    /**
     * 下载文件
     *
     * @param directory
     *            下载目录
     * @param downloadFile
     *            下载的文件
     * @param saveFile
     *            存在本地的路径
     */
    public static  void downloadSFTPFile(String host, int port, String username,String password,
                                     String directory, String downloadFile,String saveFile)
            throws Exception {
        Session sshSession =null;
        Channel channel =null;
        ChannelSftp sftp = null;
        try {
            JSch jsch = new JSch();
            jsch.getSession(username, host, port);
            sshSession = jsch.getSession(username, host, port);
            System.out.println("Session created.");
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            System.out.println("Session connected.");
            System.out.println("Opening Channel.");
            channel = sshSession.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            System.out.println("Connected to " + host + ".");

            sftp.cd(directory);
            File file = new File(saveFile);
            sftp.get(downloadFile, new FileOutputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally{
            if (sftp !=null){
                sftp.exit();
                sftp.disconnect();
            }
            if (channel !=null){
                channel.disconnect();
            }
            if (sshSession !=null){
                sshSession.disconnect();
            }
        }
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值