elfinder源碼瀏覽-Volume文件系統操作類(1)

今天看了一個文件管理的java後臺源碼,elfinder

發現這個東東比我寫的代碼效率告到不知道哪去了,苦思冥想後還是抽點時間看看吧。。

它實現了我們電腦上的所以關於文件操作的動作,並生成了api開放給前臺,具體詳細還是看官方文檔吧,本人英文賊菜

之間用了Java1.7中的NIO裏的path類,此工具類可以使我們不在使用噁心的FIle對象啦,而且速度超快,看着代碼整個人都舒服這裏有它的介紹JAVA NIO:Path ,File

 

項目地址elfinder-java-connector此版本沒有前臺頁面實現

 

此版本有前臺實現elfinder-2.x-servlet不過此版本沒有Java源碼,是通過maven導入的方式讓我們調用它的核心類

兩種相差spring集成、加載配置文件到context的實現

相差補充:elfinder-java-connector主要介紹源碼的梗概

elfinder-2.x-servlet介紹了這個核心類的包裝,多了用戶視圖,操作權限等一些附加模塊

我今天看的是前者的代碼

首先我們先從底層看齊

  今天稍微看了一個大概

  首先定義了一個公共的接口向外部開放,所以的規則依據此接口開發

public interface Volume{
  各種需要操作文件的定義
String getMimeType(Target target) throws IOException;

}  合成這個接口的包裝接口(設計模式中合成)
public interface Target { Volume getVolume(); }

 好了之後實現了這個包裝接口Target

public class NIO2FileSystemTarget implements Target {

    private final Path path;
    private final Volume volume;
。。。。。。。。。。。。。。。。。。。

再然後聚合了這個幾個接口和實現類

public class NIO2FileSystemVolume implements Volume {

    private final String alias;
    private final Path rootDir;
    private final Detector detector;

    private NIO2FileSystemVolume(Builder builder) {
        this.alias = builder.alias;
        this.rootDir = builder.rootDir;
        this.detector = new NIO2FileTypeDetector();//我今天大致弄明白了這個的實現
        createRootDir();
    }
@Override
public String getMimeType(Target target) throws IOException {
Path path = fromTarget(target);
return detector.detect(path);
}
。。。。。。。。。。。。。。。。。。。。。。

我們從這個聚合類開始看

下面這個獲取文件類型的實現是通過tika的工具獲取的 文件內容讀取--Tika這個介紹比較詳細

Tika是Apache下開源的文檔內容解析工具,支持上千種文檔格式(如PPT、XLS、PDF)。Tika使用統一的方法對各種類型文件進行內容解析,封裝了各種格式解析的內部實現,可用於搜索引擎索引、內容分析、轉換等場景。

我們來看他的具體實現

首先是Detector 他的一個接口

 

public interface Detector {
    String detect(InputStream inputStream)throws IOException;
    String detect(Path path)throws  IOException;
}

 

實現

public class NIO2DileTypeDetector implements Detector {

   private  final Tika tika = new Tika();
。。。。。。。。。。。。。。。。。。。。。

 

看了這些大概弄明白這個代碼的大致寫法心裏有點小激動,我們寫個測試類

public class Test {
    @org.junit.Test
    public void Test1()throws IOException {
        Path path = Paths.get("F:\\[加密與解密(第三版)].段鋼.掃描版.pdf");
        NIO2DileTypeDetector detector = new NIO2DileTypeDetector();
        System.out.println(detector.detect(path));
    }
}

C:\server\jdk1.8.0_77\bin\java 。。。。。。。。。。
application/pdf

 

 

 

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章