//处理认证照片/视频
List<String> imgIds = Lists.newArrayList();
String[] array = {".jpg", ".jepg", ".png", ".gif", ".bmp", ".tiff", ".tif", ".raw", ".svg", ".ico", ".psd", ".ai", ".eps"};
List<String> list = Arrays.asList(array);
if (auditImgs != null && auditImgs.length > 0) {
//循环获取file数组中得文件
for (int i = 0; i < auditImgs.length; i++) {
MultipartFile file = auditImgs[i];
MultipartFile filepath = null;
String fileUrl = "";
//压缩文件大小
try {
//filepath = VideoCompression.compressVideoByMultipartFile(file);
//拿到文件url
fileUrl = SystemUtils.storageFile(IdGen.uuid(), file.getBytes(), getFileExtension(file.getOriginalFilename()));
//转file
File file1 = MultipartFileAndFileUtils.toFile(file);
//拿到文件名忽略大小写
String name = file1.getName().toLowerCase();
//拿到字符最后出现的位置
int indexs = name.lastIndexOf(".");
//指定下标截取(这里是截取文件名后缀)
String substring = name.substring(indexs);
//判断是否为图片,是图片就直接上传
if (list.contains(substring)) {
FacePhoto facePhoto = new FacePhoto();
facePhoto.setUrl(fileUrl);
facePhoto.setOriginalFilename(file.getOriginalFilename());
facePhoto.setExtName(getFileExtension(file.getOriginalFilename()));
facePhoto.preInsert();
facePhotoDao.insert(facePhoto);
imgIds.add(facePhoto.getId());
} else {
//视频压缩上传
StringBuffer stringBuffer = new StringBuffer(fileUrl);
StringBuffer aNew = stringBuffer.insert(fileUrl.lastIndexOf("."), "New");
File newFile = new File(String.valueOf(aNew));
compress(file1, newFile);
System.out.println("压缩新文件地址" + newFile);
FacePhoto facePhoto = new FacePhoto();
facePhoto.setUrl(fileUrl);
facePhoto.setOriginalFilename(file.getOriginalFilename());
facePhoto.setExtName(getFileExtension(file.getOriginalFilename()));
facePhoto.preInsert();
facePhotoDao.insert(facePhoto);
imgIds.add(facePhoto.getId());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
必需的方法
package com.thinkgem.jeesite.modules.silence.web.test.test02;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class MultipartFileAndFileUtils {
/**
* MultipartFile 转 File
* @param file
* @throws Exception
*/
public static File toFile(MultipartFile file) throws Exception {
File toFile = null;
if (file.equals("") || file.getSize() <= 0) {
file = null;
} else {
InputStream ins = null;
ins = file.getInputStream();
toFile = new File(file.getOriginalFilename());
inputStreamToFile(ins, toFile);
ins.close();
}
return toFile;
}
/**
* File 转 MultipartFile
* @param file
* @throws Exception
*/
public static MultipartFile toMultipartFile(File file) throws Exception {
InputStream inputStream = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),"application/octet-stream", inputStream);
return multipartFile;
}
//获取流文件
private static void inputStreamToFile(InputStream ins, File file) {
try {
OutputStream os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
ins.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除本地临时文件
* @param file
*/
public static void delteTempFile(File file) {
if (file != null) {
File del = new File(file.toURI());
del.delete();
}
}
}