1 檔案目錄建立、删除(适用于unix \ Win XP 系統 ;Win 7未測試)
public static Boolean createScrrenManagerFolder(String savePath){
Boolean isBln = false;
String savePath =HttpServletRequest.getServletContext().getRealPath("");
String lastsFolderPath = "";
String partPathString = "";
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd/HHmmss");// 設定日期格式
String systime = df.format(new Date());
String partPathString = systime.substring(0, systime.lastIndexOf("/")+1);
try{
if (savePath.indexOf("wtpwebapps") != -1) {//eclipse 調試狀态下的項目路徑
int index1 = savePath.indexOf("\\.");
int index2 = savePath.lastIndexOf("\\");
lastsFolderPath = savePath.substring(0, index1) + savePath.substring(index2, savePath.length()) + "\\WebContent\\" ;
} else {//war包運作狀态下的項目路徑
lastsFolderPath = savePath + "/" ;
}
lastsFolderPath = lastsFolderPath +"resources/upload/picture/uuid/"+partPathString;//eg:E:\Tomcat7\vCat/2013/05/07/
File fp = new File(lastsFolderPath);
// 建立目錄
if (!fp.exists()) {
fp.mkdirs();// 目錄不存在的情況下,建立目錄。
}
isBln = true;
}
catch(Exception e){
isBln = false;
e.printStackTrace();
}
return isBln;
}
**====================================//4 遞歸删除空目錄(隻删除空的四級目錄:%path%/uuid/2013/06/21/)================================**
public void delFolderFour(){
Integer folderDeep = 4 ;
while (lastsPath.indexOf("/") != -1 && ( (folderDeep--) > 0) ) {
if( "0".equals(delFolder(lastsPath.trim())) ){
lastsPath = lastsPath.substring(0,lastsPath.length()-1);
lastsPath = lastsPath.substring(0,lastsPath.lastIndexOf("/"));
}
else{
break;
}
}
}
// 遞歸删除空目錄
public static String delFolder(String filePath) {
String isBln = "0";//含有空目錄
File fp = new File(filePath);
try{
// 遞歸删除空目錄
if (fp.exists() && fp.isDirectory()) {
File[] files = fp.listFiles();
if (files.length == 0) {// 空檔案夾
fp.delete();
}
else{
isBln = "1" ;// 不為空檔案夾,直接跳出,不用檢查此目錄的上級目錄是否為空檔案夾
}
}
}
catch(Exception e){
isBln = "2";//異常,直接跳出
}
return isBln;
}
**========================================目錄權限(file permission)=======================================**
2 目錄權限(file permission)
http://docs.oracle.com/javase/6/docs/api/java/io/File.html(API )
前言:
In Java, file permissions are very OS specific: *nix , NTFS (windows) and FAT/FAT32, all have different kind of file permissions. Java comes with some generic file permission to deal with it.
Check if the file permission allow :
- file.canExecute(); – return true, file is executable; false is not.
- file.canWrite(); – return true, file is writable; false is not.
- file.canRead(); – return true, file is readable; false is not.
Set the file permission :
- file.setExecutable(boolean); – true, allow execute operations; false to disallow it.
- file.setReadable(boolean); – true, allow read operations; false to disallow it.
- file.setWritable(boolean); – true, allow write operations; false to disallow it.
-
separatorChar
The system-dependent default name-separator character. This field is initialized to contain the first character of the value of the system propertypublic static final char separatorChar
. On UNIX systems the value of this field isfile.separator
; on Microsoft Windows systems it is'/'
.'\\'
In *nix system, you may need to configure more specifies about file permission, e.g set a 777 permission for a file or directory, however, Java IO classes do not have ready method for it, but you can use the following dirty workaround :
Runtime.getRuntime().exec("chmod 777 file");
2.1 操作java.io Class File 類:
WIN OS:如對1添權重限:
.......
// 建立目錄
if (!fp.exists()) {
fp.mkdirs();// 目錄不存在的情況下,建立目錄。
if( !fp.canRead() ){
fp.setReadable(true);
}
if( !fp.canWrite() ){
fp.setWritable(true);
}
if( !fp.canExecute() ){
fp.setExecutable(true);
}
}
.......
2.2 操作java.io Class FilePermission類:(未研究)
http://www.cjsdn.net/doc/jdk50/java/security/Permission.html(API)
2.3 在*nix系統中,配置檔案權限,檔案的權限為777. (java IO類沒有相關方法)
public static void updatefilePermi(){
if( -1 != System.getProperties().getProperty("os.name").toLowerCase().indexOf("windows") ){
//1 windows OS:通過io File類對檔案路徑賦予讀寫權限
//如上
}
//1 2 其它作業系統 :通過untime.getRuntime().exec()執行command對檔案路徑賦予讀寫權限 ,待驗證後進行修正
String filepath = "";
String command = "chmod 777 " + filepath ;
Runtime runtime = Runtime.getRuntime();
try {
Process exec = Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
}