文件存储 FileUtil FileBaseDto
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的更多相关文章
- android 开发-文件存储之读写sdcard
android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...
- Android数据存储之Android 6.0运行时权限下文件存储的思考
前言: 在我们做App开发的过程中基本上都会用到文件存储,所以文件存储对于我们来说是相当熟悉了,不过自从Android 6.0发布之后,基于运行时权限机制访问外置sdcard是需要动态申请权限,所以以 ...
- 深入理解Sqlserver文件存储之页和应用 (转)
我们每天都在使用数据库,我们部门使用最多的关系数据库有Sqlserver,Oracle,有没有想过这些数据库是怎么存放到操作系统的文件中的?有时候为了能够设计出最优的表结构,写出高性能的Sqlserv ...
- app端上传文件至服务器后台,web端上传文件存储到服务器
1.android前端发送服务器请求 在spring-mvc.xml 将过滤屏蔽(如果不屏蔽 ,文件流为空) <!-- <bean id="multipartResolver&q ...
- MongoDb gridfs-ngnix文件存储方案
在各类系统应用服务端开发中,我们经常会遇到文件存储的问题. 常见的磁盘文件系统,DBMS传统文件流存储.今天我们看一下基于NoSQL数据库MongoDb的存储方案.笔者环境 以CentOS ...
- Hadoop中pid文件存储
我的hadoop集群部署在自己电脑虚拟机上,有时候我是挂起虚拟机,第二天再打开发现有些线程就挂了,比如namenode,好奇怪,当时看了一些帖子说是和pid存储有关,找到log看到找不到pid.因为基 ...
- Android文件存储
文件存储是Android中最基本的一种数据存储方式,它不读存储的内容进行任何的格式化处理,所有数据原封不动的保存在文件之中.如果想用文件存储的方式保存一些较为复杂的数据,就需要定义一套自己的格式规范, ...
- Kafka深入理解-1:Kafka高效的文件存储设计
文章摘自:美团点评技术团队 Kafka文件存储机制那些事 Kafka是什么 Kafka是最初由Linkedin公司开发,是一个分布式.分区的.多副本的.多订阅者,基于zookeeper协调的分布式日 ...
- .Net平台下,分布式文件存储的实现
遇到的问题 对于Web程序,使用一台服务器的时候,客户端上传的文件一般也都是存储在这台服务器上.但在集群环境中就行不通了,如果每个服务器都存储自己接受到的文件,就乱套了,数据库中明明有这个附件的记录, ...
随机推荐
- replace的用法
http://blog.sina.com.cn/s/blog_9ed9ac7d0101ec1f.html replace 语句 如果存在,更新,否则,插入在使用REPLACE时,表中必须有唯一索引,而 ...
- UVALive 3971 Assemble(模拟 + 二分)
UVALive 3971 题意:有b块钱.想要组装一台电脑,给出n个配件的种类,名字,价格,品质因子.若各种类配件各买一个,总价格<=b,求最差品质配件的最大品质因子. 思路: 求最大的最小值一 ...
- Mybatis学习记录(二)----mybatis开发dao的方法
1 SqlSession使用范围 1.1 SqlSessionFactoryBuilder 通过SqlSessionFactoryBuilder创建会话工厂SqlSessionFactory 将Sq ...
- 【android相关】【问题解决】R.java文件丢失
在进行android开发过程中,有时候,我们会遇到gen文件中R.java丢失的现象.重新build,或者clean工程,close并重新打开Project,但有时也没解决. 这可能是由于不小心把xm ...
- 再次学习mysql优化
再次学习mysql优化 表的设计规范化(三范式) 添加索引(普通索引.主键索引.唯一索引.全文索引) 分表(水平分割.垂直分割) 读写分离(写add.update.delete) 存储过程 对mysq ...
- UVA 6475 Effective Infection Time
You are estimating the threat level of quarantined zones that have been abandoned to the infection. ...
- Java常见分页方式
1. 网站常见分页样式 采用传统的分页方式(邮件列表),可以明确的获取数据信息,如有多少条数据 分多少页显示等 采用下拉式的分页样式(QQ空间),一般无法获取明确的数据数量相关的信息,但是在分页操作之 ...
- atitit.跨语言执行cmd cli api的原理及兼容性设计草案
atitit.跨语言执行cmd cli api的原理及兼容性设计草案 1. 标准输入,标准输出,标准错误与重新定向1 2. 常见问题2 2.1. 执行bat文件2 2.2. 执行bat文件 /c ...
- [容器]gcr.io镜像下载
下载gcr.io的镜像hosts文件 把下面两行加入到/etc/hosts中. 更多在这里http://wst.so/files/hosts 61.91.161.217 gcr.io 61.91.1 ...
- activiti自己定义流程之整合(三):整合自己定义表单创建模型
本来在创建了表单之后应该是表单列表和预览功能.可是我看了看整合的代码,和之前没实用angularjs的基本没有什么变化,一些极小的变动也仅仅是基于angularjs的语法,因此全然能够參考之前说些的表 ...