webuploader java上传文件后台源码 mpush

controller 包含MD5计算和断点续传
/**
* ChengDu ShuXing Technology Co., Ltd. CopyRight
*/
package com.shuxing.mpush.web.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.shuxing.mpush.service.ResourceService;
import com.shuxing.mpush.service.UserService;
import com.shuxing.mpush.service.business.FileChunkInfoRedisCacheService;
import com.shuxing.mpush.service.business.UserUnuploadFileInfoService;
import com.shuxing.mpush.service.common.ServiceConstant;
import com.shuxing.mpush.util.CommonUtils;
import com.shuxing.mpush.web.common.PermConstants;
import com.shuxing.mpush.web.common.WebConstants;
/**
* @author xiaobing yang
* @version 创建时间:2014年6月25日 上午10:24:21
*
*/
@Controller
@RequestMapping("/fileUpload")
public class FileUploadController
{
@Autowired
private FileChunkInfoRedisCacheService fileChunkInfoRedisCacheService;
@Autowired
private UserUnuploadFileInfoService userUnuploadFileInfoService;
private static final Logger logger = LoggerFactory.getLogger(FileUploadController.class);
@Autowired
private ResourceService resourceService;
@Autowired
private UserService userService;
@RequestMapping("toRtmpAdd")
@RequiresPermissions(PermConstants.RESOURCE_RTMP)
public ModelAndView toRtmpAdd(){
ModelAndView m = new ModelAndView();
m.setViewName("fileUpload/rtmpAddView");
return m;
}
@RequestMapping("ImgUpload")
public ModelAndView fileUpload(){
ModelAndView m = new ModelAndView();
m.setViewName("fileUpload/ImgUploader");
return m;
}
@RequestMapping("VideoUpload")
public ModelAndView VideoUpload(){
ModelAndView m = new ModelAndView();
m.setViewName("fileUpload/VideoUploader");
return m;
}
@RequestMapping("AudioUpload")
public ModelAndView AudioUpload(){
ModelAndView m = new ModelAndView();
m.setViewName("fileUpload/AudioUploader");
return m;
}
@RequestMapping("webuploader")
public ModelAndView ImgUpload(){
ModelAndView m = new ModelAndView();
// m.setViewName("webuploader/webuploader");
m.setViewName("webuploader/webuploader");
return m;
}
@ResponseBody
@RequestMapping("deleteUplaodFileCache")
public Map<String,Object> deleteUplaodFileCache(HttpServletRequest request,
HttpServletResponse response){
Map<String,Object> result = new HashMap<String,Object>();
String userName =(String) request.getSession().getAttribute(WebConstants.USER_NAME);
try
{
String fileMd5 = request.getParameter("fileMd5");
this.resourceService.deleteUplaodFileCache(userName,fileMd5);
result.put("result", true);
result.put("msg", "删除缓存成功");
}
catch (Exception e)
{
result.put("result", false);
result.put("msg", "删除缓存失败");
e.printStackTrace();
}
return result;
}
@ResponseBody
@RequestMapping("getUnUploadFile")
public Map<String,Object> getUnUploadFile(HttpServletRequest request,
HttpServletResponse response){
String userName =(String) request.getSession().getAttribute(WebConstants.USER_NAME);
return this.resourceService.getUnUploadFile(userName);
}
/**
* 先判断系统存在这个文件不,
* 系统不存在这个文件,返回false
* 如果系统存在,判断上传人存在这个文件不,
* 如果系统存在,上传人存在这个文件,返回true 直接显示上传成功
* 如果系统存在,上传人不存在这个文件,把文件与自己绑定,直接显示上传成功
* @param request
* @param response
* @return
*/
@ResponseBody
@RequestMapping("existsMd5")
public Map<String,Object> existsMd5(HttpServletRequest request,
HttpServletResponse response){
String fileMd5 = request.getParameter("fileMd5");
String fileName = request.getParameter("fileName");
Integer isShared = Integer.parseInt(request.getParameter("isShared"));
String userName =(String) request.getSession().getAttribute(WebConstants.USER_NAME);
Map<String,Object> result = this.resourceService.checkFileExistsByMd5AndUserName(userName,fileMd5,fileName,isShared);
return result;
}
@ResponseBody
@RequestMapping("chunkUpload")
public Map<String,Object> chunkUpload(HttpServletRequest request,
HttpServletResponse response)
{
Map<String,Object> result = new HashMap<String,Object>();
HttpSession session = request.getSession();
String userName = (String) session.getAttribute(WebConstants.USER_NAME);
String userNameKey = ServiceConstant.USER_UNUPLOAD_PREFIX+userName;
try
{
String chunkFileName;
String fileMd5 = request.getParameter("fileMd5");
Long fileSize = Long.parseLong(request.getParameter("size"));
String fileName =request.getParameter("name");
Integer fileType = Integer.parseInt(request.getParameter("fileType"));
Integer isShared = Integer.parseInt(request.getParameter("isShared"));
Integer chunks=0,chunk=0;
String chunkStr = request.getParameter("chunk");
String chunksStr = request.getParameter("chunks");
String chunkMd5 = request.getParameter("chunkMd5");
if(CommonUtils.isEmpty(fileMd5)){
chunkFileName = fileName+fileSize;
}else{
chunkFileName = fileMd5;
}
String tempFileDirectory = System.getProperty("jboss.server.temp.dir")+ File.separator+ ServiceConstant.UPLOAD_FOLDER_TEMP;
// 临时文件目录不存在创建目录
File tempSavePath = new File(tempFileDirectory);
if(!tempSavePath.exists())
{
tempSavePath.mkdirs();
}
MultipartHttpServletRequest mutilrequest = (MultipartHttpServletRequest) request;
MultipartFile file = mutilrequest.getFile("file");//获得上传的文件
//获得文件名
// String fileName = file.getOriginalFilename();
String tempFilePathName = tempFileDirectory+File.separator+chunkFileName;
String tempFilePathNameChunk = tempFilePathName;
Map<String,String> fileInfo = new HashMap<String,String>();
if(!CommonUtils.isEmpty(chunkStr)){
chunk = Integer.parseInt(chunkStr);
chunks = Integer.parseInt(chunksStr);
tempFilePathNameChunk+= "_"+chunk;
fileInfo.put("fileName", fileName);
fileInfo.put("fileSize", fileSize.toString());
fileInfo.put("chunkFileName", chunkFileName);
fileInfo.put("chunks", chunksStr);
if(CommonUtils.isEmpty(this.userUnuploadFileInfoService.get(userNameKey, fileMd5))){
//缓存还没有的时候存一次
this.userUnuploadFileInfoService.put(userNameKey, fileMd5, fileInfo);
}
}
logger.info("begin upload file "+fileName+" "+chunk+"/"+chunks);
File uploadFile = new File(tempFilePathNameChunk);
FileCopyUtils.copy(file.getBytes(), uploadFile);
logger.info("end upload file "+fileName+" "+chunk+"/"+chunks);
if(!CommonUtils.isEmpty(chunksStr)){ //文件是否分片
this.fileChunkInfoRedisCacheService.put(fileMd5, chunkStr, chunkMd5); // file fileMd5 chunkNum chunkMd5
Long size = this.fileChunkInfoRedisCacheService.size(fileMd5);
logger.info("file chunkinfo to redis: "+chunkStr+" "+chunkMd5+" size = "+size);
result.put("result", true);
result.put("islast", false);
result.put("msg", "上传成功"+chunk+"/"+chunks);
if(chunks - size ==0){
Set<File> files = new TreeSet<File>();
for(int i = 0; i < chunks; i++)
{
File chunkFile = new File(tempFilePathName+"_"+i);
if(chunkFile.exists()){
files.add(chunkFile);
}
}
if(chunks==files.size()){ //文件合并
logger.info("begin merge file "+fileName);
File mergeFile = new File(tempFileDirectory+File.separator+fileName);
FileOutputStream writer = new FileOutputStream(mergeFile);
long time = new Date().getTime();
int length = 2097152;
byte buffer[]=new byte[length];
for(File chunkFile : files)
{
FileInputStream reader=new FileInputStream(chunkFile);
boolean flag = true;
while(flag)
{
int ins = reader.read(buffer);
if(ins == -1)
{
reader.close();
writer.flush();
time = new Date().getTime() - time;
flag =false;
}
else {
writer.write(buffer, 0, ins);
}
}
}
writer.close();
logger.info("end merge file "+fileName+" time = "+time);
this.resourceService.saveUploadResource(mergeFile, fileType, isShared, fileMd5, userName,fileName);
logger.info("---UPLOAD "+fileName + " SUCCESS!--- ");
result.put("result", true);
result.put("islast", true);
result.put("msg", "上传成功");
//删除缓存块文件
for(File chunkFile : files){
if(chunkFile.isFile()&&chunkFile.exists()){
chunkFile.delete(); //TODO 现在是放在wildfly下面的temp目录 java代码没有权限删除,以后用文件系统存放的时候可以删除
}
}
if(mergeFile.isFile()&&mergeFile.exists()){
mergeFile.delete();
}
// 合并后删除redis存放的块文件记录
this.fileChunkInfoRedisCacheService.deleteAll(fileMd5);
this.userUnuploadFileInfoService.delete(userNameKey,fileMd5);
}
}
}else{
// 单个文件直接保存
this.resourceService.saveUploadResource(uploadFile, fileType, isShared, fileMd5, userName,fileName);
if(uploadFile.isFile()&&uploadFile.exists()){
uploadFile.delete();
}
logger.info("---UPLOAD file "+fileName + " SUCCESS!--- ");
result.put("result", true);
result.put("islast", true);
result.put("msg", "上传成功");
}
}
catch (Exception e)
{
result.put("result", false);
result.put("msg", "上传失败");
// 上传失败,IO异常!
e.printStackTrace();
logger.error("--- UPLOAD FAIL ---");
logger.error(e.getMessage());
}
return result;
}
}

