package self.me.common.utils;

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

import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.util.StringUtils;

public class FileUtil {
	
	private static Logger logger = Logger.getLogger(FileUtil.class);
	
	/**
	 * 保存图片至目录
	 * @param imgFile
	 * @return
	 * @throws Exception
	 */
	public static String saveFile(MultipartFile imgFile) throws Exception{
		String filepath="";
		String fileName = imgFile.getOriginalFilename();
		if(StringUtils.isEmpty(fileName)){
			logger.error("没选择文件.");
			return filepath;
		}
		try {
			String ext=FilenameUtils.getExtension(fileName);   //获取文件后缀
			ext = ext.toLowerCase();							//将文件后缀改为小写
			int random = (int) (Math.random()*100);
			String workTime=new SimpleDateFormat("yyyyMMdd").format(new Date());//当前时间 作为文件夹
			String workTime_fileName=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());//当前时间// 作为文件名
			File file=creatFolder(Constants.FILE_SAVE_PATH,workTime, workTime_fileName+random+"."+ext);
			imgFile.transferTo(file);       
			filepath= Constants.FILE_SHOW_PATH_SUFFIX+"/"+workTime+"/"+workTime_fileName+random+"."+ext;
			return filepath;
		} catch (IOException e) {
			logger.error(fileName+":的文件上传失败!reason="+e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**     
	 * creatFolder(创建目录)    
	 */
	public static File creatFolder(String path ,String brandName,String fileName) {   
		File file = null;   
		
		File firFoler=new File(path);
		if(firFoler.exists()){
			File firstFolder = new File(path);         //一级文件夹   
			if(firstFolder.exists()) {                             //如果一级文件夹存在,则检测二级文件夹   
				File secondFolder = new File(firstFolder,brandName);   
				if(secondFolder.exists()) {                        //如果二级文件夹也存在,则创建文件   
					file = new File(secondFolder,fileName);   
				}else {                                            //如果二级文件夹不存在,则创建二级文件夹   
					secondFolder.mkdir();   
					file = new File(secondFolder,fileName);        //创建完二级文件夹后,再合建文件   
				}   
			}else {                                                //如果一级不存在,则创建一级文件夹   
				firstFolder.mkdir();   
				File secondFolder = new File(firstFolder,brandName);   
				if(secondFolder.exists()) {                        //如果二级文件夹也存在,则创建文件   
					file = new File(secondFolder,fileName);   
				}else {                                            //如果二级文件夹不存在,则创建二级文件夹   
					secondFolder.mkdir();   
					file = new File(secondFolder,fileName);   
				}   
			} 
		}else{
			firFoler.mkdir();
			File firstFolder = new File(path);         //一级文件夹   
			if(firstFolder.exists()) {                             //如果一级文件夹存在,则检测二级文件夹   
				File secondFolder = new File(firstFolder,brandName);   
				if(secondFolder.exists()) {                        //如果二级文件夹也存在,则创建文件   
					file = new File(secondFolder,fileName);   
				}else {                                            //如果二级文件夹不存在,则创建二级文件夹   
					secondFolder.mkdir();   
					file = new File(secondFolder,fileName);        //创建完二级文件夹后,再合建文件   
				}   
			}else {                                                //如果一级不存在,则创建一级文件夹   
				firstFolder.mkdir();   
				File secondFolder = new File(firstFolder,brandName);   
				if(secondFolder.exists()) {                        //如果二级文件夹也存在,则创建文件   
					file = new File(secondFolder,fileName);   
				}else {                                            //如果二级文件夹不存在,则创建二级文件夹   
					secondFolder.mkdir();   
					file = new File(secondFolder,fileName);   
				}   
			}   
		}
		
		
		return file;   
	}

}
最近下载更多
shiopaaa  LV13 2021年1月29日
清咿酱  LV11 2020年5月6日
skipple3  LV39 2019年10月15日
qq_1344127185  LV27 2018年9月7日
yangctz  LV25 2018年5月28日
106177804liyang  LV7 2018年3月12日
boqiu2016  LV17 2017年11月25日
chinafjfzlj  LV31 2017年9月8日
190679152@qq.com  LV15 2017年2月5日
雷诺阿  LV10 2016年9月13日
最近浏览更多
edpwyg  LV14 2024年7月28日
qiangmin1223  LV12 2024年4月24日
wuying8208  LV15 2023年9月4日
tomcat_test  LV2 2022年7月6日
xuweisong2010  LV28 2022年5月20日
Zeorwyc  LV8 2022年4月27日
yangsha868  LV9 2021年8月13日
on_Angels  LV1 2021年7月27日
鑫励诚  LV1 2021年5月10日
Mote-Lwh  LV1 2021年3月10日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友