|
@@ -1,6 +1,7 @@
|
|
|
package cn.com.ctop.common.module.utils;
|
|
|
|
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
@@ -11,6 +12,7 @@ import java.text.DecimalFormat;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
+@Slf4j
|
|
|
public class FileUploadTool {
|
|
|
|
|
|
|
|
@@ -18,117 +20,97 @@ public class FileUploadTool {
|
|
|
/**
|
|
|
* 文件最大500M
|
|
|
*/
|
|
|
- private static long UPLOAD_MAXSIZE = 800 * 1024 * 1024;
|
|
|
+ private static long uploadMaxsize = 800 * 1024 * 1024L;
|
|
|
/**
|
|
|
* 文件允许格式
|
|
|
*/
|
|
|
- private static String[] ALLOWFILES = {".rar", ".doc", ".docx", ".zip",
|
|
|
+ private static String[] allowfiles = {".rar", ".doc", ".docx", ".zip",
|
|
|
".pdf", ".txt", ".swf", ".xlsx", ".gif", ".png", ".jpg", ".jpeg",
|
|
|
".bmp", ".xls", ".mp4", ".flv", ".ppt", ".avi", ".mpg", ".wmv",
|
|
|
".3gp", ".mov", ".asf", ".asx", ".vob", ".wmv9", ".rm", ".rmvb", ".apk"};
|
|
|
/**
|
|
|
* 允许转码的视频格式(ffmpeg)
|
|
|
*/
|
|
|
- private static String[] ALLOWFLV = {".avi", ".mpg", ".wmv", ".3gp",
|
|
|
+ private static String[] allowflv = {".avi", ".mpg", ".wmv", ".3gp",
|
|
|
".mov", ".asf", ".asx", ".vob"};
|
|
|
|
|
|
/**
|
|
|
* 允许的视频转码格式(mencoder)
|
|
|
*/
|
|
|
- private static String[] ALLOWAVI = {".wmv9", ".rm", ".rmvb"};
|
|
|
+ private static String[] allowavi = {".wmv9", ".rm", ".rmvb"};
|
|
|
|
|
|
public FileEntity createFile(MultipartFile multipartFile, HttpServletRequest request, String savaPath) {
|
|
|
FileEntity entity = new FileEntity();
|
|
|
- boolean bflag = false;
|
|
|
- String fileName = multipartFile.getOriginalFilename().toString();
|
|
|
- // 判断文件不为空
|
|
|
- if (multipartFile.getSize() != 0 && !multipartFile.isEmpty()) {
|
|
|
- bflag = true;
|
|
|
- // 判断文件大小
|
|
|
- if (multipartFile.getSize() <= UPLOAD_MAXSIZE) {
|
|
|
- bflag = true;
|
|
|
- // 文件类型判断
|
|
|
- if (this.checkFileType(fileName)) {
|
|
|
- bflag = true;
|
|
|
- } else {
|
|
|
- bflag = false;
|
|
|
- }
|
|
|
- } else {
|
|
|
- bflag = false;
|
|
|
- }
|
|
|
- } else {
|
|
|
- bflag = false;
|
|
|
+ String fileName = multipartFile.getOriginalFilename();
|
|
|
+ // 判断文件为空
|
|
|
+ if (multipartFile.isEmpty() || multipartFile.getSize() == 0) {
|
|
|
+ return null;
|
|
|
}
|
|
|
- if (bflag) {
|
|
|
- File logoSaveFile = new File(savaPath);
|
|
|
- if (!logoSaveFile.exists()) {
|
|
|
- logoSaveFile.mkdirs();
|
|
|
- }
|
|
|
- String name = fileName.substring(0, fileName.lastIndexOf("."));
|
|
|
- // 新的文件名
|
|
|
- String newFileName = this.getName(fileName);
|
|
|
- // 文件扩展名
|
|
|
- String fileEnd = this.getFileExt(fileName);
|
|
|
- // 绝对路径
|
|
|
- String fileNamedirs = savaPath + File.separator + newFileName + fileEnd;
|
|
|
- File filedirs = new File(fileNamedirs);
|
|
|
- // 转入文件
|
|
|
- try {
|
|
|
- multipartFile.transferTo(filedirs);
|
|
|
- } catch (IllegalStateException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- // 相对路径
|
|
|
- entity.setType(fileEnd);
|
|
|
- String fileDir = savaPath + newFileName + fileEnd;
|
|
|
- StringBuilder builder = new StringBuilder(fileDir);
|
|
|
- String finalFileDir = builder.substring(1);
|
|
|
- // size存储为String
|
|
|
- String size = this.getSize(filedirs);
|
|
|
- // 源文件保存路径
|
|
|
- String aviPath = filedirs.getAbsolutePath();
|
|
|
- // 转码Avi
|
|
|
-// boolean flag = false;
|
|
|
- if (this.checkAviType(fileEnd)) {
|
|
|
- // 设置转换为AVI格式后文件的保存路径
|
|
|
- String codcAviPath = savaPath + File.separator + newFileName + ".avi";
|
|
|
- // 获取配置的转换工具(mencoder.exe)的存放路径
|
|
|
- String mencoderPath = request.getSession().getServletContext().getRealPath("/tools/mencoder.exe");
|
|
|
- aviPath = transfMediaTool.processAvi(mencoderPath, filedirs.getAbsolutePath(), codcAviPath);
|
|
|
- fileEnd = this.getFileExt(codcAviPath);
|
|
|
- }
|
|
|
- if (aviPath != null) {
|
|
|
- // 转码Flv
|
|
|
- if (this.checkMediaType(fileEnd)) {
|
|
|
- try {
|
|
|
- // 设置转换为flv格式后文件的保存路径
|
|
|
- String codcFilePath = savaPath + File.separator + newFileName + ".flv";
|
|
|
- // 获取配置的转换工具(ffmpeg.exe)的存放路径
|
|
|
- String ffmpegPath = request.getSession().getServletContext().getRealPath("/tools/ffmpeg.exe");
|
|
|
- transfMediaTool.processFlv(ffmpegPath, aviPath, codcFilePath);
|
|
|
- fileDir = savaPath + newFileName + ".flv";
|
|
|
- builder = new StringBuilder(fileDir);
|
|
|
- finalFileDir = builder.substring(1);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- entity.setSize(size);
|
|
|
- entity.setPath(fileNamedirs);
|
|
|
- entity.setTitleOrig(name);
|
|
|
- entity.setTitleAlter(newFileName);
|
|
|
- Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
|
|
- entity.setUploadTime(timestamp);
|
|
|
- return entity;
|
|
|
- } else {
|
|
|
- return null;
|
|
|
- }
|
|
|
- } else {
|
|
|
+
|
|
|
+ // 判断文件大小(大于文件大小限制)
|
|
|
+ if (multipartFile.getSize() > uploadMaxsize) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 文件类型判断
|
|
|
+ if (!this.checkFileType(fileName)) {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ File logoSaveFile = new File(savaPath);
|
|
|
+ if (!logoSaveFile.exists()) {
|
|
|
+ logoSaveFile.mkdirs();
|
|
|
+ }
|
|
|
+ String name = fileName.substring(0, fileName.lastIndexOf('.'));
|
|
|
+ // 新的文件名
|
|
|
+ String newFileName = this.getName(fileName);
|
|
|
+ // 文件扩展名
|
|
|
+ String fileEnd = this.getFileExt(fileName);
|
|
|
+ // 绝对路径
|
|
|
+ String fileNamedirs = savaPath + File.separator + newFileName + fileEnd;
|
|
|
+ File filedirs = new File(fileNamedirs);
|
|
|
+ // 转入文件
|
|
|
+ try {
|
|
|
+ multipartFile.transferTo(filedirs);
|
|
|
+ } catch (IllegalStateException | IOException e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ // 相对路径
|
|
|
+ entity.setType(fileEnd);
|
|
|
+ // size存储为String
|
|
|
+ String size = this.getSize(filedirs);
|
|
|
+ // 源文件保存路径
|
|
|
+ String aviPath = filedirs.getAbsolutePath();
|
|
|
+ // 转码Avi
|
|
|
+ if (this.checkAviType(fileEnd)) {
|
|
|
+ // 设置转换为AVI格式后文件的保存路径
|
|
|
+ String codcAviPath = savaPath + File.separator + newFileName + ".avi";
|
|
|
+ // 获取配置的转换工具(mencoder.exe)的存放路径
|
|
|
+ String mencoderPath = request.getSession().getServletContext().getRealPath("/tools/mencoder.exe");
|
|
|
+ aviPath = transfMediaTool.processAvi(mencoderPath, filedirs.getAbsolutePath(), codcAviPath);
|
|
|
+ fileEnd = this.getFileExt(codcAviPath);
|
|
|
+ }
|
|
|
+ if (aviPath == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 转码Flv
|
|
|
+ if (this.checkMediaType(fileEnd)) {
|
|
|
+ try {
|
|
|
+ // 设置转换为flv格式后文件的保存路径
|
|
|
+ String codcFilePath = savaPath + File.separator + newFileName + ".flv";
|
|
|
+ // 获取配置的转换工具(ffmpeg.exe)的存放路径
|
|
|
+ String ffmpegPath = request.getSession().getServletContext().getRealPath("/tools/ffmpeg.exe");
|
|
|
+ transfMediaTool.processFlv(ffmpegPath, aviPath, codcFilePath);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ entity.setSize(size);
|
|
|
+ entity.setPath(fileNamedirs);
|
|
|
+ entity.setTitleOrig(name);
|
|
|
+ entity.setTitleAlter(newFileName);
|
|
|
+ Timestamp timestamp = new Timestamp(System.currentTimeMillis());
|
|
|
+ entity.setUploadTime(timestamp);
|
|
|
+ return entity;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -138,7 +120,7 @@ public class FileUploadTool {
|
|
|
* @return
|
|
|
*/
|
|
|
private boolean checkFileType(String fileName) {
|
|
|
- Iterator<String> type = Arrays.asList(ALLOWFILES).iterator();
|
|
|
+ Iterator<String> type = Arrays.asList(allowfiles).iterator();
|
|
|
while (type.hasNext()) {
|
|
|
String ext = type.next();
|
|
|
if (fileName.toLowerCase().endsWith(ext)) {
|
|
@@ -155,7 +137,7 @@ public class FileUploadTool {
|
|
|
* @return
|
|
|
*/
|
|
|
private boolean checkMediaType(String fileEnd) {
|
|
|
- Iterator<String> type = Arrays.asList(ALLOWFLV).iterator();
|
|
|
+ Iterator<String> type = Arrays.asList(allowflv).iterator();
|
|
|
while (type.hasNext()) {
|
|
|
String ext = type.next();
|
|
|
if (fileEnd.equals(ext)) {
|
|
@@ -172,7 +154,7 @@ public class FileUploadTool {
|
|
|
* @return
|
|
|
*/
|
|
|
private boolean checkAviType(String fileEnd) {
|
|
|
- Iterator<String> type = Arrays.asList(ALLOWAVI).iterator();
|
|
|
+ Iterator<String> type = Arrays.asList(allowavi).iterator();
|
|
|
while (type.hasNext()) {
|
|
|
String ext = type.next();
|
|
|
if (fileEnd.equals(ext)) {
|
|
@@ -188,7 +170,7 @@ public class FileUploadTool {
|
|
|
* @return string
|
|
|
*/
|
|
|
private String getFileExt(String fileName) {
|
|
|
- return fileName.substring(fileName.lastIndexOf("."));
|
|
|
+ return fileName.substring(fileName.lastIndexOf('.'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -197,12 +179,11 @@ public class FileUploadTool {
|
|
|
* @return
|
|
|
*/
|
|
|
private String getName(String fileName) {
|
|
|
- Iterator<String> type = Arrays.asList(ALLOWFILES).iterator();
|
|
|
+ Iterator<String> type = Arrays.asList(allowfiles).iterator();
|
|
|
while (type.hasNext()) {
|
|
|
String ext = type.next();
|
|
|
if (fileName.contains(ext)) {
|
|
|
- String newFileName = fileName.substring(0, fileName.lastIndexOf(ext));
|
|
|
- return newFileName;
|
|
|
+ return fileName.substring(0, fileName.lastIndexOf(ext));
|
|
|
}
|
|
|
}
|
|
|
return "";
|