package com.guige.base.fileutils;

import com.alibaba.fastjson.JSONArray;
import com.aliyun.oss.ServiceException;
import com.guige.base.dto.FileBaseDto;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils; import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* TODO
*
* @author songaw
* @date 2018/4/9 17:07
*/
public abstract class FileUtil {
public static final String FILE_LOCAL="0";
public static final String FILE_OSS="1";
public static final String FILE_FTP="2";
private String storageType;
private Map<String,String> linkConf =new HashMap<>(); /**
* 根据一个配置得到对应的FileUtil
* @param storageType FileUtil类型 0本地 1 OSS 2FTP
* @return
* @throws Exception
*/
public static FileUtil sysConfigOfFileUtil(Map<String, String> linkConf, String storageType) throws Exception {
FileUtil fileUtil = null;
List<FileUtil> resultFileUtils = new ArrayList<>();
if (linkConf==null||linkConf.isEmpty()) {
return fileUtil;
} if (linkConf==null||linkConf.isEmpty()) {
return fileUtil;
}
List<Map<String, String>> linkConfList = new ArrayList<>();
linkConfList.add(linkConf);
resultFileUtils = sysConfigOfFileUtil(linkConfList, storageType);
if(CollectionUtils.isNotEmpty(resultFileUtils)){
fileUtil= resultFileUtils.get(0);
}
return fileUtil;
}
/**
* 根据一组链接配置 得到一组FileUtil
* @param confs 一组配置
* @param storageType FileUtil类型0本地 1 OSS 2FTP
* @return
* @throws Exception
*/
public static List<FileUtil> sysConfigOfFileUtil(List<Map<String, String>> confs, String storageType) throws Exception {
List<FileUtil> fileUtils = new ArrayList<>();
boolean isLinkSuccess = false;
try {
if (confs == null || confs.size() == 0) {
return fileUtils;
}
for (int i = 0; i < confs.size(); i++) {
Map<String, String> map = confs.get(i);
FileUtil fileUtil = null;
try {
switch (storageType) {
case "0":
fileUtil = new LocalFileUtil(map.get("ROOT_PATH"));
break;
case "1":
fileUtil = new OssFileUtil(map.get("END_POINT"), map.get("ACCESS_KEY_ID"), map.get("ACCESS_KEY_SECRET"), map.get("BUCKET_NAME"), map.get("PROXY_HOST"), map.get("PROXY_PORT"));
break;
case "2":
fileUtil = new FtpFileUtil(map.get("HOST"), Integer.parseInt(map.get("PORT")), map.get("USERNAME"), map.get("PASSWORD"));
break;
}
isLinkSuccess = true;
if (fileUtil != null) {
fileUtils.add(fileUtil);
}
} catch (Exception e) {
continue;
}
}
if (!isLinkSuccess) {
throw new RuntimeException("文件管理器创建失败");
}
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("文件管理器链接失败");
}
return fileUtils;
}
//上传文件 /**
* 保存文件
*
* @param path
* @param filename
* @param input
* @return
*/
public abstract boolean saveFile(String path, String filename, InputStream input, boolean replace_existing) throws IOException; /**
* 保存文件
*
* @param path
* @param filename
* @param fileDate
* @return
* @throws IOException
*/
public abstract boolean saveFile(String path, String filename, byte[] fileDate, boolean replace_existing) throws IOException; /**
* 保存文件base 64
*
* @param path
* @param filename
* @param base64Data
* @return
* @throws IOException
*/
public abstract boolean saveFile(String path, String filename, String base64Data, boolean replace_existing) throws IOException; /**
* 保存文件
*
* @param path 路径
* @param filename 文件名称
* @param file 文件
* @return
* @throws IOException
*/
public abstract boolean saveFile(String path, String filename, File file, boolean replace_existing) throws IOException; /**
* 保存文件 直接保存文件 文件名是File的name
*
* @param path 路径
* @param file 文件
* @return
* @throws IOException
*/
public abstract boolean saveFile(String path, File file, boolean replace_existing) throws IOException; /**
* 上传多个文件
*
* @param path
* @param files
* @return
* @throws IOException
*/
public abstract boolean saveFile(String path, List<File> files, boolean replace_existing) throws IOException; /**
* 上传已经存在的文件 到FTP或者 OSS
*
* @param path
* @param urls
* @return
* @throws IOException
*/
public abstract boolean saveLocalFile(String path, List<String> urls, boolean replace_existing) throws IOException; /**
* 读取文件
*
* @param path
* @param filename
* @return
*/
public abstract InputStream readInputStream(String path, String filename) throws IOException; /**
* 查询一个目录下的所有文件
*
* @param pathStr
* @return
*/
public abstract List<FileBaseDto> readFileList(String pathStr, boolean isReadDir) throws IOException; /**
* 查询文件
*
* @param path
* @param filename
* @return
*/
public abstract FileBaseDto readFileInfo(String path, String filename) throws IOException; /**
* 删除文件
*
* @param path 文件所在路径
* @return
*/
public abstract boolean delete(String path) throws IOException; /**
* 删除多个文件
*
* @param paths
* @return
*/
public abstract boolean delete(List<String> paths) throws IOException; /**
* 判断文件是否存在
*
* @param path
* @return
*/
public abstract boolean exists(String path) throws IOException; /**
* 移动文件
*
* @param path 原路径 可以是文件夹也可以是文件
* @param newPath 目标路径 跟原路径格式保持一致
* @param replace_existing 是否替換 (如果为true并且目标文件夹存在则会删除目标文件夹 然后进行移动)
* @return
*/
public abstract boolean moveTo(String path, String newPath, boolean replace_existing) throws IOException; /**
* 复制文件
*
* @param path 原路径 可以是文件夹也可以是文件
* @param newPath 目标路径 跟原路径格式保持一致
* @param replace_existing 是否替換 (如果为true并且目标文件夹存在则会删除目标文件夹 然后进行复制)
* @return
*/
public abstract boolean copy(String path, String newPath, boolean replace_existing) throws IOException; public String getContentType(String fileName) {
if (fileName.contains(".")) {
String fileExtension = fileName.substring(fileName.lastIndexOf(".")); switch (fileExtension) {
case "asf":
return "video/x-ms-asf"; case "avi":
return "video/avi"; case "doc":
return "application/msword"; case "docx":
return "application/msword"; case "zip":
return "application/zip"; case "xls":
return "application/vnd.ms-excel"; case "xlsx":
return "application/vnd.ms-excel"; case "gif":
return "image/gif"; case "jpg":
return "image/jpeg"; case "png":
return "image/jpeg"; case "jpeg":
return "image/jpeg"; case "wav":
return "audio/wav"; case "mp3":
return "audio/mpeg3"; case "mpg":
return "video/mpeg "; case "mepg":
return "video/mpeg"; case "rtf":
return "application/rtf"; case "html":
return "text/html"; case "mht":
return "message/rfc822"; case "mhtl":
return "message/rfc822"; case "txt":
return "text/plain"; default:
// throw new RuntimeException("fileutils type:" + ext + "can't be downloaded.");
return "multipart/form-data";
}
}
return "dir";
} public byte[] inputStream2ByteArray(File file) throws IOException { InputStream in = new FileInputStream(file);
byte[] data = toByteArray(in);
in.close(); return data;
} public byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int n = 0;
while ((n = in.read(buffer)) != -1) {
out.write(buffer, 0, n);
}
return out.toByteArray();
}
public String getStorageType() {
return storageType;
} public void setStorageType(String storageType) {
this.storageType = storageType;
} public Map<String, String> getLinkConf() {
return linkConf;
} public void setLinkConf(Map<String, String> linkConf) {
this.linkConf = linkConf;
}
}
package com.guige.base.dto;

