小鸟慢飞

博客园 首页 联系 订阅 管理

package cn.itcast610.oa.utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.struts2.ServletActionContext;

public class UploadUtils {
    public static String saveUploadFile(File upload){
        SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
        String basePath = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");
        String subPath = sdf.format(new Date());
        //如果文件夹不存在,就创建文件夹
        File dir = new File(basePath+subPath);
        if(!dir.exists()){
            dir.mkdirs();
        }
        //String path = basePath+"/"+this.uploadFileName;
        String path = basePath+subPath+UUID.randomUUID().toString();
        File dest = new File(path);
        //把文件移动到dest处
        upload.renameTo(dest);
        return path;
    }
}

posted on 2013-01-11 21:54  小鸟慢飞  阅读(414)  评论(0编辑  收藏  举报