java上传工具_Java一个文件上传工具类

/**

* 文件上传

*

* @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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值