package framework.fileUpload;
|
|
import java.io.File;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.UUID;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpSession;
|
|
import org.apache.ibatis.annotations.Param;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
import framework.base.FrameBaseController;
|
import framework.startup.FrameUtil;
|
|
/**
|
* 通用的文件上传类,与sys.js中的 $.commonFileUpload一起使用<br/>
|
*
|
* 需要验证权限,一般只能登录才可上传
|
*
|
* @author liuyajun, 8384503@qq.com
|
* @date 2016年1月18日
|
* @time 下午7:18:41
|
*/
|
@Controller
|
public class FileUploadController extends FrameBaseController {
|
|
@Autowired
|
MyMultipartResolver resolver;
|
|
@RequestMapping(value = "systemFileUpload", method = RequestMethod.POST)
|
public String upload(HttpServletRequest request,
|
@Param("getProgress") String getProgress,
|
@Param("clear") String clear) throws Exception {
|
|
//清空session,以备记录上传进度
|
if(clear !=null && "y".equals(clear)){
|
HttpSession session = request.getSession();
|
session.removeAttribute(FileUploadProgressListener.SESSION_FILE_UPLOAD_KEY_MAX);
|
session.removeAttribute(FileUploadProgressListener.SESSION_FILE_UPLOAD_KEY);
|
return this.ajax(null);
|
}
|
|
//获取上传进度
|
if(getProgress !=null && "y".equals(getProgress)){
|
HttpSession session = request.getSession();
|
|
String max =(String)session.getAttribute(
|
FileUploadProgressListener.SESSION_FILE_UPLOAD_KEY_MAX);
|
String percent = (String)session.getAttribute(
|
FileUploadProgressListener.SESSION_FILE_UPLOAD_KEY);
|
|
if(max !=null){
|
return this.ajax(max);
|
}
|
return this.ajax(percent);
|
}
|
|
//接收上传的文件
|
resolver.setServletContext(request.getSession().getServletContext());
|
|
String uploadPath = FrameUtil.getUploadFilePath();
|
|
if (resolver.isMultipart(request)) {
|
|
MultipartHttpServletRequest req = (MultipartHttpServletRequest)request;
|
|
Iterator<String> iter = req.getFileNames();
|
|
String split = FileUploadHelper.SPLIT;
|
StringBuffer ss = new StringBuffer();
|
|
while (iter.hasNext()) {
|
|
String inputName = iter.next();
|
|
List<MultipartFile> fileList = req.getFiles(inputName);
|
|
for(MultipartFile file : fileList){
|
if(file==null || file.getOriginalFilename().trim().length()==0){
|
continue;
|
}
|
|
// CommonsMultipartFile cf= (CommonsMultipartFile)file;
|
// DiskFileItem fi = (DiskFileItem)cf.getFileItem();
|
// File f = fi.getStoreLocation();
|
// System.err.println(f.getAbsolutePath());
|
|
String oriName = file.getOriginalFilename().trim();
|
|
String newName = UUID.randomUUID().toString().toLowerCase()
|
.replace("-", "").replace("/", "");
|
String fileString = inputName+split+newName+split+oriName;
|
|
ss.append(split).append(split).append(fileString);
|
|
String newPath = uploadPath + newName;
|
|
file.transferTo(new File(newPath));
|
}
|
}
|
|
if(ss.length()>0){
|
ss.delete(0, 2);
|
}
|
|
/*
|
* 上传文件控件name/保存的文件名(相对于上传目录)/文件原始名//
|
*/
|
this.getRequest().setAttribute("fileInfo", ss.toString());
|
}
|
|
return "forward:/frame-jsp/file-upload-succuess.jsp";
|
}
|
}
|