码迷,mamicode.com
首页 > 其他好文 > 详细

elfinder源码浏览-Volume文件系统操作类(1)

时间:2018-03-17 00:41:18      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:方式   导入   unit   开发   csdn   搜索引擎   .net   --   个人   

今天看了一个文件管理的java后台源码,elfinder

发现这个东东比我写的代码效率告到不知道哪去了,苦思冥想后还是抽点时间看看吧。。

它实现了我们电脑上的所以关于文件操作的动作,并生成了api开放给前台,具体详细还是看官方文档吧,本人英文贼菜

之间用了Java1.7中的NIO里的path类,此工具类可以使我们不在使用恶心的FIle对象啦,而且速度超快,看着代码整个人都舒服这里有它的介绍JAVA NIO:Path ,File

项目地址elfinder-java-connector此版本没有前台页面实现

此版本有前台实现elfinder-2.x-servlet不过此版本没有Java源码,是通过maven导入的方式让我们调用它的核心类

两种相差spring集成、加载配置文件到context的实现

我今天看的是前者的代码

首先我们先从底层看齐

  今天稍微看了一个大概

  首先定义了一个公共的接口向外部开放,所以的规则依据此接口开发

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

 

 

 

  

elfinder源码浏览-Volume文件系统操作类(1)

标签:方式   导入   unit   开发   csdn   搜索引擎   .net   --   个人   

原文地址:https://www.cnblogs.com/dmeck/p/8586482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!