@ResponseBody
@RequestMapping(value = "/uploadPhoto", method = RequestMethod.POST)
public ApiMessageVO uploadPhoto2(HttpServletRequest request,MultipartFile[] file, String domainId,String domainPassWord,String taskId) throws Exception {
// 验证文件类型
if(file == null || file.length < 1){
apiMessageVO.setMessage("文件不能为空");
return apiMessageVO;
}
String fileType = FileOperationUtil.getFileType(file[0].getInputStream());
if(!"jpg".equals(fileType) && !"png".equals(fileType)){
apiMessageVO.setMessage("上传文件类型不正确");
return apiMessageVO;
}
return apiMessageVO;
}
package org.zyyd.base.service.impl;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.zyyd.base.entity.vo.Message;
import org.zyyd.base.service.CacheBaseService;
import org.zyyd.base.service.FileService;
import org.zyyd.base.util.BasicService;
import org.zyyd.base.util.FileOperationUtil;
import org.zyyd.base.util.StringContentUtil;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
@Service
public class FileServiceImpl extends BasicService implements FileService {
@Autowired
private CacheBaseService cacheBaseService;
@Override
public Message saveTempFiles(MultipartFile[] file,String id) {
Message message = new Message();
Map fm = new HashMap();
Message m = new Message();
try {
if (file != null && file.length > 0) {
// Date createTime = DateUtil.getCurrentDate(DateUtil.DATE_STYLE5);
for (int i = 0; i < file.length; i++) {
MultipartFile f = file[i];
String fileName = f.getOriginalFilename(); // 文件名
String diskFileName = StringContentUtil.getUuid(); // 保存后的文件名
CommonsMultipartFile cf = (CommonsMultipartFile) f;
DiskFileItem fi = (DiskFileItem) cf.getFileItem();
File f1 = fi.getStoreLocation();
m = FileOperationUtil.saveFileToDisk(f1, fileName,
diskFileName, "/file/upload/" + id);
/*
* 返回文件url地址
*/
if (m.getSuccess()) {
try {
fm.put("fileUrl", (String) m.getResult().get("filePath"));
message.setSuccess(true);
message.setResult(fm);
// 删除文件
// FileOperationUtil.deleteFile2(path);
} catch (Exception e) {
Object o = m.getResult().get("savefile");
File ef = (File) o;
if (ef.exists()) {
ef.delete();
}
throw e;
}
}
}
}
message.setResult(fm);
} catch (Exception e) {
e.printStackTrace();
}
return m;
}
@Override
public Message deleteFile(JSONObject json) {
Message message = new Message();
if(StringUtils.isBlank(json.getString("filePath"))){
message.setSuccess(true);
message.setMessage("文件不存在");
}else{
try {
Boolean success = FileOperationUtil.deleteFile3(json.getString("filePath"));
if(success){
message.setMessage("删除成功");
}else{
message.setMessage("发生错误");
}
message.setSuccess(success);
} catch (Exception e) {
e.printStackTrace();
}
}
return message;
}
}
package org.zyyd.base.util;
import org.apache.commons.io.FileUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.zyyd.base.entity.vo.Message;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
*
* ProjectName:OnlineExam ClassName:FileOperationUtil Description:文件操作工具类
* author:Steven Data:2014年9月18日 下午4:40:45 Modifier:Steven Data:2014年9月18日
* 下午4:40:45 ModifyRemark:
*
* @version
*/
public class FileOperationUtil {
private List path1 = new ArrayList();// ffmpeg能处理的文件的地址集
private List path2 = new ArrayList();// ffmpeg不能处理的文件的地址集
private static ServletRequestAttributes attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
private static String FILE_DIR = "/file/";
// private static String MENCODER_PATH = ServletActionContext.getServletContext().getRealPath("/") + "/convertvedioutils/mencoder";
private static String MENCODER_PATH = attr.getRequest().getServletContext().getRealPath("/") + "/convertvedioutils/mencoder";
// private static String MENCODER_PATH = "E:/Workspaces/TestSpaces/OnlineExam/convertvedioutils/mencoder";
private static String FFMPEG_PATH = attr.getRequest().getServletContext().getRealPath("/") + "/convertvedioutils/ffmpeg";
// private static String FFMPEG_PATH = ServletActionContext.getServletContext().getRealPath("/") + "/convertvedioutils/ffmpeg";
// private static String FFMPEG_PATH = "E:/Workspaces/TestSpaces/OnlineExam/convertvedioutils/ffmpeg";
public final static Map FILE_TYPE_MAP = new HashMap();
private FileOperationUtil(){}
static{
getAllFileType(); //初始化文件类型信息
}
/**
* Discription:[getAllFileType,常见文件头信息]
*/
private static void getAllFileType()
{
FILE_TYPE_MAP.put("ffd8ffe000104a464946", "jpg"); //JPEG (jpg)
FILE_TYPE_MAP.put("89504e470d0a1a0a0000", "png"); //PNG (png)
FILE_TYPE_MAP.put("47494638396126026f01", "gif"); //GIF (gif)
FILE_TYPE_MAP.put("49492a00227105008037", "tif"); //TIFF (tif)
FILE_TYPE_MAP.put("424d228c010000000000", "bmp"); //16色位图(bmp)
FILE_TYPE_MAP.put("424d8240090000000000", "bmp"); //24位位图(bmp)
FILE_TYPE_MAP.put("424d8e1b030000000000", "bmp"); //256色位图(bmp)
FILE_TYPE_MAP.put("41433130313500000000", "dwg"); //CAD (dwg)
FILE_TYPE_MAP.put("3c21444f435459504520", "html"); //HTML (html)
FILE_TYPE_MAP.put("3c21646f637479706520", "htm"); //HTM (htm)
FILE_TYPE_MAP.put("48544d4c207b0d0a0942", "css"); //css
FILE_TYPE_MAP.put("696b2e71623d696b2e71", "js"); //js
FILE_TYPE_MAP.put("7b5c727466315c616e73", "rtf"); //Rich Text Format (rtf)
FILE_TYPE_MAP.put("38425053000100000000", "psd"); //Photoshop (psd)
FILE_TYPE_MAP.put("46726f6d3a203d3f6762", "eml"); //Email [Outlook Express 6] (eml)
FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "doc"); //MS Excel 注意:word、msi 和 excel的文件头一样
FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "vsd"); //Visio 绘图
FILE_TYPE_MAP.put("5374616E64617264204A", "mdb"); //MS Access (mdb)
FILE_TYPE_MAP.put("252150532D41646F6265", "ps");
FILE_TYPE_MAP.put("255044462d312e350d0a", "pdf"); //Adobe Acrobat (pdf)
FILE_TYPE_MAP.put("2e524d46000000120001", "rmvb"); //rmvb/rm相同
FILE_TYPE_MAP.put("464c5601050000000900", "flv"); //flv与f4v相同
FILE_TYPE_MAP.put("00000020667479706d70", "mp4");
FILE_TYPE_MAP.put("49443303000000002176", "mp3");
FILE_TYPE_MAP.put("000001ba210001000180", "mpg"); //
FILE_TYPE_MAP.put("3026b2758e66cf11a6d9", "wmv"); //wmv与asf相同
FILE_TYPE_MAP.put("52494646e27807005741", "wav"); //Wave (wav)
FILE_TYPE_MAP.put("52494646d07d60074156", "avi");
FILE_TYPE_MAP.put("4d546864000000060001", "mid"); //MIDI (mid)
FILE_TYPE_MAP.put("504b0304140000000800", "zip");
FILE_TYPE_MAP.put("526172211a0700cf9073", "rar");
FILE_TYPE_MAP.put("235468697320636f6e66", "ini");
FILE_TYPE_MAP.put("504b03040a0000000000", "jar");
FILE_TYPE_MAP.put("4d5a9000030000000400", "exe");//可执行文件
FILE_TYPE_MAP.put("3c25402070616765206c", "jsp");//jsp文件
FILE_TYPE_MAP.put("4d616e69666573742d56", "mf");//MF文件
FILE_TYPE_MAP.put("3c3f786d6c2076657273", "xml");//xml文件
FILE_TYPE_MAP.put("494e5345525420494e54", "sql");//xml文件
FILE_TYPE_MAP.put("7061636b616765207765", "java");//java文件
FILE_TYPE_MAP.put("406563686f206f66660d", "bat");//bat文件
FILE_TYPE_MAP.put("1f8b0800000000000000", "gz");//gz文件
FILE_TYPE_MAP.put("6c6f67346a2e726f6f74", "properties");//bat文件
FILE_TYPE_MAP.put("cafebabe0000002e0041", "class");//bat文件
FILE_TYPE_MAP.put("49545346030000006000", "chm");//bat文件
FILE_TYPE_MAP.put("04000000010000001300", "mxp");//bat文件
FILE_TYPE_MAP.put("504b0304140006000800", "docx");//docx文件
FILE_TYPE_MAP.put("d0cf11e0a1b11ae10000", "wps");//WPS文字wps、表格et、演示dps都是一样的
FILE_TYPE_MAP.put("6431303a637265617465", "torrent");
FILE_TYPE_MAP.put("6D6F6F76", "mov"); //Quicktime (mov)
FILE_TYPE_MAP.put("FF575043", "wpd"); //WordPerfect (wpd)
FILE_TYPE_MAP.put("CFAD12FEC5FD746F", "dbx"); //Outlook Express (dbx)
FILE_TYPE_MAP.put("2142444E", "pst"); //Outlook (pst)
FILE_TYPE_MAP.put("AC9EBD8F", "qdf"); //Quicken (qdf)
FILE_TYPE_MAP.put("E3828596", "pwl"); //Windows Password (pwl)
FILE_TYPE_MAP.put("2E7261FD", "ram"); //Real Audio (ram)
}
/**
* 得到上传文件的文件头
* @param src
* @return
*/
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
/**
* 根据制定文件的文件头判断其文件类型
* @param filePaht
* @return
*/
public static String getFileType(InputStream is){
String res = null;
try {
byte[] b = new byte[10];
is.read(b, 0, b.length);
String fileCode = bytesToHexString(b);
System.out.println(fileCode);
//这种方法在字典的头代码不够位数的时候可以用但是速度相对慢一点
Iterator keyIter = FILE_TYPE_MAP.keySet().iterator();
while(keyIter.hasNext()){
String key = keyIter.next();
if(key.toLowerCase().startsWith(fileCode.toLowerCase()) || fileCode.toLowerCase().startsWith(key.toLowerCase())){
res = FILE_TYPE_MAP.get(key);
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}
/**
*
* @Title: getProperties
* @Description: 得到src路径下指定名称的Properties文件
* @param @param fileName
* @param @return 设定文件
* @return Properties 返回类型
*/
public static Properties getProperties(String fileName) {
InputStream inputStream = new FileOperationUtil().getClass().getClassLoader().getResourceAsStream(fileName + ".properties");
Properties p = new Properties();
try{
p.load(inputStream);
} catch (IOException e1){
e1.printStackTrace();
}
return p;
}
/**
*
* @Title: saveFileToDisk
* @Description: 保存文件到磁盘
* @param @param uploadFile 保存的文件
* @param @param fileName 文件名
* @param @param diskFileName 保存到磁盘的文件名
* @param @param ContentType 文件类型
* @param @throws Exception 设定文件
* @return Message 返回类型
*/
public static Message saveFileToDisk(File uploadFile, String fileName, String diskFileName,
String ContentType) throws Exception {
Message message = null;
if (StringContentUtil.isNoEmpty(diskFileName)) {
message = new Message();
//HttpSession session = request.getSession();
//ServletContext application = session.getServletContext();
String externalName = fileName.substring(fileName.lastIndexOf("."))
.trim(); // 获取文件扩展名
attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
String savePath = attr.getRequest().getServletContext().getRealPath("/") + ContentType + "/";
// String savePath =
// ServletActionContext.getServletContext().getRealPath(FILE_DIR +
// ContentType)+ "/";
// String savePath2="G:\\AllWorkspace\\IDEA\\mine\\news\\target\\classes\\static\\file\\img";
File savefile = new File(new File(savePath), diskFileName
+ externalName);
if (!savefile.getParentFile().exists()) {
savefile.getParentFile().mkdirs();
}
FileUtils.copyFile(uploadFile, savefile);
Map fileInfo = new HashMap();
fileInfo.put("fileName", fileName);
fileInfo.put("filePath", ContentType + "/" + diskFileName
+ externalName);
fileInfo.put("fileExt", externalName);
fileInfo.put("fileType", ContentType);
fileInfo.put("savefile", savefile);
fileInfo.put("fileSize", savefile.length());
System.out.println(savefile);
message.setResult(fileInfo);
message.setSuccess(true);
}
return message;
}
/**
*
* @Title: saveFileToDisk
* @Description: 保存文件到磁盘
* @param @param request
* @param @param fileName 文件名
* @param @param filePath 路径
* @param @param ContentType 文件类型
* @param @throws Exception 设定文件
* @return Message 返回类型
* @throws
*/
public static Message saveFileToDisk(HttpServletRequest request,
String fileName, String filePath, String ContentType)
throws Exception {
Message message = new Message();
if (request != null) {
BufferedInputStream bis = new BufferedInputStream(
request.getInputStream());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
String savePath = attr.getRequest().getServletContext()
.getRealPath("/") + FILE_DIR + filePath + "/";
File savefile = new File(savePath + fileName + ContentType);
if (!savefile.getParentFile().exists()) {
savefile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(savefile);
BufferedOutputStream stream = new BufferedOutputStream(fos);
try {
int i = 1024;
byte[] buf = new byte[i];
while ((i = bis.read(buf, 0, i)) > 0) {
bos.write(buf, 0, i);
}
stream.write(bos.toByteArray());
stream.flush();
fos.flush();
message.setSuccess(true);
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
stream.close();
fos.close();
bos.close();
}
}
return message;
}
/**
* 保存文件到磁盘
* @updatetime :2016-6-12
* @param inputStream 文件流
* @param diskFileName 保存名字
* @param ContentType 保存文件类型
* @return
* @throws Exception
*/
public static Message saveFileToDisk(InputStream inputStream,
String diskFileName, String ContentType)
throws Exception {
Message message = new Message();
if (inputStream != null) {
BufferedInputStream bis = new BufferedInputStream(
inputStream);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
/*String savePath = attr.getRequest().getServletContext()
.getRealPath("/") + FILE_DIR + filePath + "/";*/
attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
String savePath = attr.getRequest().getServletContext().getRealPath("/") + FILE_DIR + ContentType + "/";
File savefile = new File(savePath + diskFileName);
if (!savefile.getParentFile().exists()) {
savefile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(savefile);
BufferedOutputStream stream = new BufferedOutputStream(fos);
try {
int i = 1024;
byte[] buf = new byte[i];
while ((i = bis.read(buf, 0, i)) > 0) {
bos.write(buf, 0, i);
}
stream.write(bos.toByteArray());
stream.flush();
fos.flush();
message.setSuccess(true);
message.getResult().put("filePath",FILE_DIR + ContentType + "/"+diskFileName);
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
stream.close();
fos.close();
bos.close();
}
}
return message;
}
/**
*
* @Title: deleteFile
* @Description: 根据路径删除文件
* @param @param path
* @param @throws Exception 设定文件
* @return boolean 返回类型
* @throws
*/
public static boolean deleteFile(String path) throws Exception {
boolean result = false;
if (StringContentUtil.isNoEmpty(path)) {
/*String savePath = ServletActionContext.getServletContext()
.getRealPath(FILE_DIR) + path;*/
File file = new File(path);
if (file.exists()) {
result = file.delete();
}
}
return result;
}
/**
* @Title: 根据相对路径删除,带file
* @Description:
* @author pengbin
* @date 2018/12/27 10:29
* @param
* @return
* @throws
*/
public static boolean deleteFile2(String fPath) throws Exception {
boolean result = false;
if (StringContentUtil.isNoEmpty(fPath)) {
attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
String path = attr.getRequest().getServletContext()
.getRealPath("/") + "file/" + fPath;
File file = new File(path);
if (file.exists()) {
File pFile = file.getParentFile();
result = file.delete();
File[] ls = pFile.listFiles();
if (ls.length <= 0) {
result = pFile.delete();
}
}
}
return result;
}
/**
* @Title: 根据相对路径删除,不带file,纯相对路径
* @Description:
* @author pengbin
* @date 2018/12/27 10:29
* @param
* @return
* @throws
*/
public static boolean deleteFile3(String fPath) throws Exception {
boolean result = false;
if (StringContentUtil.isNoEmpty(fPath)) {
attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
String path = attr.getRequest().getServletContext()
.getRealPath("/") + fPath;
File file = new File(path);
if (file.exists()) {
File pFile = file.getParentFile();
result = file.delete();
File[] ls = pFile.listFiles();
if (ls.length <= 0) {
result = pFile.delete();
}
}
}
return result;
}
public static boolean deleteFile(String ContentType, String fileName) throws Exception {
boolean result = false;
if (StringContentUtil.isNoEmpty(ContentType) && StringContentUtil.isNoEmpty(fileName)) {
String path = attr.getRequest().getServletContext()
.getRealPath("/") + FILE_DIR + ContentType + "/";
File file = new File(path + fileName);
if (file.exists()) {
result = file.delete();
}
}
return result;
}
public static boolean deleteByFilePath(String filePath) throws Exception {
boolean result = false;
if (StringContentUtil.isNoEmpty(filePath)) {
attr = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
String path = attr.getRequest().getServletContext()
.getRealPath("/") + FILE_DIR + filePath;
File file = new File(path);
if (file.exists()) {
result = file.delete();
}
}
return result;
}
public static boolean findFile(String ContentType, String fileName) throws Exception {
boolean result = false;
if (StringContentUtil.isNoEmpty(ContentType) && StringContentUtil.isNoEmpty(fileName)) {
String path = attr.getRequest().getServletContext()
.getRealPath("/") + FILE_DIR + ContentType + "/";
File file = new File(path + fileName);
if (file.exists()) {
result = true;
}
}
return result;
}
public static boolean convertVideo(HttpServletRequest request, File uploadFile, String fileName, String diskFileName, String ContentType) throws Exception {
HttpSession session = request.getSession();
ServletContext application = session.getServletContext();
String externalName = fileName.substring(fileName.lastIndexOf("."))
.trim(); // 获取文件扩展名
String savePath = attr.getRequest().getServletContext()
.getRealPath("/") + "/filedir/temp/";
String vedioBasePath = attr.getRequest().getServletContext()
.getRealPath("/") + "/filedir/media/";
/*String otherPath = ServletActionContext.getServletContext()
.getRealPath("/") + "/filedir/media/other/";*/
File savefile = new File(new File(savePath), diskFileName
+ externalName);
if (!savefile.getParentFile().exists()) {
savefile.getParentFile().mkdirs();
}
FileUtils.copyFile(uploadFile, savefile);
boolean flag = processVedio(savefile, vedioBasePath);
savefile.delete();
return flag;
}
public static boolean processVedio(File f, String filePath) throws Exception {
String name = f.getName();
boolean result = false;
if (name.matches(".+\\.(avi|wmv|mkv|asx|swf|asf|vob|mp3|mp4|mpg|mov|flv)$")) {
result= pFLV(f, filePath);
if (result) {
result= pMP4(f, filePath);
}
} else {
result= pFLV1(f, filePath);
if (result) {
result= pMP4(f, filePath);
}
}
return result;
}
public static String pAVI(File f, String filePath) throws Exception {
// String utilPath = "E:/Workspaces/TestSpaces/OnlineExam/convertvedioutils/mencoder";
String str = f.getPath();
String name = f.getName();
List commend = new ArrayList();
commend.add(MENCODER_PATH);
commend.add("\"" + str + "\"");
commend.add("-oac");
commend.add("mp3lame");
commend.add("-lameopts");
commend.add("preset=64");
commend.add("-ovc");
commend.add("xvid");
commend.add("-xvidencopts");
commend.add("bitrate=600");
commend.add("-of");
commend.add("avi");
commend.add("-o");
String file = name.substring(0, name.lastIndexOf("."));
String fileName = file + ".avi";
String saveFilePath = filePath + "flv/" + fileName;
fileDirIsExit(saveFilePath);
commend.add("\"" + saveFilePath + "\"");// 最后输出出来的avi,尽量不要出现二义性,否则会覆盖掉原有的视频
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process p = builder.start();
int exitValue = doWaitFor(p);
if (exitValue != -1) {
return saveFilePath;
}
} catch (Exception e) {
e.printStackTrace();
//System.out.println("********avi转换出错********");
}
return null;
}
public static boolean pFLV(File f, String filePath) throws Exception {
// String utilPath = "E:/Workspaces/TestSpaces/OnlineExam/convertvedioutils/ffmpeg";
String str = f.getPath();
String name = f.getName();
List commend = new ArrayList();
commend.add(FFMPEG_PATH);
commend.add("-i");
commend.add("\"" + str + "\"");
commend.add("-ab");
commend.add("64");
commend.add("-ac");
commend.add("2");
commend.add("-ar");
commend.add("22050");
commend.add("-b");
commend.add("1000");
//commend.add("-r");
//commend.add("29.97");
commend.add("-qscale");
commend.add("4");
commend.add("-y");
String file = name.substring(0, name.lastIndexOf("."));
String fileName = file + ".flv";
String saveFilePath = filePath + "flv/" + fileName;
fileDirIsExit(saveFilePath);
commend.add("\"" + saveFilePath + "\"");
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process p = builder.start();
int exitValue = doWaitFor(p);
if (exitValue != -1) {
processImg(fileName, filePath);
return true;
}
} catch (Exception e) {
// System.out.println("*********转换为FLV格式出错*********");
e.printStackTrace();
return false;
}
return true;
}
public static boolean pFLV1(File f, String filePath) throws Exception {
String aviPath = pAVI(f, filePath);
if (StringContentUtil.isNoEmpty(aviPath)) {
// String utilPath = "E:/Workspaces/TestSpaces/OnlineExam/convertvedioutils/ffmpeg";
String str = aviPath;
List commend = new ArrayList();
commend.add(FFMPEG_PATH);
commend.add("-i");
commend.add("\"" + aviPath + "\"");
commend.add("-ab");
commend.add("64");
commend.add("-ac");
commend.add("2");
commend.add("-ar");
commend.add("22050");
commend.add("-b");
commend.add("1500");
// commend.add("-r");
//commend.add("29.97");
commend.add("-qscale");
commend.add("4");
commend.add("-y");
String file = str.substring(str.lastIndexOf("/") + 1,
str.lastIndexOf("."));
String fileName = file + ".flv";
String saveFilePath = filePath + "flv/" + fileName;
fileDirIsExit(saveFilePath);
commend.add("\"" + saveFilePath + "\"");
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process p = builder.start();
int exitValue = doWaitFor(p);
if (exitValue != -1) {
processImg(fileName, filePath);
return true;
}
} catch (Exception e) {
//System.out.println("*********转换为FLV格式出错*********");
e.printStackTrace();
return false;
} finally {
deleteFile(aviPath);
}
}
return false;
}
public static boolean pMP4(File f, String filePath) throws Exception {
// String utilPath = "E:/Workspaces/TestSpaces/OnlineExam/convertvedioutils/mencoder";
String str = f.getPath();
String name = f.getName();
List commend = new ArrayList();
commend.add(MENCODER_PATH);
commend.add("\"" + str + "\"");
commend.add("-oac");
commend.add("faac");
commend.add("-faacopts");
commend.add("mpeg=4:object=2:raw:br=12");
//commend.add("-ofps");
//commend.add("15");
//commend.add("-lameopts");
//commend.add("preset=64");
commend.add("-ovc");
commend.add("x264");
commend.add("-x264encopts");
commend.add("nocabac:level_idc=30:bframes=0:bitrate=512:threads=auto:turbo=1:global_header:threads=auto:subq=5:frameref=6:partitions=all:trellis=1:chroma_me:me=umh");
//commend.add("bitrate=440:global_header");
//commend.add("-xvidencopts");
// commend.add("bitrate=600");
//commend.add("-vf");
//commend.add("scale=720:576");
commend.add("-of");
commend.add("lavf");
commend.add("-lavfopts");
commend.add("format=mp4");
commend.add("-o");
String file = name.substring(0, name.lastIndexOf("."));
String fileName = file + ".mp4";
String saveFilePath = filePath + "other/" + fileName;
fileDirIsExit(saveFilePath);
commend.add(saveFilePath);// 最后输出出来的avi,尽量不要出现二义性,否则会覆盖掉原有的视频
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process p = builder.start();
int exitValue = doWaitFor(p);
if (exitValue != -1) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
//System.out.println("********mp4转换出错********");
}
return false;
}
public static boolean processImg(String fileName, String filePath) {
List commend = new ArrayList();
commend.add(FFMPEG_PATH);
commend.add("-i");
commend.add("\"" + fileName + "\"");
commend.add("-y");
commend.add("-f");
commend.add("image2");
commend.add("-ss");
commend.add("5");
commend.add("-t");
commend.add("0.001");
commend.add("-s");
commend.add("320x240");
String saveFilePath = filePath + "img/" + fileName + ".jpg";
//commend.add("\"e:\\img\\"+ fileName.substring(10, fileName.lastIndexOf(".")) + ".jpg\"");
commend.add(saveFilePath);
try {
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
builder.start();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static int doWaitFor(Process p) throws Exception {
InputStream in = null;
InputStream err = null;
int exitValue = -1; // returned to caller when p is finished
try {
//System.out.println("comeing");
in = p.getInputStream();
err = p.getErrorStream();
boolean finished = false; // Set to true when p is finished
while (!finished) {
try {
while (in.available() > 0) {
// Print the output of our system call
Character c = new Character((char) in.read());
//System.out.print(c);
}
while (err.available() > 0) {
// Print the output of our system call
Character c = new Character((char) err.read());
//System.out.print(c);
}
// Ask the process for its exitValue. If the process
// is not finished, an IllegalThreadStateException
// is thrown. If it is finished, we fall through and
// the variable finished is set to true.
exitValue = p.exitValue();
finished = true;
} catch (IllegalThreadStateException e) {
// Process is not finished yet;
// Sleep a little to save on CPU cycles
Thread.currentThread().sleep(500);
}
}
} catch (Exception e) {
// unexpected exception! print it out for debugging...
//System.err.println("doWaitFor();: unexpected exception - " + e.getMessage());
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
//System.out.println(e.getMessage());
}
if (err != null) {
try {
err.close();
} catch (IOException e) {
e.printStackTrace();
//System.out.println(e.getMessage());
}
}
}
// return completion status to caller
return exitValue;
}
public static void fileDirIsExit(File savefile) throws Exception {
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
}
public static void fileDirIsExit(String path) throws Exception {
File savefile = new File(path);
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
}
public static String getContextPath() throws Exception {
return attr.getRequest().getServletContext().getRealPath("/") + FILE_DIR;
}
public static String getContextPath2() throws Exception {
return attr.getRequest().getServletContext().getRealPath("/");
}
public static String getServerPath(HttpServletRequest request){
String path = request.getContextPath();
String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + FILE_DIR;
return basePath;
}
/**
* 将文件转成base64 字符串
* @param path文件路径
* @return *
* @throws Exception
*/
public static String encodeBase64File(String path) throws Exception {
File file = new File(path);;
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
return new BASE64Encoder().encode(buffer);
}
/**
* 将base64字符解码保存文件
* @param base64Code
* @param targetPath
* @throws Exception
*/
public static void decoderBase64File(String base64Code, String targetPath)
throws Exception {
byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
/**
* 将base64字符保存文本文件
* @param base64Code
* @param targetPath
* @throws Exception
*/
public static void toFile(String base64Code, String targetPath)
throws Exception {
byte[] buffer = base64Code.getBytes();
FileOutputStream out = new FileOutputStream(targetPath);
out.write(buffer);
out.close();
}
}