/**
* 文件管理帮助类
*
* @author songaw
* @date 2018/4/13 10:55
*/ public class FileBaseDto {
/**
* 文件的路径
*/
public String filePath;
/**
* 文件名
*/
public String originName;
/**
* 文件大小
*/
private Long fileSize;
/**
* 类型
*/
public String contentType; /**
* 存储类型,0 LOCAL, 1 OSS 2.FTP
*/
private Integer storageType; public String getFilePath() {
return filePath;
} public void setFilePath(String filePath) {
this.filePath = filePath;
} public String getOriginName() {
return originName;
} public void setOriginName(String originName) {
this.originName = originName;
} public Long getFileSize() {
return fileSize;
} public void setFileSize(Long fileSize) {
this.fileSize = fileSize;
} public String getContentType() {
return contentType;
} public void setContentType(String contentType) {
this.contentType = contentType;
} public Integer getStorageType() {
return storageType;
} public void setStorageType(Integer storageType) {
this.storageType = storageType;
}
}

文件存储 FileUtil FileBaseDto的更多相关文章

  1. android 开发-文件存储之读写sdcard

    android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...

  2. Android数据存储之Android 6.0运行时权限下文件存储的思考

    前言: 在我们做App开发的过程中基本上都会用到文件存储,所以文件存储对于我们来说是相当熟悉了,不过自从Android 6.0发布之后,基于运行时权限机制访问外置sdcard是需要动态申请权限,所以以 ...

  3. 深入理解Sqlserver文件存储之页和应用 (转)

    我们每天都在使用数据库,我们部门使用最多的关系数据库有Sqlserver,Oracle,有没有想过这些数据库是怎么存放到操作系统的文件中的?有时候为了能够设计出最优的表结构,写出高性能的Sqlserv ...

  4. app端上传文件至服务器后台,web端上传文件存储到服务器

    1.android前端发送服务器请求 在spring-mvc.xml 将过滤屏蔽(如果不屏蔽 ,文件流为空) <!-- <bean id="multipartResolver&q ...

  5. MongoDb gridfs-ngnix文件存储方案

          在各类系统应用服务端开发中,我们经常会遇到文件存储的问题. 常见的磁盘文件系统,DBMS传统文件流存储.今天我们看一下基于NoSQL数据库MongoDb的存储方案.笔者环境 以CentOS ...

  6. Hadoop中pid文件存储

    我的hadoop集群部署在自己电脑虚拟机上,有时候我是挂起虚拟机,第二天再打开发现有些线程就挂了,比如namenode,好奇怪,当时看了一些帖子说是和pid存储有关,找到log看到找不到pid.因为基 ...

  7. Android文件存储

    文件存储是Android中最基本的一种数据存储方式,它不读存储的内容进行任何的格式化处理,所有数据原封不动的保存在文件之中.如果想用文件存储的方式保存一些较为复杂的数据,就需要定义一套自己的格式规范, ...

  8. Kafka深入理解-1:Kafka高效的文件存储设计

    文章摘自:美团点评技术团队  Kafka文件存储机制那些事 Kafka是什么 Kafka是最初由Linkedin公司开发,是一个分布式.分区的.多副本的.多订阅者,基于zookeeper协调的分布式日 ...

  9. .Net平台下,分布式文件存储的实现

    遇到的问题 对于Web程序,使用一台服务器的时候,客户端上传的文件一般也都是存储在这台服务器上.但在集群环境中就行不通了,如果每个服务器都存储自己接受到的文件,就乱套了,数据库中明明有这个附件的记录, ...

