/**
* 文件上传
*
* @author cary
* @since 2012-12-19 下午2:22:12*/
public classFileUploader {static final String UPLOAD_PATH_KEY = "upload.dir";private static Logger logger = Logger.getLogger(FileUploader.class);/**
* 定义允许上传的文件扩展名*/
private Map extMap = new HashMap();/**
* 文件上传目录*/
privateString savePath;/**
* 文件完整目录*/
privateString saveUrl;/**
* 空构造器*/
publicFileUploader() {//允许上传的文件类型
extMap.put("extName", "gif,jpg,jpeg,png,bmp,xls,xlsx");
}/**
* @param request 请求对象
* @param upload 被上传的文件
* @param uploadFileName 被上传的文件名
* @param dirName 文件类型对应的路径
* @param maxSize 文件大小阈值
* @return
* @throws IOException*/
publicString[] uploadFile(HttpServletRequest request, File upload, String uploadFileName, String dirName,
Long maxSize) throws IOException {/**
* 保存上传文件返回信息*/String[] infos= new String[2];/**
* 获取文件上传目录*/String extName= uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1, uploadFileName.length());
infos[0] = this.validateFields(request, extName, dirName, maxSize);
String prefixName= uploadFileName.substring(0, uploadFileName.lastIndexOf("."));
SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddhh24mmss");
String ymd= sdf.format(newDate());
String realName= prefixName + ymd + "." +extName;/**
* 获取文件全路径*/saveUrl= savePath +realName;if (infos[0].equals("true")) {
saveFile(upload, savePath, realName);
infos[1] =saveUrl;
}returninfos;
}/**
* 上传验证,并初始化文件目录
*
* @param request 请求对象
* @param extName 扩展名
* @param dirName 子目录名
* @param maxSize 文件大小阈值
* @throws IOException*/
privateString validateFields(HttpServletRequest request, String extName, String dirName, Long maxSize)
throws IOException {
String errorInfo= "true";//获取内容类型
String contentType =request.getContentType();
String extType= extMap.get("extName");int contentLength =request.getContentLength();
String path=ConfigUtil.getString(FileUploader.UPLOAD_PATH_KEY);
savePath= path + "/" + dirName + "/";
File uploadDir= newFile(savePath);if (contentType == null || !contentType.startsWith("multipart")) {
logger.error("请求不包含multipart/form-data流");return "请求不包含multipart/form-data流";
}if (extType.indexOf(extName) == -1) {return "不允许上传的文件类型[" + "." + extName + "]";
}if (maxSize
logger.error("上传文件大小超出文件最大大小");return "上传文件大小超出文件最大大小[" + maxSize + "]";
}if (!ServletFileUpload.isMultipartContent(request)) {return errorInfo = "请选择文件";
}if (!uploadDir.exists()) {//检查目录
logger.warn("上传目录[" + savePath + "]不存在");
uploadDir.mkdir();
}if (!uploadDir.canWrite()) {return "上传目录[" + savePath + "]没有写权限";
}returnerrorInfo;
}/**
* 保存文件
*
* @param upload 待上传文件
* @param savePath 文件存储目录
* @param realName 原始文件名
* @throws IOException*/
public voidsaveFile(File upload, String savePath, String realName) throws IOException {
File target= newFile(savePath, realName);
FileUtils.copyFile(upload, target);
}public static voidmain(String[] args) {
File dirFile= new File("/usr/local/upload/excel");if (!dirFile.exists()) {
dirFile.mkdirs();
}
}
2.action调用代码FileUploader fileUploader = new FileUploader();