com.luues.util.upload.webuploader.WebUploader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-util Show documentation
Show all versions of commons-util Show documentation
A Simple Tool Operations Class
package com.luues.util.upload.webuploader;
import com.luues.util.TypeConvert;
import com.luues.util.encryption.MD5Util;
import com.luues.util.uuid.JUUID;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class WebUploader {
/**
* webuploader切片上传
*
* @param file
* @param temporaryFolder 存放临时文件夹
* @param chunks
* @param chunk
* @param name
* @return
*/
public WebUploadInfo upload(MultipartFile file,
String guid,
String storageFolder,
String temporaryFolder,
String m3u8Folder,
String chunks,
String chunk,
String name) {
if (TypeConvert.isNull(file, guid, storageFolder, temporaryFolder)) {
WebUploadInfo webUploadInfo = new WebUploadInfo();
webUploadInfo.setError("文件上传失败");
return webUploadInfo;
}
String name_ = name.split("\\.")[0];
//guid生成一个专属该文件的文件夹
String guidFolder = MD5Util.MD5Encode(guid, "UTF-8");
//创建临时文件夹
File tempFileFolder = new File(temporaryFolder + "/" + guidFolder);
if (!tempFileFolder.exists()) {
tempFileFolder.mkdirs();
}
//创建储存原视频文件夹
File storageFileFolder = new File(storageFolder + "/" + guidFolder);
if (!storageFileFolder.exists()) {
storageFileFolder.mkdirs();
}
//创建储存m3u8文件夹
File m3u8FileFolder = new File(m3u8Folder + "/" + guidFolder);
if (!m3u8FileFolder.exists()) {
m3u8FileFolder.mkdirs();
}
try {
//判断上传的文件是否被分片(小于1M的不会分片)
if (null == chunks && null == chunk) {
File destTempFile = new File(storageFileFolder.getPath(), name);
file.transferTo(destTempFile);
destTempFile.createNewFile();
return new WebUploadInfo();
}
File partFile = new File(tempFileFolder.getPath() + "/" + name_ + "_" + chunk + ".part");
if (!partFile.exists()) {
partFile.createNewFile();
}
file.transferTo(partFile);
// 是否全部上传完成
// 所有分片都存在才说明整个文件上传完成
// 所有分片文件都上传完成
// 将所有分片文件合并到一个文件中
if (uploadAll(tempFileFolder.getPath(), Integer.valueOf(chunks))) {
String uuid = JUUID.getUUID();
m3u8FileFolder = new File(m3u8FileFolder.getPath(), uuid + ".mp4");
if (!m3u8FileFolder.exists()) {
m3u8FileFolder.createNewFile();
}
File m3u8File = new File(m3u8FileFolder.getParent(), uuid + ".m3u8");
if (!m3u8File.exists()) {
m3u8File.createNewFile();
}
storageFileFolder = new File(storageFileFolder.getPath(), name);
if (!storageFileFolder.exists()) {
storageFileFolder.createNewFile();
}
for (int i = 0; i < Integer.parseInt(chunks); i++) {
partFile = new File(tempFileFolder.getPath(), name_ + "_" + i + ".part");
FileOutputStream destTempfos = new FileOutputStream(storageFileFolder, true);
FileUtils.copyFile(partFile, destTempfos);
destTempfos.close();
}
FileUtils.deleteDirectory(tempFileFolder);
WebUploadInfo webUploadInfo = new WebUploadInfo();
webUploadInfo.setFid(JUUID.getUUID());
webUploadInfo.setStorageFileFolder(storageFileFolder.getPath());
webUploadInfo.setM3u8FileFolder(m3u8FileFolder.getPath());
webUploadInfo.setName(name_);
webUploadInfo.setM3u8File(m3u8File.getPath());
webUploadInfo.setGuidFolder(guidFolder);
webUploadInfo.setVideoName(name);
webUploadInfo.setVideoSize(storageFileFolder.length());
return webUploadInfo;
} else {
return new WebUploadInfo();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return new WebUploadInfo();
}
protected synchronized boolean uploadAll(String path, int chunks) {
int length = new File(path).listFiles().length;
if (length == chunks) {
return true;
}
return false;
}
}