随机推荐

  1. org.hibernate.exception.ConstraintViolationException: could not delete:

    转自:http://fireinwind.iteye.com/blog/848515 异常描述: org.hibernate.exception.ConstraintViolationExceptio ...

  2. Linux——CentOS 6.3下PostgreSQL 的安装与配置

    一.简介 PostgreSQL 是一种非常复杂的对象-关系型数据库管理系统(ORDBMS),也是目前功能最强大,特性最丰富和最复杂的自由软件数据库系统.有些特性甚至连商业数据库 都不具备.这个起源于伯 ...

  3. vue 项目中 自定义 webpack 的 配置文件(webpack.config.babel.js)

    webpack.config.babel.js,这样命名是想让webpack在编译的时候自动识别es6的语法,现在貌似不需要这样命名了,之前用webpack1.x的时候貌似是需要的 let path ...

  4. win7系统扩展双屏幕时,怎样在两个屏幕下都显示任务栏

    扩展屏幕下都显示任务栏!!! win7系统本身无法设置该功能(眼下我是不知道) 但能够下载第三方软件来解决该问题. 第一步:Dual Monitor Taskbar 下载软件 下载链接:http:// ...

  5. mac os x 安装adb

    http://stackoverflow.com/questions/31374085/installing-adb-on-mac-os-x Option 1 - Using Homebrew Thi ...

  6. Python基础--人们一些最爱的标准库(random time)

    Python继续! random 包括返回随机数的函数. 这里跟C++一样,产生的是伪随机数,并非全然随机数. random中一些重要的函数: random() 返回0<n<=1的随机数n ...

  7. JAVA IO:Scanner类

    使用Scanner类接收输入数据. JAVA提供了专门的输入数据类,此类可以完成BufferedReader类的功能,也可以方便的对输入数据进行验证,此类存放于JAVA.UTILL包中. 常用方法如下 ...

  8. zabbix监控redis的key值

    配置zabbix客户端配置文件 vim /etc/zabbix/zabbix_agentd.conf 添加  Include=/etc/zabbix/zabbix_agentd.d/ 添加脚本对red ...

  9. ftp获取远程Pdf文件

    此程序需要安装ftp服务器,安装adobe reader(我这里使用的adobe reader9.0) 1.部署ftp服务器 将ftp的权限设置为允许匿名访问,部署完成 2.安装adobe reade ...

  10. GuozhongCrawler系列教程 (2) CrawTaskBuilder具体解释

    GuozhongCrawler是分层架构.要高速学习CrawlTask独立的配置多少要了解框架的源码.所以CrawTaskBuilder提供要更加扁平且易于理解的的方式创建CrawTask 方法具体资 ...