package com.cuit.util;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.multipart.MultipartFile;
import com.cuit.domain.FileEntity;
public class FileUploadTool {
TransfMediaTool transfMediaTool = new TransfMediaTool();
// 文件最大500M
private static long upload_maxsize = 800 * 1024 * 1024;
// 文件允许格式
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" };
// 允许转码的视频格式(ffmpeg)
private static String[] allowFLV = { ".avi", ".mpg", ".wmv", ".3gp", ".mov", ".asf", ".asx", ".vob" };
// 允许的视频转码格式(mencoder)
private static String[] allowAVI = { ".wmv9", ".rm", ".rmvb" };
public FileEntity createFile(MultipartFile multipartFile, HttpServletRequest request) {
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;
System.out.println("文件类型不允许");
}
} else {
bflag = false;
System.out.println("文件大小超范围");
}
} else {
bflag = false;
System.out.println("文件为空");
}
if (bflag) {
String logoPathDir = "/video/";
String logoRealPathDir = request.getServletContext().getRealPath(logoPathDir);
// 上传到本地磁盘
// String logoRealPathDir = "E:/upload";
File logoSaveFile = new File(logoRealPathDir);
if (!logoSaveFile.exists()) {
logoSaveFile.mkdirs();
}
String name = fileName.substring(0, fileName.lastIndexOf("."));
System.out.println("文件名称:" + name);
// 新的文件名
String newFileName = this.getName(fileName);
// 文件扩展名
String fileEnd = this.getFileExt(fileName);
// 绝对路径
String fileNamedirs = logoRealPathDir + File.separator + newFileName + fileEnd;
System.out.println("保存的绝对路径:" + fileNamedirs);
File filedirs = new File(fileNamedirs);
// 转入文件
try {
multipartFile.transferTo(filedirs);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 相对路径
entity.setType(fileEnd);
String fileDir = logoPathDir + 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 = logoRealPathDir + File.separator + newFileName + ".avi";
// 获取配置的转换工具(mencoder.exe)的存放路径
String mencoderPath = request.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 = logoRealPathDir + File.separator + newFileName + ".flv";
// 获取配置的转换工具(ffmpeg.exe)的存放路径
String ffmpegPath = request.getServletContext().getRealPath("/tools/ffmpeg.exe");
transfMediaTool.processFLV(ffmpegPath, aviPath, codcFilePath);
fileDir = logoPathDir + newFileName + ".flv";
builder = new StringBuilder(fileDir);
finalFileDir = builder.substring(1);
} catch (Exception e) {
e.printStackTrace();
}
}
entity.setSize(size);
entity.setPath(finalFileDir);
entity.setTitleOrig(name);
entity.setTitleAlter(newFileName);
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
entity.setUploadTime(timestamp);
return entity;
} else {
return null;
}
} else {
return null;
}
}
/**
* 文件类型判断
*
* @param fileName
* @return
*/
private boolean checkFileType(String fileName) {
Iterator<String> type = Arrays.asList(allowFiles).iterator();
while (type.hasNext()) {
String ext = type.next();
if (fileName.toLowerCase().endsWith(ext)) {
return true;
}
}
return false;
}
/**
* 视频类型判断(flv)
*
* @param fileName
* @return
*/
private boolean checkMediaType(String fileEnd) {
Iterator<String> type = Arrays.asList(allowFLV).iterator();
while (type.hasNext()) {
String ext = type.next();
if (fileEnd.equals(ext)) {
return true;
}
}
return false;
}
/**
* 视频类型判断(AVI)
*
* @param fileName
* @return
*/
private boolean checkAVIType(String fileEnd) {
Iterator<String> type = Arrays.asList(allowAVI).iterator();
while (type.hasNext()) {
String ext = type.next();
if (fileEnd.equals(ext)) {
return true;
}
}
return false;
}
/**
* 获取文件扩展名
*
* @return string
*/
private String getFileExt(String fileName) {
return fileName.substring(fileName.lastIndexOf("."));
}
/**
* 依据原始文件名生成新文件名
*
* @return
*/
private String getName(String fileName) {
Random random = new Random();
return "" + random.nextInt(10000) + System.currentTimeMillis();
}
/**
* 文件大小,返回kb.mb
*
* @return
*/
private String getSize(File file) {
String size = "";
long fileLength = file.length();
DecimalFormat df = new DecimalFormat("#.00");
if (fileLength < 1024) {
size = df.format((double) fileLength) + "BT";
} else if (fileLength < 1048576) {
size = df.format((double) fileLength / 1024) + "KB";
} else if (fileLength < 1073741824) {
size = df.format((double) fileLength / 1048576) + "MB";
} else {
size = df.format((double) fileLength / 1073741824) + "GB";
}
return size;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
毕设-SSM实现精品课程网站.zip
共450个文件
js:165个
css:41个
jsp:39个
0 下载量 117 浏览量
2024-01-06
12:59:25
上传
评论
收藏 22.64MB ZIP 举报
温馨提示
毕设-SSM实现精品课程网站.zip
资源推荐
资源详情
资源评论
收起资源包目录
毕设-SSM实现精品课程网站.zip (450个子文件)
AdminController.class 7KB
UserController.class 5KB
ShiroRealm.class 3KB
UserServiceImpl.class 2KB
User.class 2KB
UserService.class 777B
UserMapper.class 771B
CryptographyUtil.class 617B
App.class 579B
.classpath 1KB
org.eclipse.wst.common.component 643B
org.eclipse.wst.jsdt.ui.superType.container 49B
bootstrap.css 143KB
bootstrap.min.css 118KB
samples.css 64KB
editor_ie8.css 37KB
editor_iequirks.css 37KB
editor_ie.css 36KB
editor_gecko.css 36KB
editor.css 36KB
bootstrap-theme.css 26KB
bootstrap-theme.min.css 23KB
dialog_ie8.css 15KB
dialog_iequirks.css 14KB
dialog_ie.css 14KB
dialog.css 13KB
codemirror.css 8KB
balloonpanel.css 5KB
sample.css 5KB
balloonpanel.css 4KB
balloonpanel.css 4KB
easyimage.css 3KB
contents.css 3KB
default.css 2KB
outputxhtml.css 2KB
fontello.css 2KB
wsc.css 1KB
toolbar.css 1KB
wsc.css 1KB
style.css 1KB
tableselection.css 1KB
balloontoolbar.css 994B
balloontoolbar.css 914B
neo.css 815B
style.css 738B
show-hint.css 662B
balloontoolbar.css 472B
style2.css 462B
dialog.css 396B
style3.css 395B
scayt.css 356B
style.css 248B
imagebase.css 185B
pncrt.dll 272KB
drv43260.dll 212KB
pthreadGC2.dll 86KB
glyphicons-halflings-regular.eot 20KB
fontello.eot 5KB
ffmpeg.exe 38.38MB
mencoder.exe 7.52MB
outputforflash.fla 84KB
spinner.gif 3KB
.gitignore 9B
datafiltering.html 46KB
index.html 15KB
inlineall.html 10KB
outputforflash.html 10KB
toolbar.html 9KB
magicline.html 8KB
fullpage.html 8KB
jquery.html 7KB
dialog.html 7KB
outputhtml.html 7KB
api.html 7KB
replacebyclass.html 7KB
replacebycode.html 7KB
xhtmlstyle.html 7KB
index.html 7KB
inlinebycode.html 6KB
index.html 6KB
inlinetextarea.html 5KB
easyimage.html 5KB
divreplace.html 4KB
uilanguages.html 4KB
enterkey.html 4KB
readonly.html 3KB
ajax.html 3KB
uicolor.html 2KB
tabindex.html 2KB
appendto.html 2KB
tmpFrameset.html 2KB
ciframe.html 2KB
index.html 143B
index.html 143B
FileUploadTool.java 7KB
TopicController.java 5KB
AdminController.java 5KB
NewsController.java 5KB
VideoController.java 4KB
SourceController.java 4KB
共 450 条
- 1
- 2
- 3
- 4
- 5
资源评论
Lei宝啊
- 粉丝: 1980
- 资源: 1330
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- LM324_datasheet.pdf
- 全新Storm+Core+API管理系统源码
- 基于RP2040的电子沙漏,使用RP2040游戏机开发板,灯板是74HC595驱动的8*8LED矩阵
- 基于SSM和VUE的商店POS积分管理系统(免费提供全套java开源项目源码+论文)
- 基于SpringBoot的“在线动漫信息平台”的设计与实现.rar
- NPP xml tools
- 基于python开发的树莓派RP2040的游戏机
- 基于SNMP网络设备MIB信息采集系统(免费提供全套java开源项目源码+论文)
- 基于SSM和VUE的五子棋游戏的设计(免费提供全套java开源项目源码+论文)
- qiun-data-charts
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功