user3327133
user3327133

Reputation: 1

Calling JAVA FTP program from PLSQL

As per my requirement, I have developped a a java code that will FTP a file from local path to server. Now I want to store it in database as a procedure/function and want to call it from backend. Below is my code:

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "FtpUrlUpload" AS

package net.codejava.ftp;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.lang.String;

public class FtpUrlUpload {

private static final int BUFFER_SIZE = 4096;

public static void main(String[] args) {
    String ftpUrl = "ftp://%s:%s@%s/%s;type=i";
    String host = "127.23.21.10";
    String user = "samuser";
    String pass = "password";
    String filePath = "E:/Work/Project.zip";
    String uploadPath = "/MyProjects/archive/Project.zip";

    ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath);
    System.out.println("Upload URL: " + ftpUrl);

    try {
        URL url = new URL(ftpUrl);
        URLConnection conn = url.openConnection();
        OutputStream outputStream = conn.getOutputStream();
        FileInputStream inputStream = new FileInputStream(filePath);

        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = -1;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        inputStream.close();
        outputStream.close();

        System.out.println("File uploaded");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
}

I am facing an issue while trying to compile it in database. Below is the issue: Errors for JAVA SOURCE FtpUrlUpload:

LINE/COL                                                                        
--------------------------------------------------------------------------------
ERROR                                                                           
--------------------------------------------------------------------------------
0/0                                                                             
FtpUrlUpload:22: cannot resolve symbol                                          

0/0                                                                             
symbol  : method format (java.lang.String,java.lang.String,java.lang.String,java
.lang.String,java.lang.String)                                                  

0/0                                                                             
1 error                                                                         

LINE/COL                                                                        
--------------------------------------------------------------------------------
ERROR                                                                           
--------------------------------------------------------------------------------

0/0                                                                             
        ftpUrl = String.format(ftpUrl, user, pass, host, uploadPath);           

0/0                                                                             
                       ^                                                        

0/0                                                                             
location: class java.lang.String                                                

LINE/COL                                                                        
--------------------------------------------------------------------------------
ERROR                                                                           
--------------------------------------------------------------------------------

But I have compiled it using JAVAC, where it is getting compiled successfully. I am basically a database programmer (SQL/PLSQL), and have a liitle knowledge about java. Could anyone please guide me here.

Upvotes: 0

Views: 503

Answers (1)

Leo
Leo

Reputation: 1919

Your problem is that the Oracle JVM in Oracle database 10.2 uses Java 1.4.x , so in 1.4 String.format() did'nt exist. When you compiled with javac, you were using a recent version of java (1.5 or later) , therefore there were no problem. My advice is to remove String.format() and you should follow the 1.4 java style.

Upvotes: 1

Related Questions