zoukankan      html  css  js  c++  java
  • 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

     

      

  • 相关阅读:
    oracle 函数WMSYS.WM_CONCAT()的用法(行转列) 老猫
    PL/SQL 数独 九宫图 老猫
    oracle10g rman backup and recover 老猫
    Oracle SQL的优化 老猫
    Oracle数据库中的字符处理技巧总结 老猫
    WITH分析函数 老猫
    30套JSP网站源代码合集
    Java获取系统信息(cpu,内存,硬盘,进程等)的相关方法
    [原]Web Service学习
    常用Web Service汇总(天气预报、时刻表等)
  • 原文地址:https://www.cnblogs.com/dmeck/p/8586482.html
Copyright © 2011-2022 走看看