您的位置:首页 > 其它

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 命名类

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: