今天看了一个文件管理的java后台源码,elfinderjava
发现这个东东比我写的代码效率告到不知道哪去了,苦思冥想后仍是抽点时间看看吧。。git
它实现了咱们电脑上的因此关于文件操做的动做,并生成了api开放给前台,具体详细仍是看官方文档吧,本人英文贼菜github
之间用了Java1.7中的NIO里的path类,此工具类能够使咱们不在使用恶心的FIle对象啦,并且速度超快,看着代码整我的都舒服这里有它的介绍JAVA NIO:Path ,Filespring
项目地址elfinder-java-connector此版本没有前台页面实现设计模式
此版本有前台实现elfinder-2.x-servlet不过此版本没有Java源码,是经过maven导入的方式让咱们调用它的核心类api
两种相差spring集成、加载配置文件到context的实现app
相差补充:elfinder-java-connector主要介绍源码的梗概maven
elfinder-2.x-servlet介绍了这个核心类的包装,多了用户视图,操做权限等一些附加模块ide
我今天看的是前者的代码工具
首先咱们先从底层看齐
今天稍微看了一个大概
首先定义了一个公共的接口向外部开放,因此的规则依据此接口开发
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