Merge remote-tracking branch 'origin/master'

# Conflicts:
#	eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java
pull/174/head
jyao 2019-10-29 09:48:29 +08:00
commit 5916cb2f50
1 changed files with 22 additions and 31 deletions

View File

@ -7,7 +7,6 @@ import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import me.zhengjie.exception.BadRequestException;
import org.springframework.web.multipart.MultipartFile;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
@ -20,7 +19,6 @@ import java.util.Map;
/**
* File hutool
*
* @author Zheng Jie
* @date 2018-12-27
*/
@ -46,15 +44,14 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
/**
* MultipartFileFile
*
* @param multipartFile
* @return
*/
public static File toFile(MultipartFile multipartFile) {
public static File toFile(MultipartFile multipartFile){
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀
String prefix = "." + getExtensionName(fileName);
String prefix="."+getExtensionName(fileName);
File file = null;
try {
// 用uuid作为文件名防止生成的临时文件重复
@ -69,7 +66,6 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
/**
*
*
* @param files
*/
public static void deleteFile(File... files) {
@ -82,14 +78,13 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
/**
*
*
* @param filename
* @return
*/
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
if ((dot >-1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
@ -98,14 +93,13 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
/**
* Java
*
* @param filename
* @return
*/
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length()))) {
if ((dot >-1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
@ -114,11 +108,10 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
/**
*
*
* @param size
* @return
*/
public static String getSize(long size) {
public static String getSize(long size){
String resultSize = "";
if (size / GB >= 1) {
//如果当前Byte的值大于等于1GB
@ -137,14 +130,13 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
/**
* inputStream File
*
* @param ins
* @param name
* @return
* @throws Exception
*/
public static File inputStreamToFile(InputStream ins, String name) throws Exception {
File file = new File(System.getProperty("java.io.tmpdir") + name);
public static File inputStreamToFile(InputStream ins, String name) throws Exception{
File file = new File(System.getProperty("java.io.tmpdir") + File.separator + name);
if (file.exists()) {
return file;
}
@ -174,12 +166,13 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
String nowStr = "-" + format.format(date);
try {
String fileName = name + nowStr + "." + suffix;
String path = filePath + File.separator + fileName;
File dest = new File(path).getCanonicalFile();
String path = filePath + fileName;
File dest = new File(path);
// 检测是否存在目录
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();// 新建文件夹
}
String d = dest.getPath();
file.transferTo(dest);// 文件写入
return dest;
} catch (Exception e) {
@ -190,33 +183,32 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
public static String fileToBase64(File file) throws Exception {
FileInputStream inputFile = new FileInputStream(file);
String base64 = null;
byte[] buffer = new byte[(int) file.length()];
String base64 =null;
byte[] buffer = new byte[(int)file.length()];
inputFile.read(buffer);
inputFile.close();
base64 = new Base64().encode(buffer);
base64=new Base64().encode(buffer);
String encoded = base64.replaceAll("[\\s*\t\n\r]", "");
return encoded;
}
/**
* excel
*
* @param list
* @return
* @throws Exception
*/
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
String tempPath = System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
String tempPath =System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
File file = new File(tempPath);
BigExcelWriter writer = ExcelUtil.getBigWriter(file);
BigExcelWriter writer= ExcelUtil.getBigWriter(file);
// 一次性写出内容,使用默认样式,强制输出标题
writer.write(list, true);
//response为HttpServletResponse对象
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//test.xls是弹出下载对话框的文件名不能为中文中文请自行编码
response.setHeader("Content-Disposition", "attachment;filename=file.xlsx");
ServletOutputStream out = response.getOutputStream();
response.setHeader("Content-Disposition","attachment;filename=file.xlsx");
ServletOutputStream out=response.getOutputStream();
// 终止后删除临时文件
file.deleteOnExit();
writer.flush(out, true);
@ -229,13 +221,13 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
String music = "mp3 wav wma mpa ram ra aac aif m4a";
String video = "avi mpg mpe mpeg asf wmv mov qt rm mp4 flv m4v webm ogv ogg";
String image = "bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg";
if (image.indexOf(type) != -1) {
if(image.indexOf(type) != -1){
return "图片";
} else if (documents.indexOf(type) != -1) {
} else if(documents.indexOf(type) != -1){
return "文档";
} else if (music.indexOf(type) != -1) {
} else if(music.indexOf(type) != -1){
return "音乐";
} else if (video.indexOf(type) != -1) {
} else if(video.indexOf(type) != -1){
return "视频";
} else return "其他";
}
@ -244,9 +236,8 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
String mimeType = new MimetypesFileTypeMap().getContentType("." + type);
return mimeType.split("\\/")[0];
}
public static void checkSize(long maxSize, long size) {
if (size > (maxSize * 1024 * 1024)) {
if(size > (maxSize * 1024 * 1024)){
throw new BadRequestException("文件超出规定大小");
}
}