httpRequest.setCharacterEncoding("gbk");
String preName = genName.doMake();//设置文件前缀名
String extName = null;
String allName = null;
String fileName = null;
try {
//初始化
sUpload.initialize(servletConfig, request, response);
//设置文件最大上传为10M
sUpload.setTotalMaxFileSize(10*1024*1024);
sUpload.setAllowedFilesList("jpg,bmp,gif,png,JPG,BMP,GIF,PNG");
//不允许哪几种格式上传,不允许及无扩展名的文件类型
//sUpload.setDeniedFilesList("exe,bat,jsp,asp,php,aspx,cer,cdx,jspx,,");
sUpload.upload();
//获取所有的上传文件
File tmpfiles=sUpload.getFiles().getFile(0);
//判断是不是选择了上传文件
if(!tmpfiles.isMissing()){ //如果选择了文件
//获取文件后缀
extName = sUpload.getFiles().getFile(0).getFileExt();
//组合文件名
allName = preName+"."+extName;
//组合文件路径+文件名
fileName = this.getServletContext().getRealPath("/")+"images/case/"+allName;
System.out.println("fileName:_"+fileName);
sUpload.getFiles().getFile(0).saveAs(fileName);
mv.addObject("casesMsg", "提示:文件上传成功!");
}else{
mv.addObject("casesMsg", "提示:文件上传失败!");
}
} catch (Exception e) {
mv.addObject("casesMsg", "提示:文件上传失败!");
}
smartupload.jar下载地址:http://files.cnblogs.com/files/koal/smartupload.zip