SmartUpload的基本使用、相关控制及IP随机文件命名
2015-08-12 19:37
344 查看
1 首先下载SmartUpload的jar包。
2 基本使用方法:
SmartUpload smartUpload = new SmartUpload();
smartUpload.initialize(pageContext);//初始化上传操作
smartUpload.setCharset("UTF-8");//需要设置字符集某则报错
3 对上传文件进行控制及修改文件名称
1 命名类
2 上传文件控制
http://download.csdn.net/detail/chenzhao2013/8997385
2 基本使用方法:
SmartUpload smartUpload = new SmartUpload();
smartUpload.initialize(pageContext);//初始化上传操作
smartUpload.setCharset("UTF-8");//需要设置字符集某则报错
3 对上传文件进行控制及修改文件名称
1 命名类
package com.cz.util; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import org.eclipse.jdt.internal.compiler.ast.ThisReference; /** * 给文件重命名=规则为:ip+时间戳+三维随机数 * @author * */ public class IPTimeStamp { private SimpleDateFormat simpleDateFormat; private String ip; public IPTimeStamp(String ip){ this.ip = ip; } /** * * @return 返回文件名 */ public String getIPTimeStamp(){ StringBuffer sBuffer = new StringBuffer(); if(ip!=null){ String[] strings = ip.split("\\."); for(int i=0;i<strings.length;i++){ sBuffer.append(this.addZero(strings[i], 3)); } } sBuffer.append(this.getTimeStamp()); Random random = new Random(); for(int i=0;i<3;i++){ sBuffer.append(random.nextInt(10)); } return sBuffer.toString(); } /** * ip地址不足三位数的在其前面用0补齐 * @param str 表示ip某一段的字符串 * @param len 补齐后的长度 * @return 返回补齐后的字符串 */ public String addZero(String str,int len){ StringBuffer sb = new StringBuffer(); sb.append(str); while(sb.length()<len){ sb.insert(0, "0"); } return sb.toString(); } public String getTimeStamp(){ Date date = new Date(); this.simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS"); return this.simpleDateFormat.format(date); } }
2 上传文件控制
package com.cz.util; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.PageContext; import com.jspsmart.upload.File; import com.jspsmart.upload.SmartUpload; public class UploadFileManager { /** * 上传控制 * @param su smartUpload的对象 * @param pageContext * @param request * @return * @throws Exception */ public static String upload(SmartUpload su,PageContext pageContext,HttpServletRequest request) throws Exception { File suFile = null; int fileSize; String fileExt = ""; int fileCount = 0; int maxFileSize = (int) (10*Math.pow(2, 10));//单个文件最大单位K最大上传大小为50M String AllowedExtensions=",jpg,jpeg,gif,png,mp4,zip,";//允许上传的文件类型,注意添加文件类型需要在最后加上逗号 try { for (int i=0; i<su.getFiles().getCount();i++) { suFile = su.getFiles().getFile(i); if (suFile.isMissing()) continue; fileSize = suFile.getSize()/1024;//字节转换成KB if(fileSize==0) fileSize=1; if (suFile.getFileExt() == null || "".equals(suFile.getFileExt())) { fileExt = ",,"; } else { fileExt = "," + suFile.getFileExt().toLowerCase() + ","; } if (!"".equals(AllowedExtensions) && AllowedExtensions.indexOf(fileExt) == -1) { throw new Exception("您上传的文件[" + suFile.getFileName() + "]的类型为系统禁止上传的文件类型,不能上传!"); } if(maxFileSize<fileSize) throw new Exception("单个上传文件的容量不能超过["+maxFileSize+"KB]即10M"+"超过文件为:"+suFile.getFileName()); fileCount++; } if (fileCount==0) throw new Exception("请选择上传的文件"); String ip = request.getRemoteAddr(); IPTimeStamp ipTimeStamp = new IPTimeStamp(ip); String fullFileName = null;//保存到服务器上的文件名(带路径) for (int i=0; i<su.getFiles().getCount();i++) { suFile = su.getFiles().getFile(i); fileExt = su.getFiles().getFile(i).getFileExt(); if (suFile.isMissing()) continue; fullFileName = ipTimeStamp.getIPTimeStamp()+"."+fileExt;//填写 文件的路径+文件名 String path1 = pageContext.getServletContext().getRealPath("/")+"upload"+java.io.File.separator+fullFileName;//填写 文件名 System.out.println(path1); suFile.saveAs(path1.toString(),SmartUpload.SAVE_PHYSICAL);// 项目跟路径下upload文件夹下 String path2 = "F:\\Receive\\"+fullFileName;//本地保存路径 System.out.println(path2); suFile.saveAs(path2.toString(), SmartUpload.SAVE_PHYSICAL); } return "successed"; } finally { // } } }4 代码中注释清楚介绍了内容,更多内容请看
http://download.csdn.net/detail/chenzhao2013/8997385
相关文章推荐
- 费波纳契数d动态规划实现
- python函数——根据经纬度计算距离公式的错误及修正
- myeclipse,eclipse 在 debug 模式下 启动慢
- File res/drawable-xxhdpi-v4/title_bg.9.png from drawable resource ID #0x7f02
- iOS开发-Day21-OC ARC&类目
- sql的各种日期输出
- MySQL 打开federated存储引擎
- Leetcode62 Unique Paths
- opensips-1.8.3在RHEL5.5的搭建过程
- BFS
- 为jlabel设置背景颜色
- spring开发文档收集
- asp.net div不被撑开,出现滚动条
- UITabBar
- wcf使用X509证书加密传输
- 周赛 Hdoj 5146
- bzoj 1503: [NOI2004]郁闷的出纳员 (伸展树)
- 第二次练习
- Android 进阶 教你打造 Android 中的 IOC 框架 【ViewInject】 (上)
- 曝刘亦菲奢华豪宅 面积堪比4个足球场,有钱就是这么任性!