class FileManageActionController extends BaseAction
{
// windows
private String PATH_LINEs = "\\";
// linux
private String PATH_LINE = "/";
/**
* 文件上传
* @param request {@link HttpServletRequest}
* @param response {@link HttpServletResponse}
* @return json response
*/
@SuppressWarnings("unchecked")
@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
@ResponseBody
public void fileUpload(HttpServletRequest request, HttpServletResponse response,@RequestParam(value = "imgFile", required = false) MultipartFile[] imgFile ) {
try {
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
//文件保存本地目录路径
String savePath = MapCacheManager.getInstance().getMapCache().get("serverPathUp");
String serverPicPath = savePath + "/" + "pic"+ "/";
//文件保存目录URL
String saveUrls =MapCacheManager.getInstance().getMapCache().get("serverPaths");
String saveUrlPath = saveUrls + "/" + "upload"+ "/";
if(!ServletFileUpload.isMultipartContent(request)){
out.print(getError("请选择文件。"));
out.close();
return;
}
//检查目录
File uploadDir = new File(serverPicPath);
if(!uploadDir.isDirectory()){
out.print(getError("上传目录不存在。"));
out.close();
return;
}
//检查目录写权限
if(!uploadDir.canWrite()){
out.print(getError("上传目录没有写权限。"));
out.close();
return;
}
String dirName = request.getParameter("dir");
if (dirName == null) {
dirName = "image";
}
//定义允许上传的文件扩展名
Map extMap = new HashMap();
extMap.put("image", "gif,jpg,jpeg,png,bmp");
extMap.put("flash", "swf,flv");
extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,xml,txt,zip,rar,gz,bz2");
if(!extMap.containsKey(dirName)){
out.print(getError("目录名不正确。"));
out.close();
return;
}
//创建文件夹
serverPicPath += dirName + PATH_LINE;
saveUrlPath+= dirName + PATH_LINE;
File saveDirFile = new File(serverPicPath);
if (!saveDirFile.exists()) {
saveDirFile.mkdirs();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String ymd = sdf.format(new Date());
serverPicPath += ymd + PATH_LINE;
saveUrlPath += ymd + PATH_LINE;
File dirFile = new File(serverPicPath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
//最大文件大小
long maxSize = 1000000;
// 保存文件
for(MultipartFile iFile : imgFile){
String fileName = iFile.getOriginalFilename();
//检查文件大小
if(iFile.getSize() > maxSize){
out.print(getError("上传文件大小超过限制。"));
out.close();
return;
}
//检查扩展名
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
if(!Arrays.asList(extMap.get(dirName).split(",")).contains(fileExt)){
out.print(getError("上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(dirName) + "格式。"));
out.close();
return;
}
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
try{
File uploadedFile = new File(serverPicPath, newFileName);
}catch(Exception e){
out.print(getError("上传文件失败。"));
out.close();
return;
}
System.out.println(saveUrlPath+newFileName);
JSONObject obj = new JSONObject();
obj.put("error", 0);
obj.put("url", saveUrlPath + newFileName);
System.out.println(newFileName+"上传的图片");
out.print(obj.toJSONString());
out.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private Map getError(String errorMsg) {
Map errorMap = new HashMap();
errorMap.put("error", 1);
errorMap.put("message", errorMsg);
return errorMap;
}
}