يكي از مشكلات در محيط اوراكل ساخت دايركتوري ، فايل و … مي باشد و با استفاده از دستورات اوراكل فقط مي توان Oracle Directory ساخت و هر Oracle Directory فقط به يك دايركتوري متصل مي باشد و امكان دسترسي به Subdirectory وجود ندارد
براي حل اين مشكل از Java مي توان استفاده نمود به روش زير:
1- با كاربر SYS به بانك متصل شويد.
2- يك Java Sourec با نام “FileHandler” به روش زير ايجاد نمائيد.
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED “FileHandler” AS
import java.lang.*;
import java.util.*;
import java.io.*;
import java.sql.Timestamp;
public class FileHandler
{
private static int SUCCESS = 1;
private static int FAILURE = 0;
public static int canRead (String path) {
File myFile = new File (path);
if (myFile.canRead()) return SUCCESS; else return FAILURE;
}
public static int canWrite (String path) {
File myFile = new File (path);
if (myFile.canWrite()) return SUCCESS; else return FAILURE;
}
public static int createNewFile (String path) throws IOException {
File myFile = new File (path);
if (myFile.createNewFile()) return SUCCESS; else return FAILURE;
}
public static int delete (String path) {
File myFile = new File (path);
if (myFile.delete()) return SUCCESS; else return FAILURE;
}
public static int exists (String path) {
File myFile = new File (path);
if (myFile.exists()) return SUCCESS; else return FAILURE;
}
public static int isDirectory (String path) {
File myFile = new File (path);
if (myFile.isDirectory()) return SUCCESS; else return FAILURE;
}
public static int isFile (String path) {
File myFile = new File (path);
if (myFile.isFile()) return SUCCESS; else return FAILURE;
}
public static int isHidden (String path) {
File myFile = new File (path);
if (myFile.isHidden()) return SUCCESS; else return FAILURE;
}
public static Timestamp lastModified (String path) {
File myFile = new File (path);
return new Timestamp(myFile.lastModified());
}
public static long length (String path) {
File myFile = new File (path);
return myFile.length();
}
public static String list (String path) {
String list = “”;
File myFile = new File (path);
String[] arrayList = myFile.list();
Arrays.sort(arrayList, String.CASE_INSENSITIVE_ORDER);
for (int i=0; i < arrayList.length; i++) {
// Prevent directory listing expanding if we will blow VARCHAR2 limit.
if ((list.length() + arrayList[i].length() + 1) > 32767)
break;
if (!list.equals(“”))
list += “,” + arrayList[i];
else
list += arrayList[i];
}
return list;
}
public static int mkdir (String path) {
File myFile = new File (path);
if (myFile.mkdir()) return SUCCESS; else return FAILURE;
}
public static int mkdirs (String path) {
File myFile = new File (path);
if (myFile.mkdirs()) return SUCCESS; else return FAILURE;
}
public static int renameTo (String fromPath, String toPath) {
File myFromFile = new File (fromPath);
File myToFile = new File (toPath);
if (myFromFile.renameTo(myToFile)) return SUCCESS; else return FAILURE;
}
public static int setReadOnly (String path) {
File myFile = new File (path);
if (myFile.setReadOnly()) return SUCCESS; else return FAILURE;
}
public static int copy (String fromPath, String toPath) {
try {
File myFromFile = new File (fromPath);
File myToFile = new File (toPath);
InputStream in = new FileInputStream(myFromFile);
OutputStream out = new FileOutputStream(myToFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
return SUCCESS;
}
catch (Exception ex) {
return FAILURE;
}
}
};
/
—————————–
3- يك Package با نام Nepaco_file_api به روش زير ايجاد نمائيد
CREATE OR REPLACE PACKAGE Nepaco_file_api AS
FUNCTION canRead (p_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.canRead (java.lang.String) return java.lang.int‘;
FUNCTION canWrite (p_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.canWrite (java.lang.String) return java.lang.int‘;
FUNCTION createNewFile (p_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.createNewFile (java.lang.String) return java.lang.int‘;
FUNCTION delete (p_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.delete (java.lang.String) return java.lang.int‘;
FUNCTION exists (p_path IN VARCHAR2) RETURN N UMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.exists (java.lang.String) return java.lang.int‘;
FUNCTION isDirectory (p_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.isDirectory (java.lang.String) return java.lang.int‘;
FUNCTION isFile (p_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.isFile (java.lang.String) return java.lang.int‘;
FUNCTION isHidden (p_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.isHidden (java.lang.String) return java.lang.int‘;
FUNCTION lastModified (p_path IN VARCHAR2) RETURN DATE
AS LANGUAGE JAVA
NAME ‘FileHandler.lastModified (java.lang.String) return java.sql.Timestamp‘;
FUNCTION length (p_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.length (java.lang.String) return java.lang.long‘;
FUNCTION list (p_path IN VARCHAR2) RETURN VARCHAR2
AS LANGUAGE JAVA
NAME ‘FileHandler.list (java.lang.String) return java.lang.String‘;
FUNCTION mkdir (p_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.mkdir (java.lang.String) return java.lang.int‘;
FUNCTION mkdirs (p_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.mkdirs (java.lang.String) return java.lang.int‘;
FUNCTION renameTo (p_from_path IN VARCHAR2,
p_to_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.renameTo (java.lang.String, java.lang.String) return java.lang.int‘;
FUNCTION setReadOnly (p_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.setReadOnly (java.lang.String) return java.lang.int‘;
FUNCTION copy (p_from_path IN VARCHAR2,
p_to_path IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA
NAME ‘FileHandler.copy (java.lang.String, java.lang.String) return java.lang.int‘;
END file_api;
/
4 دسترسي هاي زير را به كاربر SYS اعطا نمائيد
BEGIN
DBMS_JAVA.grant_permission(‘SYS’, ‘java.io.FilePermission‘, ‘<<ALL FILES>>’, ‘read ,write, execute, delete‘);
DBMS_JAVA.grant_permission(‘SYS’, ‘SYS:java.lang.RuntimePermission’, ‘writeFileDescriptor‘, ”);
DBMS_JAVA.grant_permission(‘SYS’, ‘SYS:java.lang.RuntimePermission’, ‘readFileDescriptor‘, ”);
END;
GRANT JAVAUSERPRIV TO SYS;
dir=”LTR”>private static int FAILURE = 0;
public static int canRead (String path) {
File myFile = new File (path);
if (myFile.canRead()) return SUCCESS; else return FAILURE;
}
public static int canWrite (String path) {
File myFile = new File (path);
if (myFile.canWrite()) return SUCCESS; else return FAILURE;
}
public static int createNewFile (String path) throws IOException {
File myFile = new File (path);
if (myFile.createNewFile()) return SUCCESS; else return FAILURE;
}
public static int delete (String path) {
File myFile = new File (path);
if (myFile.delete()) return SUCCESS; else return FAILURE;
}
public static int exists (String path) {
File myFile = new File (path);
if (myFile.exists()) return SUCCESS; else return FAILURE;
}
public static int isDirectory (String path) {
File myFile = new File (path);
if (myFile.isDirectory()) return SUCCESS; else return FAILURE;
}
public static int isFile (String path) {
File myFile = new File (path);
if (myFile.isFile()) return SUCCESS; else return FAILURE;
}
public static int isHidden (String path) {
File myFile = new File (path);
if (myFile.isHidden()) return SUCCESS; else return FAILURE;
}
public static Timestamp lastModified (String path) {
File myFile = new File (path);
return new Timestamp(myFile.lastModified());
}
public static long length (String path) {
File myFile = new File (path);
return myFile.length();
}
public static String list (String path) {
String list = “”;
File myFile = new File (path);
String[] arrayList = myFile.list();
Arrays.sort(arrayList, String.CASE_INSENSITIVE_ORDER);
for (int i=0; i < arrayList.length; i++) {
// Prevent directory listing expanding if we will blow VARCHAR2 limit.
if ((list.length() + arrayList[i].length() + 1) > 32767)
break;
if (!list.equals(“”))
list += “,” + arrayList[i];
else
list += arrayList[i];
}
return list;
}
public static int mkdir (String path) {
File myFile = new File (path);
if (myFile.mkdir()) return SUCCESS; else return FAILURE;
}
public static int mkdirs (String path) {
File myFile = new File (path);
if (myFile.mkdirs()) return SUCCESS; else return FAILURE;
}
public static int renameTo (String fromPath, String toPath) {