serviceImpl 这里只是按照需求复制已经存在临时目录的文件和保存到数据库
@Override
@Transactional
public Resource saveUploadResource(File f, Integer fileType,
Integer isShared, String fileMd5, String userName, String fileName) throws Exception
{
String prefix=fileName.substring(fileName.lastIndexOf("."));
String randomName = java.util.UUID.randomUUID().toString()+prefix;
User user = this.userDao.findByUsername(userName);
if(CommonUtils.isEmpty(fileMd5))
{
fileMd5 = MD5Util.getFileMD5String(f); // 上传文件后 计算MD5
}
// 检查是否有重复资源,有则不保存记录
Boolean isExist = checkMd5Code(fileMd5);
if(isExist)
{
logger.info("The uploaded resource is already existed! fileName="+ fileName + " fileMd5=" + fileMd5);
// 保存客户关联关系 新增一条记录
UploadFile uploadFile = this.findUploadFileByMd5Code(fileMd5);
Set<Resource> resources = uploadFile.getResources();
Resource newResource = null;
Set<User> existsUsers = new HashSet<User>();
for(Resource resource : resources)
{
existsUsers.add(resource.getUser());
}
if(!existsUsers.contains(user))
{// 此文件虽然存在,但是当前上传用户需要建立关联关系到原来的这个文件,现在上传的这个文件可以删除。
newResource = new Resource();
newResource.setFileName(fileName);
newResource.setUser(user);
newResource.setDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
newResource.setUploadFile(uploadFile);
newResource = this.save(newResource);
}
return newResource;
}
else
{
String upload = ShareTools.getServerParameter(ServiceConstant.SERVER_UPLOAD_PATH_BASE);// 文件真实目录 D:/upload
String secondFolder = ResourceType.getUploadFolder(fileType);
String saveDirectory = upload + File.separator + secondFolder;
// 目录不存在创建目录
File savePath = new File(saveDirectory);
if(!savePath.exists())
{
savePath.mkdirs();
}
File saveFile = new File(saveDirectory + File.separator + randomName);
FileCopyUtils.copy(f, saveFile);
// 创建资源对象
Resource resource = new Resource();
resource.setDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
resource.setFileName(fileName);
resource.setIsShared(isShared);
// if(isShared == ServiceConstant.RESOURCE_SHARED_YES)
// {
//不管是否共享,都确定文件的共享范围
Area area = user.getUserTypeArea();
resource.setShareScopeArea(area);
logger.info("resource share scope is " + area.getName());
// }
resource.setUser(user);
UploadFile uploadFile = new UploadFile();
uploadFile.setFileType(fileType);
// 如果是音视频计算时长
if(fileType == ResourceType.AUDIO.value()|| fileType == ResourceType.VIDEO.value())
{
long duration = FFmpegUtil.getMediaDuration(f);
uploadFile.setDuration(duration);
if(fileType == ResourceType.VIDEO.value())
{
FFmpegUtil.genVedioThumbnail(saveFile,ServiceConstant.DEFAULT_VEDIO_CUT_TIME);
}
}
else if(fileType == ResourceType.IMAGE.value())
{
// 生成缩略图
String scaleFileName = randomName.substring(0,randomName.lastIndexOf("."))+ ServiceConstant.SCALE_IMG_SUFFIX;
// 文件保存目录
String saveFilepathAndName = saveDirectory + File.separator+ scaleFileName;
// 生成缩略图
Thumbnails.of(f).size(ServiceConstant.SCALE_DESC_WIDTH,ServiceConstant.SCALE_DESC_HEIGHT).toFile(saveFilepathAndName);
}
uploadFile.setMd5Code(fileMd5);
uploadFile.setRandomName(randomName);
uploadFile.setSize(f.length());
resource.setUploadFile(uploadFile);
Resource savedResource = this.save(resource);
logger.info("save upload file "+ savedResource);
return savedResource;
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值