SFTP上传下载

1、导入jar包 ora-sftp-1.4.jar

2、编译package和执行sql文件   cux_1_pm_sftp_util.pck, ORA_SFTP_FILE.sql

3、上传文件

/*===============================================
  *   PROCEDURE NAME:
  *       Upload_Sftp
  *
  *   DESCRIPTION:
  *       上传sftp
  *   HISTORY:
  *     1.00   2020-07-02   wang.chen   Creation
  * ==============================================*/
  PROCEDURE Upload_Sftp(p_Org_Id      IN NUMBER --公司ID
                       ,p_Doc_Name    IN VARCHAR2 --文件名称
                       ,p_Blob        IN BLOB --文件BLOB
                       ,x_Ret_Status  OUT VARCHAR2 --成功返回S
                       ,x_Ret_Message OUT VARCHAR2 --错误信息
                        ) IS
    l_Connection_Id   NUMBER;
    l_File_Name       VARCHAR2(240);
    l_File_Path       VARCHAR2(240);
    l_File_Path1      VARCHAR2(240);
    l_Upload_Path     VARCHAR2(240);
    l_File_Exists     NUMBER;
    l_Downloaded_File BLOB;
  BEGIN
    --初始化  
    x_Ret_Status  := Fnd_Api.g_Ret_Sts_Success;
    x_Ret_Message := NULL;
  
    --连接文件服务器
    BEGIN
      l_Connection_Id := Cux_1_Pm_Sftp_Util.Connect_Host(''
                                                        ,22
                                                        ,''
                                                        ,'');
    EXCEPTION
      WHEN OTHERS THEN
        l_Connection_Id := NULL;
        x_Ret_Status    := Fnd_Api.g_Ret_Sts_Error;
        x_Ret_Message   := x_Ret_Message || SQLERRM;
        RETURN;
    END;
  
    l_File_Path  := '/pmsfiles/test/upload/b01/' || p_Org_Id || '/' ||
                    p_Doc_Name;
    l_File_Path1 := '/pmsfiles/test/upload/b01/' || p_Org_Id || '/';
    --验证文件是否存在
    BEGIN
      IF Cux_1_Pm_Sftp_Util.Exists(l_Connection_Id
                                  ,l_File_Path1) THEN
        NULL;
      ELSE
        --若文件不存在则创建目录文件
        Cux_1_Pm_Sftp_Util.Create_Dir(l_Connection_Id
                                     ,l_File_Path1);
      END IF;
    EXCEPTION
      WHEN OTHERS THEN
        x_Ret_Status  := Fnd_Api.g_Ret_Sts_Error;
        x_Ret_Message := x_Ret_Message || SQLERRM;
        RETURN;
    END;
  
    --上传
    BEGIN
      Cux_1_Pm_Sftp_Util.Upload(l_Connection_Id
                               ,p_Blob
                               ,l_File_Path);
    EXCEPTION
      WHEN OTHERS THEN
        x_Ret_Status  := Fnd_Api.g_Ret_Sts_Error;
        x_Ret_Message := x_Ret_Message || SQLERRM;
        RETURN;
    END;
  
    Ora_Sftp.Disconnect_Host(l_Connection_Id);
  
  EXCEPTION
    WHEN OTHERS THEN
      x_Ret_Status  := Fnd_Api.g_Ret_Sts_Error;
      x_Ret_Message := x_Ret_Message || SQLERRM;
  END Upload_Sftp;

4、下载文件

 /*===============================================
  *   PROCEDURE NAME:
  *       Download_Sftp
  *   DESCRIPTION:
  *       下载sftp
  *   HISTORY:
  *     1.00   2020-07-01   wang.chen   Creation
  * ==============================================*/
  PROCEDURE Download_Sftp(p_File_Id     IN NUMBER --文件id
                         ,x_Blob        OUT BLOB --文件BLOB
                         ,x_Ret_Status  OUT VARCHAR2 --成功返回S
                         ,x_Ret_Message OUT VARCHAR2 --错误信息
                          ) IS
  
    l_Connection_Id   NUMBER;
    l_File_Name       VARCHAR2(240);
    l_File_Path       VARCHAR2(240);
    l_Upload_Path     VARCHAR2(240);
    l_File_Exists     NUMBER;
    l_Downloaded_File BLOB;
  BEGIN
    --初始化  
    x_Ret_Status  := Fnd_Api.g_Ret_Sts_Success;
    x_Ret_Message := NULL;
  
    --连接文件服务器
    BEGIN
      l_Connection_Id := Cux_1_Pm_Sftp_Util.Connect_Host('101.01.197.411'
                                                        ,22
                                                        ,'123456'
                                                        ,'111111');
    EXCEPTION
      WHEN OTHERS THEN
        l_Connection_Id := NULL;
        x_Ret_Status    := Fnd_Api.g_Ret_Sts_Error;
        x_Ret_Message   := x_Ret_Message || SQLERRM;
        RETURN;
    END;
  
    --根据文件id获取文件名称,文件路径
    BEGIN
      SELECT f.File_Name
            ,f.Upload_Path
        INTO l_File_Name
            ,l_Upload_Path
        FROM Cux_1_Pm_Upload_Files f
       WHERE 1 = 1
         AND f.File_Id = p_File_Id;
    EXCEPTION
      WHEN OTHERS THEN
        l_File_Name   := NULL;
        l_Upload_Path := NULL;
        x_Ret_Status  := Fnd_Api.g_Ret_Sts_Error;
        x_Ret_Message := x_Ret_Message || '根据文件id找不到文件名称和目录!';
        RETURN;
    END;
  
    l_File_Path := l_Upload_Path || l_File_Name;
    --验证文件是否存在
    BEGIN
      IF Cux_1_Pm_Sftp_Util.Exists(l_Connection_Id
                                  ,l_File_Path) THEN
        NULL;
      ELSE
        x_Ret_Status  := Fnd_Api.g_Ret_Sts_Error;
        x_Ret_Message := x_Ret_Message || '文件服务器下文件不存在!';
        RETURN;
      END IF;
    EXCEPTION
      WHEN OTHERS THEN
        x_Ret_Status  := Fnd_Api.g_Ret_Sts_Error;
        x_Ret_Message := x_Ret_Message || SQLERRM;
        RETURN;
    END;
  
    --下载
    BEGIN
      l_Downloaded_File := Cux_1_Pm_Sftp_Util.Download(l_Connection_Id
                                                      ,l_File_Path);
      IF (l_Downloaded_File IS NULL) THEN
        x_Ret_Status  := Fnd_Api.g_Ret_Sts_Error;
        x_Ret_Message := x_Ret_Message || '下载的文件为空';
        RETURN;
      END IF;
    EXCEPTION
      WHEN OTHERS THEN
        x_Ret_Status  := Fnd_Api.g_Ret_Sts_Error;
        x_Ret_Message := x_Ret_Message || SQLERRM;
        RETURN;
    END;
  
    x_Blob := l_Downloaded_File;
  
    Cux_1_Pm_Sftp_Util.Disconnect_Host(l_Connection_Id);
  
  EXCEPTION
    WHEN OTHERS THEN
      x_Ret_Status  := Fnd_Api.g_Ret_Sts_Error;
      x_Ret_Message := x_Ret_Message || SQLERRM;
  END Download_Sftp;

 

posted @ 2020-07-02 14:16  旺仔丶小馒头  阅读(294)  评论(0编辑  收藏  举报