SmartUpload的基本使用、相关控制及IP随机文件命名

2 篇文章 0 订阅

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值