返回列表 回复 发帖

Oracle 9 中PL/SQL实现FTP功能的文件传输

FTP From PL/SQLSometimes it's preferable to trigger FTP jobs directly from PL/SQL rather than rely on CRON or AT. This article contains a brief description of the two methods I use.

The first method relies on a java stored procedure, described in Shell Commands From PL/SQL, which can be used to trigger a shell script to perform the transfer. The shell script may look like the following:

#! /bin/ksh# Move to appropriate directory on local servercd /extracts# FTP all files in directoryftp -inv ftp.company.com <<EOFuser ftpuser ftppassword# Move to appropriate directory on remote server.cd /loadsasciimput *.*byeEOF
The second approach uses a combination of the UTL_TCP and UTL_FILE packages to create a simple FTP API
( http://www.oracle-base.com/dba/miscellaneous/ftp.pks,
http://www.oracle-base.com/dba/miscellaneous/ftp.pkb).
Once the API is loaded into the appropriate schema simple FTP commands can be initiated as follows:
附件: 您所在的用户组无法下载或查看附件
2005年4月发起成立ERP100.com社区。希望更多的朋友能够融入到ERP100这个大家庭。
努力值相关制度请参考http://bbs.erp100.com/thread-29183-1-1.html
个人空间:http://zonghengsihai.erp100.com
联系方式:huwanjun@baidu.com 13911575376 QQ:27675401
行业:互联网
模块:财务,人力资源,订单,合同,预算,BI(Discoverer)。
主要工作:需求分析,系统设计,项目管理,部门协调。
技术:pl/sql java Oracle 等
所在公司:百度(中国)有限公司 www.baidu.com (2004年6月~至今)
工作地点:北京
相关代码:

  1. CREATE OR REPLACE DIRECTORY my_docs AS '/u01/app/oracle/';
  2. SET SERVEROUTPUT ON SIZE 1000000@c:\ftp.pks@c:\ftp.pkb
  3. -- Retrieve an ASCII file from a remote FTP server.
  4. DECLARE  l_conn  UTL_TCP.connection;
  5. BEGIN  l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');  
  6. ftp.ascii(p_conn => l_conn);  
  7. ftp.get(p_conn      => l_conn,    p_from_file => '/u01/app/oracle/test.txt',    p_to_dir    => 'MY_DOCS',    p_to_file   => 'test_get.txt');  
  8. ftp.logout(l_conn);  utl_tcp.close_all_connections;END;
  9. /
  10. -- Send an ASCII file to a remote FTP server.
  11. DECLARE  l_conn  UTL_TCP.connection;
  12. BEGIN  l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');  
  13. ftp.ascii(p_conn => l_conn);  
  14. ftp.put(p_conn      => l_conn,          p_from_dir  => 'MY_DOCS',    p_from_file => 'test_get.txt',     p_to_file   => '/u01/app/oracle/test_put.txt');  
  15. ftp.logout(l_conn);  utl_tcp.close_all_connections;END;
  16. /
  17. -- Retrieve a binary file from a remote FTP server.
  18. DECLARE  l_conn  UTL_TCP.connection;
  19. BEGIN  l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');  
  20. ftp.binary(p_conn => l_conn);  ftp.get(p_conn      => l_conn,    p_from_file => '/u01/app/oracle/product/9.2.0.1.0/sysman/reporting/gif/jobs.gif',          p_to_dir    => 'MY_DOCS',          p_to_file   => 'jobs_get.gif');  
  21. ftp.logout(l_conn);  utl_tcp.close_all_connections;END;
  22. /
  23. -- Send a binary file to a remote FTP server.
  24. DECLARE  l_conn  UTL_TCP.connection;
  25. BEGIN  l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');  
  26. ftp.binary(p_conn => l_conn);  ftp.put(p_conn   => l_conn,   p_from_dir  => 'MY_DOCS',          p_from_file => 'jobs_get.gif',          p_to_file   => '/u01/app/oracle/jobs_put.gif');  ftp.logout(l_conn);  
  27. utl_tcp.close_all_connections;END;
  28. /
  29. -- Get a directory listing from a remote FTP server.
  30. DECLARE  l_conn  UTL_TCP.connection;  l_list  ftp.t_string_table;
  31. BEGIN  l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');  
  32. ftp.list(p_conn   => l_conn,           p_dir   => '/u01/app/oracle',           p_list  => l_list);  
  33. ftp.logout(l_conn);  utl_tcp.close_all_connections;   
  34. IF l_list.COUNT > 0 THEN    FOR i IN l_list.first .. l_list.last LOOP      
  35. DBMS_OUTPUT.put_line(i || ': ' || l_list(i));   
  36. END LOOP;  
  37. END IF;
  38. END;
  39. /
  40. -- Rename a file on a remote FTP server.
  41. DECLARE  l_conn  UTL_TCP.connection;
  42. BEGIN  l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');  
  43. ftp.rename(p_conn => l_conn,             p_from => '/u01/app/oracle/dba/shutdown',             p_to   => '/u01/app/oracle/dba/shutdown.old');  
  44. ftp.logout(l_conn);  utl_tcp.close_all_connections;
  45. END;

  46. /-- Delete a file on a remote FTP server.
  47. DECLARE  l_conn  UTL_TCP.connection;
  48. BEGIN  l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');  
  49. ftp.delete(p_conn => l_conn,             p_file => '/u01/app/oracle/dba/temp.txt');  
  50. ftp.logout(l_conn);  utl_tcp.close_all_connections;END;


  51. /-- Create a directory on a remote FTP server.
  52. DECLARE  l_conn  UTL_TCP.connection;BEGIN  
  53. l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');  
  54. ftp.mkdir(p_conn => l_conn,            p_dir => '/u01/app/oracle/test');  
  55. ftp.logout(l_conn);  utl_tcp.close_all_connections;
  56. END;

  57. /-- Remove a directory from a remote FTP server.
  58. DECLARE  l_conn  UTL_TCP.connection;
  59. BEGIN  l_conn := ftp.login('ftp.company.com', '21', 'ftpuser', 'ftppassword');  ftp.rmdir(p_conn => l_conn,            p_dir  => '/u01/app/oracle/test');  
  60. f tp.logout(l_conn);  utl_tcp.close_all_connections;
  61. END;
  62. /
复制代码
2005年4月发起成立ERP100.com社区。希望更多的朋友能够融入到ERP100这个大家庭。
努力值相关制度请参考http://bbs.erp100.com/thread-29183-1-1.html
个人空间:http://zonghengsihai.erp100.com
联系方式:huwanjun@baidu.com 13911575376 QQ:27675401
行业:互联网
模块:财务,人力资源,订单,合同,预算,BI(Discoverer)。
主要工作:需求分析,系统设计,项目管理,部门协调。
技术:pl/sql java Oracle 等
所在公司:百度(中国)有限公司 www.baidu.com (2004年6月~至今)
工作地点:北京
Thanks for sharing ^_^
Can't understand method 1;
And will have a try about approch 2 when I get the opportunity...
It must be very practical,TKS again :)
thank you for your sharing ,
学习了,谢谢,好东东。。
下了,谢谢楼主
automate things ... thanks for sharing
返回列表