首页 技术 正文
技术 2022年11月6日
0 收藏 685 点赞 1,076 浏览 7079 个字
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;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,118
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,590
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,435
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,206
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,842
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,927