What would be the file location on server.
Nisheeth Mitharwal, December 30, 2002 - 1:32 pm UTC
Tom your response was useful. Thanks. one more doubt TOM.
i am dropping that file on remote server say
i want file on batch/xml folder.i do this using
v_bytes_written := UTL_TCP.write_line(con,'CWD /batch/xml');
v_bytes_written := UTL_TCP.write_line(con,'STOR file.xml');
now can i mention the file location from where i want to pick the file.
for ex: oracle server has F drive connected and i want to pick file from that particular drive(F) and and send it to /batch/xml folder of remote server.
How do i mention this using store command? Do i need to put that file on some default directory of local server and ftp process will know from where to pick up that file.
Thaks in advance TOM.
December 30, 2002 - 1:45 pm UTC
but now, after doing the STOR command -- your plsql routine has to get the file to ftp server.
the ftp server doesn't just magically "get the file", it has to be transmitted. STOR doesn't move a file. STOR just says "hey, I'm going to send you a file later, this is what and where I would like you to PUT IT when you get it"...
The ftp "process" is YOU -- you are the ftp process! you would have to open a connection to the server again and send it the file. The STOR command doesn't send anything, you have to.
This is NOT something you will be doing in PLSQL very easily
If you attemp -- you will need to know exactly how to implement the ftp protocol -- you are only halfway there!
How do we implement FTP process in PL/sql?
Nisheeth Mitharwal, December 30, 2002 - 1:56 pm UTC
Thanks TOM for answer.
Tom,I thought STOR works as standard put command. Now how would i implement FTP process in oracle. Can you suggest me how can we actually transfer files using ftp in PL/SQL.
Thanks in advace.
December 30, 2002 - 2:56 pm UTC
PUT is a command to a ftp client (which is what you are writing). It tells the ftp client "hey, tell the ftp server where I want it to put this file -- that is the STOR command and then actually SEND the file for me". That is the part you are missing -- you need to send the file, you need to understand the ftp protocol and stream the file yourself
I am suggesting you do not use PLSQL, I am suggesting that java, with existing ftp classes, would be an easier approach.
Short of that -- look up the ftp spec on the web and code the entire thing from scratch.
how to implement FTP in java stored procedure
Nisheeth Mitharwal, December 30, 2002 - 4:08 pm UTC
Thanks Tom,
your response was very useful.Now i am thinking of doing this using Java stored procedure. Tom can you send me some sample code for FTP in java stored proc. I am new to orcale and java. How to call java stored proc from oracle and what is the archirecture.
Thanks in advance.
December 30, 2002 - 4:40 pm UTC
here is an example -- it does a get, i don't have a put - but it won't be much different. You'll want to "bfile" the data to PUT (so it is a blob) to minimize the code changes:
set define off
create or replace and compile java source named "my_ftp" as
import java.io.*;
import sun.net.ftp.*;
import oracle.sql.*;
public class my_ftp {
public static void get(
String host,
NUMBER port,
String username,
String password,
String sourceFile,
BLOB target,
String proxyHost,
NUMBER proxyPort) throws Exception {
OutputStream os = target.getBinaryOutputStream();
FtpClient ftpClient = null;
try {
int lastSlash = sourceFile.lastIndexOf('/');
String file = sourceFile.substring(lastSlash + 1);
String directory = sourceFile.substring(0, lastSlash);
// Turn on proxy support when requested.
if (proxyHost != null && proxyHost.trim().length() > 0 && proxyPort != null) {
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", proxyHost.trim());
System.getProperties().put("proxyPort", proxyPort.stringValue());
}
ftpClient = new FtpClient(host, port.intValue());
ftpClient.login(username, password);
ftpClient.binary();
ftpClient.cd(directory);
InputStream is = ftpClient.get(file);
int length;
byte[] buffer = new byte[target.getChunkSize()];
while((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
} finally {
if (ftpClient != null) {
ftpClient.closeServer();
}
}
}
}
/
show errors
create or replace procedure FTP_GET(
p_host in varchar2,
p_port in number,
p_username in varchar2,
p_password in varchar2,
p_source_file in varchar2,
p_target_blob in BLOB,
p_proxy_host in varchar2,
p_proxy_port in number)
as language java name 'my_ftp.get(
java.lang.String,
oracle.sql.NUMBER,
java.lang.String,
java.lang.String,
java.lang.String,
oracle.sql.BLOB,
java.lang.String,
oracle.sql.NUMBER)';
/
show errors
set serveroutput on size 1000000
exec dbms_java.set_output(1000000)
declare
file_data blob;
file_name varchar2(255) := '/export/home/tkyte/tmp/tford.dbg';
begin
insert into lob_test(id, filename, title, data)
values(2, file_name, 'Terry Ford debug file', empty_blob)
returning data into file_data;
ftp_get(
'aria.us.oracle.com',
21,
'mbpierma',
'password',
file_name,
file_data,
'www-proxy.us.oracle.com',
0);
end;
/
getting Java exception: sun.net.ftp.FtpProtocolException: PORT
nisheeth mitharwal, December 31, 2002 - 9:39 am UTC
Tom,
I implemented the put method using sun.net.ftp.* and getting some exceptions.
some of them were for granting permissions and resolved using
begin
dbms_java.grant_permission( 'ENROLL', 'SYS:java.net.SocketPermission', 'remote_server', 'resolve');
dbms_java.grant_permission( 'ENROLL', 'SYS:java.net.SocketPermission', 'IP address:21', 'connect,resolve');
dbms_java.grant_permission( 'ENROLL', 'SYS:java.net.SocketPermission', 'localhost:1024-', 'listen,resolve' );
exception
when others then
dbms_output.put_line(sqlerrm);
end;
still i am getting this exception
Java exception: sun.net.ftp.FtpProtocolException: PORT
my code is
create or replace and compile java source named "ftp_utility" as
import java.io.*;
import sun.net.ftp.*;
import oracle.sql.*;
public static void put(
String host,
NUMBER port,
String username,
String password,
String sourceFile,
String remoteDirectory ) throws Exception
{
FtpClient ftpClient = null;
try
{
int lastSlash = sourceFile.lastIndexOf('/');
String file = sourceFile.substring(lastSlash + 1);
String directory = sourceFile.substring(0, lastSlash);
ftpClient = new FtpClient(host, port.intValue());
ftpClient.login(username, password);
ftpClient.cd(remoteDirectory);
ftpClient.put(sourceFile);
}
finally
{
if (ftpClient != null)
{
ftpClient.closeServer();
}
}
}
sourcefile variable has local path in it as
'F:/ftp/sourcefile.txt'
could you please help me in this...
am i using the right class...
how can i set the mode(active or passive) using this class....
thanks in advance
December 31, 2002 - 10:04 am UTC
Sorry, not a heavy duty java programmer myself. You'll need to goto the java groups to see if/how to set passive on the connection.
As you give no real error message, and an imcomplete example (eg: no plsql mapping and such to your java routine) -- I cannot tell you what the PORT issue is -- I don't know what values you are using for port for example.
Getting exceptions
nisheeth mitharwal, December 31, 2002 - 10:14 am UTC
sorry Tom for incomplete information
here is complete code...
create or replace and compile java source named "ftp_utility" as
import java.io.*;
import sun.net.ftp.*;
import oracle.sql.*;
public class ftp_utility {
public static void put(
String host,
NUMBER port,
String username,
String password,
String sourceFile,
String remoteDirectory ) throws Exception
{
FtpClient ftpClient = null;
try
{
int lastSlash = sourceFile.lastIndexOf('/');
String file = sourceFile.substring(lastSlash + 1);
String directory = sourceFile.substring(0, lastSlash);
ftpClient = new FtpClient(host, port.intValue());
ftpClient.login(username, password);
ftpClient.cd(remoteDirectory);
ftpClient.put(sourceFile);
}
finally
{
if (ftpClient != null)
{
ftpClient.closeServer();
}
}
}
}
/
Procedure PRC_FTP_PUT (
p_host in varchar2,
p_port in number,
p_username in varchar2,
p_password in varchar2,
p_source_file in varchar2,
p_remoteDirectory in varchar2)
as language java name 'ftp_utility.put(
java.lang.String,
oracle.sql.NUMBER,
java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String)';
/
PRC_FTP_PUT(p_ftp_server,21,p_userid,p_pwd,'F:/xml/test.xml','/batchxml/init');
actual error message i am getting is
ORA-29532: Java call terminated by uncaught Java exception: sun.net.ftp.FtpProtocolException: PORT
Thanks
Still getting the same error...
Nisheeth Mitharwal, January 02, 2003 - 6:02 pm UTC
Tom,
Happy new year.
I have checked the server. It seems there is no firewall problem.
Could you please help me further finding out what could be wrong.I wasn't able to find out FTPCliet which supports passive connections.
Thanks in advance Tom for your support.
happy new year
Nisheeth
January 02, 2003 - 6:32 pm UTC
sorry -- i cannot reproduce.
how did you check the server?
goto the SERVER and try to ftp the the client side -- see if an inbound ftp connection is permitted.
You're exceeding my limited knownledge of the FTP protocol. Your code worked "as is" for me.
How to set permission using DBMS_JAVA package?
Nisheeth Mitharwal, January 03, 2003 - 10:33 am UTC
Tom,
There is no firewall i am sure and one more thing i ran that java progaram (not from otacle) form my machine and i ma able to FTP to the server.
Does this mean that oracle srevr is having some access permission probelm to remote server.?
Yesterday i granted these permission to server.
Can you tell me if there is any thing else i need to grant to get rid of PORT exception.
begin
dbms_java.grant_permission( 'ENROLL', 'SYS:java.net.SocketPermission', 'test-mdsftp.humana.com', 'resolve');
dbms_java.grant_permission( 'ENROLL', 'SYS:java.net.SocketPermission', 'test-mdsftp.humana.com:20', 'connect,resolve');
dbms_java.grant_permission( 'ENROLL', 'SYS:java.net.SocketPermission', 'test-mdsftp.humana.com:21', 'connect,resolve');
dbms_java.grant_permission( 'ENROLL', 'SYS:java.net.SocketPermission', 'localhost:1024-', 'listen,resolve' );
exception
when others then
dbms_output.put_line(sqlerrm);
end;
Thanks in advance.
Nisheeth
January 03, 2003 - 11:21 am UTC
grant yourself javasyspriv and retry.
A reader, January 03, 2003 - 12:12 pm UTC
Thanks Tom it worked...
you have been very helpful Tom.
Thanks again
Nisheeth
Not able to FTP form oracle server.
Ted Smith, October 10, 2003 - 1:46 pm UTC
I used nisheet mitharwal complete code in this document and
set the dbms_java as mentioned, but I am receiving the following error: Java exception: sun.net.ftp.FtpProtocolException: STOR. Can you assist me in resolving this error. We are useing a 8i database.
Thanks,
Ted
October 10, 2003 - 4:22 pm UTC
hows about the entire stack (error stack)
java works outside database but not inside
Tom, November 29, 2003 - 12:08 pm UTC
Hi Tom,
I have looked at the code you wrote using the sun ftp classes and re-implemented using the jakarta commons net libraries [www.jakarta.org] for the FTPClient.
The following works fine if called outside the database using the main method, but hangs the procedure when called from inside the database [never terminates].
Any ideas?
Code follows
JAVA CLASS
----------
import org.apache.commons.net.ftp.*;
import java.io.*;
public class FtpHelper{
private static FTPClient ftp = new FTPClient();
public static final void main(String[] args){
try{
getFile();
}catch(Exception e){
e.printStackTrace(System.err);
}
}
public static void getFile() throws Exception{
try{
int reply;
//connect to the server
ftp.connect("ftp.yourname.co.uk");
//get the reply code
reply = ftp.getReplyCode();
//if we connected successfully
if(FTPReply.isPositiveCompletion(reply)){
//login
if(ftp.login("username","password")){
//text file download
ftp.setFileType(FTP.ASCII_FILE_TYPE);
//behind firewall so use passive connection
ftp.enterLocalPassiveMode();
//create a new file
File file = new File("c:\\downloaded.html");
//get an outputstream
FileOutputStream out = new FileOutputStream(file);
//read the file
if(!ftp.retrieveFile("file.txt",out)){
throw(new Exception("Didn't get file"));
}
//flush the stream
out.flush();
out.close();
//logout and disconnect
ftp.logout();
ftp.disconnect();
}else{
throw(new Exception("invalid logon"));
}
}else{
throw(new Exception("unable to connect"));
}
}catch(Exception e){
try{
ftp.logout();
ftp.disconnect();
}catch(Exception logoutexception){ throw(new Exception("Couldn't logout");}
}
}
}
PLSQL Wrapper
-------------
create or replace package utlFtp IS
PROCEDURE getFile;
end utlFtp;
/
create or replace package body utlFtp IS
PROCEDURE getFile
AS LANGUAGE JAVA
NAME 'FtpHelper.getFile()';
end utlFtp;
November 29, 2003 - 1:01 pm UTC
nope, no idea. don't write too much java, get a headache reading it.
guess I would put some debug in there (system.out.printlns will be written to a trace file on the server by default) to see where it gets stuck -- or calls to dbms_application_info.set_client_info so you can query v$session to see what "step" it is on.
my_ftp.get
Faisal, February 11, 2004 - 11:07 am UTC
Tom i created my_ftp java procedure as you mentioned in your code its created successfully. but when i run the procedure it give me following error.
ERROR at line 1:
ORA-29532: Java call terminated by uncaught Java exception:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
ORA-06512: at "PSDS.FTP_GET", line 0
ORA-06512: at line 8
here is the block code:
set serveroutput on size 1000000
exec dbms_java.set_output(1000000)
declare
file_data blob;
file_name varchar2(255) := '\ftp-java.txt';
begin
insert into lob_test(id, filename, title, data)
values(2, file_name, 'ftp test', empty_blob)
returning data into file_data;
ftp_get('myftpsite.co.uk',21,'username','pswd',
file_name, file_data, '', 0);
end;
/
please can you help me.
February 11, 2004 - 12:42 pm UTC
code seems to be looking for '/'
you seem to be sending '\'
try '/' or update the code to look for 'windows' stuff.
fTP
M Faisal, February 16, 2004 - 7:36 am UTC
Thanks TOM its work. Thanks for help.
For those interested...
Kashif, March 11, 2004 - 11:26 am UTC
Where are these articles?
Thomas Ko, April 27, 2005 - 12:46 pm UTC
I cannot find the articles that Kashif mentioned from the Metalink. Does anyone have copy of them, and why they were pulled from the Metalink?
Thanks.
Thomas
FTP in Plsql
Pretaish, February 09, 2006 - 7:33 am UTC
Hi Tom,
Even after going through this page I am not very sure I grasped it. Is it possible to transfer a file from server to client using some built-in package of Plsql ( maybe through utl_tcp... I am not too sure) ? If yes, could you please demonstrate this through a small piece of code?
Thanks in Advance
Pretaish
February 10, 2006 - 11:05 am UTC
Auto FTP via CRON job
Dawar Naqvi, February 20, 2006 - 11:02 am UTC
Tom,
Currently via cron job setup I am doing our RMAN backup every day at mid night, I am also export one of our main schema every morning at 1.00 am via cron job.
After that every morning, I need to manually transfer two weeks old files from our database server to the backup server through FTP.
Then I remove old file from database server.
Now I want to do this FTP process also automatically via cron job.
Please note:
we need transfer only two weeks old files from the database server and delete after successful ftp from database server.
Each time 6 files created by Backup process.
And plus one .dmp file.
Do I need to use any plsql for it?
I need your comments. thanks
cheers,
Dawar
February 20, 2006 - 12:25 pm UTC
while you can ftp from plsql, your cron job is running in the OS.
In order to automate an ftp in a script,
$ man netrc
Auto FTP via CRON job
Dawar, February 20, 2006 - 2:36 pm UTC
My OS is AIX 5.2 --IBM
Here what I get
>man netrc
>Manual entry for netrc not found or not installed.
Man has installed on the system.
I can use for other stuff such as passwd, domain etc.
February 21, 2006 - 6:52 am UTC
google it then. it is a file, .netrc, you can put ftp auto login things in it.
FTP via PL/SQL
Barry C, April 19, 2006 - 8:58 pm UTC
As stated there is a lot to using UTL_TCP for FTP. In the package solution that I have put together it has a built timeout feature to prevent hanging sessions when the server either doesn't respond or a path is invalid. I would be happy to post here but its fairly large package. For thus interested feel free to email me at bsc7080mqc@mylxhq.com and I can email it to you.
Supports
- PUT, GET, REMOVE, RENAME
- SERVER UP VERIFICATION
- BINARY and ASCII SUPPORT
- USES UTL_TCP
- REQUIRES 9iR2+
- BUILT-IN TIMEOUT of 4 MINUTES
- PL/SQL BASED
FTP in PL/SQL
sujit, June 12, 2006 - 3:21 pm UTC
Tom, here is what i did,
1) I compiled this. It went fine
create or replace and compile java source named "ftp_utility" as
import java.io.*;
import sun.net.ftp.*;
import oracle.sql.*;
public class ftp_utility {
public static void put(
String host,
NUMBER port,
String username,
String password,
String sourceFile,
String remoteDirectory ) throws Exception
{
FtpClient ftpClient = null;
try
{
int lastSlash = sourceFile.lastIndexOf('/');
String file = sourceFile.substring(lastSlash + 1);
String directory = sourceFile.substring(0, lastSlash);
ftpClient = new FtpClient(host, port.intValue());
ftpClient.login(username, password);
ftpClient.cd(remoteDirectory);
ftpClient.put(sourceFile);
}
finally
{
if (ftpClient != null)
{
ftpClient.closeServer();
}
}
}
}
/
2) I compiled the following. it went fine
CREATE OR REPLACE procedure sp_ftp_utility_PUT (
p_host in varchar2,
p_port in number,
p_username in varchar2,
p_password in varchar2,
p_source_file in varchar2,
p_remoteDirectory in varchar2)
as language java name 'ftp_utility.put(
java.lang.String,
oracle.sql.NUMBER,
java.lang.String,
java.lang.String,
java.lang.String,
java.lang.String)';
/
3) did grant...it went fine
begin
dbms_java.grant_permission( 'DEV', 'SYS:java.net.SocketPermission', '169.2.2.12:21', 'connect,resolve' );
exception
when others then
dbms_output.put_line(sqlerrm);
end;
4) I get the following error when I run the following plsql block
DEV>declare
2 file_data varchar2(255) := '/data/home/whited';
3 file_name varchar2(255) := 'c:/list.txt';
4 begin
5 --insert into lob_test(id, filename, title, data)
6 --values(2, file_name, 'Terry Ford debug file', empty_blob)
7 --returning data into file_data;
8
9 sp_ftp_utility_PUT(
10 '169.2.2.12',
11 21,
12 'whited',
13 'password',
14 file_name,
15 file_data
16 );
17 end;
18 /
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java)
at java.net.Socket.connect(Socket.java)
at java.net.Socket.connect(Socket.java)
at sun.net.NetworkClient.doConnect(NetworkClient.java)
at sun.net.NetworkClient.openServer(NetworkClient.java)
at sun.net.ftp.FtpClient.openServer(FtpClient.java)
at sun.net.ftp.FtpClient.<init>(FtpClient.java)
at ftp_utility.put(ftp_utility:23)
declare
*
ERROR at line 1:
ORA-29532: Java call terminated by uncaught Java exception:
java.net.ConnectException: Connection refused
ORA-06512: at "DEV.SP_FTP_UTILITY_PUT", line 1
ORA-06512: at line 9
Please help.
Thanks in advance.
June 13, 2006 - 10:52 am UTC
connection was refused, are you sure you can even ftp to that machine
FTP using PL/SQL
A reader, July 27, 2007 - 9:59 am UTC
Hi Tom,
I have a requirement like this:
Using a html interface, we will specify the
folder name and attach a file to be created
in that folder. Assuming that the folder is already created,
how can we do this using pl/sql?
Thanks in advance.
July 27, 2007 - 3:14 pm UTC
huh? no idea what you mean.
we go from ftp using plsql to we are writing some html that'll get a folder name?
PASV mode FTP put
Stefan P Knecht, August 02, 2007 - 9:17 am UTC
It would actually be quite simple to implement.
USER tkyte
331 Password required for tkyte.
PASS xxxx
230 User tkyte logged in.
PASV
227 Entering Passive Mode (138,2,5,51,179,70)
Once you get the pasv reply from the server, all you need to do is open a connection using the details the server gives you. In the above example, the first 4 digits are the ip address (138.2.5.51) and 45715 would be the port (4th digit times 255 + 5th digit; 179 * 255 + 70). Then just use utl_file to open the file locally (in raw), and utl_tcp to write it to the new connection.
Stefan
FTP not working properly
Manvinder Singh, June 03, 2009 - 12:07 pm UTC
Dear TOM,
While doing FTP using the "ftp_utility" i am getting following error
Error Code :-29532, Error Msg : ORA-29532: Java call terminated by uncaught Java exception: java.io.FileNotFoundException: CWD D:\share: 550 D:\share: The filename, directory name, or volume label syntax is incorrect.
we are issuing the following command to call ftp_utility from a procdure on windows environment
PRC_FTP_PUT('102.086.106.157',21,'in002/ab0098675','abc@1234','C:\\sample.xls','D\\share');
Plz help.
A reader, October 31, 2011 - 11:39 am UTC
In oracle 11d , is it possible to put / get a file into an SFTP site using native packages ?
October 31, 2011 - 12:12 pm UTC
there is no 11d, you mean 11g
no, there are no native sftp packages, you'd have to load up a java library probably for that.
A reader, October 31, 2011 - 11:39 am UTC
In oracle 11g , is it possible to put / get a file into an SFTP site using native packages ?
CWD failed to change the directory
A reader, November 05, 2013 - 8:57 pm UTC
Hi Tom,
I am on oracle 11gr2, i granted all the required java privileges. I am getting an error, when I excute the following code,
declare
file_data blob;
file_name varchar2(255) := 'home/ftpuser/2.sql';
begin
insert into lob_test(id,filename,title,data)
values (2,file_name,'Terry Ford debug file',empty_blob)
returning data into file_data;
ftp_get('127.0.0.1',21,'ftpuser','ftpuser',file_name,file_data);
end;
/
Error at line 1:
ORA-29532: Java call terminated by uncaught java exception:
java.io.filenotfoundexception: CWD /home/ftpuser: 550 failed to change directory.
I am ftping it to my self, and that directory exists on the server. I am also able to ftp file from the terminal window.
Do i need to grant any other privilege? or am I missing something..?
Could you throw me some pointer, what the issue might be please.
Thanks
November 07, 2013 - 3:21 pm UTC
is the file name really 'home/ftpuser/2.sql'? and not '/home/ftpuser/2.sql'?