java實現sftp文件上傳下載


/**
     * 
     * @param filePath 文件全路徑 
     * @param ftpPath 上傳到目的端目錄
     * @param username
     * @param password
     * @param host
     * @param port
     */
    public static void uploadFile(String filePath, String ftpPath, String username, String password, String host, Integer port) {
        FileInputStream input = null;
        ChannelSftp sftp = null;
        try {
            JSch jsch = new JSch();
            //獲取session  賬號-ip-端口
            com.jcraft.jsch.Session sshSession = jsch.getSession(username, host, port);
            //添加密碼
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            //嚴格主機密鑰檢查
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            //開啟session連接
            sshSession.connect();
            //獲取sftp通道
            sftp = (ChannelSftp) sshSession.openChannel("sftp");
            //開啟
            sftp.connect();
            //文件亂碼處理
            /*Class<ChannelSftp> c = ChannelSftp.class;
            Field f = c.getDeclaredField("server_version");
            f.setAccessible(true);
            f.set(sftp, 2);
            sftp.setFilenameEncoding("GBK");*/
            //判斷目錄是否存在
            try {
                Vector ls = sftp.ls(ftpPath); //ls()得到指定目錄下的文件列表
                /*if (ls == null) {   //ls不會為null,哪怕它是一個空目錄
                    sftp.mkdir(ftpPath);
                }*/
            } catch (SftpException e) {
                sftp.mkdir(ftpPath);
            }
            sftp.cd(ftpPath);
            String filename = filePath.substring(filePath.lastIndexOf(File.separator) + 1); //附件名字
            //filename = new String(filename.getBytes("GBK"), StandardCharsets.ISO_8859_1);
            input = new FileInputStream(new File(filePath));
            sftp.put(input, filename);
            //設定777權限,轉為8進制放入chmod中
            //sftp.chmod(Integer.parseInt("777", 8), ftpPath + filename);
            input.close();
            sftp.disconnect();
            sshSession.disconnect();
            System.out.println("================上傳成功!==================");
        } catch (Exception e) {
            System.out.println("================上傳失敗!==================");
            e.printStackTrace();
        }
    }

    /**
     * @param directory    SFTP服務器的文件路徑
     * @param downloadFile SFTP服務器上的文件名
     * @param saveFile     保存到本地路徑
     * @param username
     * @param password
     * @param host
     * @param port
     */
    public static void downloadFile(String directory, String downloadFile, String saveFile, String username, String password, String host, Integer port) {
        ChannelSftp sftp = null;
        try {
            JSch jsch = new JSch();
            //獲取session  賬號-ip-端口
            com.jcraft.jsch.Session sshSession = jsch.getSession(username, host, port);
            //添加密碼
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            //嚴格主機密鑰檢查
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            //開啟session連接
            sshSession.connect();
            //獲取sftp通道
            sftp = (ChannelSftp) sshSession.openChannel("sftp");
            //開啟
            sftp.connect();
            if (directory != null && !"".equals(directory)) {
                sftp.cd(directory);
            }
            FileOutputStream output = new FileOutputStream(new File(saveFile));
            sftp.get(downloadFile, output);
            output.close();
            sftp.disconnect();
            sshSession.disconnect();
            System.out.println("================下載成功!==================");
        } catch (SftpException | FileNotFoundException | JSchException e) {
            log.error("文件下載異常!", e);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM