package com.tfsay.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.web.multipart.MultipartFile;
import com.tfsay.exception.BizException;
/**
*
* 创建人:xiaoya
* @version 1.0
*
*/
public class FileUpload {
/**
* getFile(这里用一句话描述这个方法的作用)
* @param imgFile:要上传的文件
* @param path:当前项目路径
* @param pathName:上传文件的一级目录
* @param fileTypes:上传文件支持的格式
* @param maxSize:上传文件最大限制
* @return String DOM对象
* @throws BizException
* @Exception 异常对象
* @since CodingExample Ver(编码范例查看) 1.1
*/
public static String saveFile(MultipartFile imgFile,String path,String pathName,List<String> fileTypes , long maxSize) throws BizException {
String filePath=null;
String fileName = imgFile.getOriginalFilename();
if (imgFile.getSize()>maxSize) {
throw new BizException("文件不能大于"+maxSize/1024+"K");
}
//获取上传文件类型的扩展名,先得到.的位置,再截取从.的下一个位置到文件的最后,最后得到扩展名
String ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
//对扩展名进行小写转换
ext = ext.toLowerCase();
File file = null;
if(fileTypes.contains(ext)) {
int random = (int) (Math.random()*100);
String workTime=new SimpleDateFormat("yyyyMMdd").format(new Date());//当前时间
String workTime1=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());//当前时间//如果扩展名属于允许上传的类型,则创建文件
file = creatFolder(path+"upload/",pathName , workTime , workTime1 + random +"."+ext);
try {
imgFile.transferTo(file); //保存上传的文件
filePath="/upload/"+pathName+ "/" +workTime+ "/" + workTime1 + random +"."+ext;
} catch (Exception e) {
throw new BizException("上传文件出错");
}
} else {
throw new BizException("上传文件格式不正确");
}
return filePath;
}
public static void updateFile(MultipartFile imgFile,String path,List<String> fileTypes , long maxSize) throws BizException {
String fileName = imgFile.getOriginalFilename();
if (imgFile.getSize()>maxSize) {
throw new BizException("文件不能大于"+maxSize/1024+"K");
}
//获取上传文件类型的扩展名,先得到.的位置,再截取从.的下一个位置到文件的最后,最后得到扩展名
String ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
//对扩展名进行小写转换
ext = ext.toLowerCase();
File file = null;
if(fileTypes.contains(ext)) {
try {
path = path.replaceAll("\\\\", "/");
file = new File(path);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
imgFile.transferTo(file); //保存上传的文件
} catch (Exception e) {
throw new BizException("上传文件出错");
}
} else {
throw new BizException("上传文件格式不正确");
}
}
/**
* creatFolder(创建目录)
* @param path:当前项目目录
* @param typeName:一级目录
* @param brandName:二级目录
* @param fileName:文件名称
* @param @return 设定文件
* @return String DOM对象
* @Exception 异常对象
* @since CodingExample Ver(编码范例查看) 1.1
*/
public static File creatFolder(String path , String typeName,String brandName,String fileName) {
File file = null;
typeName = typeName.replaceAll("/", ""); //去掉"/"
typeName = typeName.replaceAll(" ", ""); //替换半角空格
typeName = typeName.replaceAll(" ", ""); //替换全角空格
brandName = brandName.replaceAll("/", ""); //去掉"/"
brandName = brandName.replaceAll(" ", ""); //替换半角空格
brandName = brandName.replaceAll(" ", ""); //替换全角空格
File firstFolder = new File(path + typeName); //一级文件夹
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;
}
/**
* deleteFile(删除文件)
* @param name
* @param @return 设定文件
* @return String DOM对象
* @Exception 异常对象
* @since CodingExample Ver(编码范例查看) 1.1
*/
public static void deleteFile(String srcFileName) throws BizException {
boolean success = true;
File file = new File(srcFileName);
if (file.exists()) {
file.delete();
}
if (!success) {
throw new BizException("删除文件"+srcFileName+"出错");
} else {
return;
}
}
/**
* isFileExist(判断某路径下是否有该文件)
* @param name
* @param @return 设定文件
* @return String DOM对象
* @Exception 异常对象
* @since CodingExample Ver(编码范例查看) 1.1
*/
public static boolean isFileExist(String filePath, String file) {
if (filePath == null || file == null) {
return false;
}
String absPath = null;
if (filePath.endsWith("/") || filePath.endsWith("\\")) {
absPath = filePath + file;
}
else {
absPath = filePath + File.separator + file;
}
return (new File(absPath)).exists();
}
/**
* isFileExist(判断文件是否存在)
* @param name
* @param @return 设定文件
* @return String DOM对象
* @Exception 异常对象
* @since CodingExample Ver(编码范例查看) 1.1
*/
public static boolean isFileExist(String file){
return (new File(file)).exists();
}
public static void main(String[] args) throws IOException {
String path = "D:\\Program Files\\apache-tomcat-6.0.20\\webapps\\ROOT";
String path2 = "/upload/advertising/20120812/20120812003231.jpg";
path = path.replaceAll("\\\\", "/");
System.out.println(path+path2);
File file = new File(path+path2);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
System.out.println(file.getParentFile());
file.createNewFile();
System.out.println(FileUpload.isFileExist(path));
}
//复制活动缩略图图片到相册
public static String copyFile(String path,String src,String workTime1){
File file = null;
FileInputStream in=null;
FileOutputStream out=null;
String workTime=new SimpleDateFormat("yyyyMMdd").format(new Date());//当前时间
//=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());//当前时间//如果扩展名属于允许上传的类型,则创建文件
file=FileUpload.creatFolder(path+"upload/", "album", workTime, workTime1+src.substring(src.lastIndexOf("."),src.length()));
try {
in=new FileInputStream(new File(path+src));
out=new FileOutputStream(file);
int i=0;
while((i=in.read())!=-1){
out.write(i);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file.getAbsolutePath().substring(file.getAbsolutePath().indexOf("upload")-1,file.getAbsolutePath().length()).replaceAll("\\\\","/");
}
}