天天看點

企業級項目防止檔案夾同問題解決方案

處理檔案同名問題:

    方案一

      * 檔案名+uuid

      * 進行分目錄操作

    [16:37]

    檔案上傳目錄分層------------

   黑馬程式員---------安卓

 1今天上課,老師給我們講解的知識擴充,說企業級OA系統,部署到企業一年後,發送郵件附件功能,不可以發送附加了。最後排錯是因為,同一個檔案夾中,子檔案夾太多(子檔案)夾中的檔案太多問題。

       這樣在小的web項目中,也許不會發生,可以在大的企業級,頻繁建立檔案,中,會遇到這樣的問題

    解決方案是

         1.檔案名+uuid 來解決

            uuid屬于java.util 包下的工具類 可以随機生成一些32位的随機數字

         2.使用file.hashcode後八位與上二進制來建立,不同的檔案夾,我用的是與二進制,+右移4為進行 來進行組合,建立16*16檔案夾

                 使用String 類行的hashCode方法來得到,傳來檔案名的hashcode碼。使用hashcode碼後八位與二進制進行與運算。得到1-15數字

                 在原來的基礎上,右移4得到

         進行上床目錄分層的操作

​​http://blog.sina.com.cn/s/blog_439f80c40100n0hc.html                   java​​ 左移動和右移動<轉載>

       1.file+File.separator    跨浏覽器支援路徑分隔符

       2.file+file.mkdirs();     建立多層檔案

        3.  int d1=hashCode & 0xf;

            int d2=hashCode>>>4 & 0xf;

  eg:str.hashCode()-------->return int------------>int d1=hashCode & 0xf; 得到随機的15 個數字 -=------>int d2=hashCode>>>4 & 0xf ------->得到随機的15位,

          -----15*15個不同的  檔案夾

源碼貼出來,我寫的是工具類

package com.it.heima.uuidutils;
import java.io.File;
 import java.io.IOException;
 import java.util.UUID;public class Uuidutils {
 
  public static String getUuid(String filename){  UUID uuid=new UUID(32, 32);
   String newuid=uuid.randomUUID().toString();
   System.out.println(newuid.replace("-", "")+"-"+filename);
   
   return newuid.replace("-", "")+"-"+filename;
   
  }
  
  public static void main(String[] args) throws IOException {
  
  //System.out.println(Uuidutils.ItemListener("hege.txt"));
   
   
  }
  /*public static String ItemListener(String filename){
   
   if(filename==null)
   {
    return null;
   }
   
    int hashCode=filename.hashCode();
    
    int d1=hashCode & 0xf;
    
    int d2=hashCode>>>4 & 0xf;
    
   return File.separator+d1+File.separator+d2+File.separator+filename;
   
   
  }*/
  public static String ItemListener(String dirPath,String filename) throws IOException{
   
   if(filename==null)
   {
    return null;
   }
    int hashCode=filename.hashCode();
    
    int d1=hashCode & 0xf;
    
    int d2=hashCode>>>4 & 0xf;
    String subpath= File.separator+d1+File.separator+d2; //中間目錄
    /**
     * New 檔案建立
     */
    File file=new File(dirPath+subpath);
    file.mkdirs();
   // String zString=dirPath+subpath;
    //交給主函數
   File lastPath=new File(file+File.separator+filename);
   
   System.out.println( lastPath.createNewFile());
   
   
   return lastPath.toString();
   
   
  }
 }封裝的接口為
     public static String ItemListener(String dirPath,String filename)
 
       dirPath    //檔案上傳的路徑
      filename   //需要生成的檔案名
     //還可以擴充,讓檔案名+uuid的形式 這樣完全解決了,
    檔案夾下子檔案太多的瓶頸,解決了檔案名重名的問題
 
 // 寫路徑 得到上傳的檔案名字+檔案要寫的路徑,web資源的位置
 FileOutputStream fileOut = new FileOutputStream(new File(Uuidutils.ItemListener(dirPath, fileName)));
      //FileOutputStream fileOut = new FileOutputStream(new File(dirPath,Uuidutils.ItemListener(fileName)));
      byte[] buf = new byte[1024];
      int len = -1;
      while( (len = is.read(buf)) != -1 ){
       fileOut.write(buf, 0, len);
      }      
企業級項目防止檔案夾同問題解決方案