首页 > 代码库 > 文件操作

文件操作

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
/**
 * 文件操作工具类
 * @author deruiyu
 *
 */
public class FileUtil {
	/**
	 * 构造函数
	 * @param savePathName 上传保存的文件夹名字
	 */
	public FileUtil(String savePathName){
		this.savePathName = savePathName;
		String path = getUpPath();
		upFiles = new File(path);  
	    if (!upFiles.exists()) {  
	    	upFiles.mkdirs();  
	    } 
	}
	
	private boolean allowOver = false;
	
	private boolean randomName = true;
	
	public boolean isRandomName() {
		return randomName;
	}
	public void setRandomName(boolean randomName) {
		this.randomName = randomName;
	}
	
	public boolean isAllowOver() {
		return allowOver;
	}
	public void setAllowOver(boolean allowOver) {
		this.allowOver = allowOver;
	}

	private File upFiles;
	
	public FileUtil(){}
	
	private String savePathName;
	/**
	 * 获取上传地址
	 * @return
	 */
	private String getUpPath(){
		return ServletActionContext.getServletContext().getRealPath("/"+savePathName);
	}
	/**
	 * 获取文件路径
	 * @param filePath
	 * @return
	 */
	public static String getFilePath(String filePath){
		return ServletActionContext.getServletContext().getRealPath("/"+filePath);
	}
	/**
	 * 文件删除
	 * @param filePath 文件相对路径
	 * @return
	 */
	public static boolean delPathFile(String filePath){
		filePath = getFilePath(filePath);
		File file = new File(filePath);  
		if(file.exists()){
			return file.delete();
		}else{
			return false;
		}
	}
	/**
	 * 文件删除(以当前工具类参数路径为准,如果参数路径为空,则将文件名当做相对路径)
	 * @param fileName 文件名
	 * @return
	 */
	public boolean delFile(String fileName){
		if(savePathName!=null){
			String filePath = getUpPath()+"/"+fileName;
			File file = new File(filePath); 
			if(file.exists()){
				return file.delete();
			}else{
				return false;
			}
		}else{
			return delPathFile(fileName);
		}
	}
	
	/**
	 * 上传文件
	 * @return
	 */
	public String[] uploadFile(List<File> upload,List<String> uploadFileName){
		String[] filePath = new String[upload.size()];
		try { 
			String path = getUpPath();
			File file = new File(path);  
		    if (!file.exists()) {  
		    	file.mkdirs();  
		    }  
		    for (int i = 0; i < upload.size(); i++) {  
	    		String upname = uploadFileName.get(i);
	    		if(isRandomName()){
	    			upname = getUpName(upname);
	    		}
	    		File upFile = new File(file,upname);
	    		if(isAllowOver()&&upFile.exists()&&!upFile.delete()){
	    			continue;
	    		}
	    		FileUtils.copyFile(upload.get(i), upFile);  
	    		filePath[i] = savePathName+"/"+upname;
		    }  
	    }catch (IOException e) {  
	    	e.printStackTrace();  
	    }  
		return filePath;
	}
	
	/**
	 * 上传文件
	 * @return
	 */
	public String uploadFileOne(File uploadFile,String uploadFileName){
		String filePath = null;
		try { 
			String path = getUpPath();
			File file = new File(path);  
		    if (!file.exists()) {  
		    	file.mkdirs();  
		    }  
	    		if(isRandomName()){
	    			uploadFileName = getUpName(uploadFileName);
	    		}
	    		File upFile = new File(file,uploadFileName);
	    		if(isAllowOver()&&upFile.exists()&&!upFile.delete()){
	    			return null;
	    		}
	    		FileUtils.copyFile(uploadFile, upFile);  
	    		filePath = savePathName+"/"+uploadFileName;
	    }catch (IOException e) {  
	    	e.printStackTrace();  
	    }  
		return filePath;
	}
	
	/**
	 * 上传文件
	 * @throws IOException 
	 */
	public void uploadFile(File upload,String saveFileName) throws IOException{
		FileUtils.copyFile(upload, new File(upFiles,saveFileName));  
		
	}
	
    /**
     * 随机生成上传名称
     * @param name
     * @return
     */
	public String getUpName(String name){
		Calendar time=Calendar.getInstance();
    	Random r = new Random();
    	String upName=(r.nextInt(900)+100)+"_";
    	upName+= time.get(Calendar.YEAR)+""+(time.get(Calendar.MONTH)+1)+""+time.get(Calendar.DAY_OF_MONTH)
    			+""+time.get(Calendar.HOUR_OF_DAY)+""+time.get(Calendar.MINUTE)+""+time.get(Calendar.SECOND)+""+time.get(Calendar.MILLISECOND);
    	upName+="_"+(r.nextInt(90)+10);
    	upName+=name.substring(name.lastIndexOf('.'));
    	return upName;
    }
	
    /**
     * 是否是允许上传类型
     * @param name
     * @return
     */
    public static boolean canupType(String name){
    	if(name.indexOf(".html")!=-1) return false;
    	if(name.indexOf(".js")!=-1) return false;
    	if(name.indexOf(".css")!=-1) return false;
    	if(name.indexOf(".jsp")!=-1) return false;
    	if(name.indexOf(".bat")!=-1) return false;
    	if(name.indexOf(".java")!=-1) return false;
    	if(name.indexOf(".class")!=-1) return false;
    	return true;
    }

}

文件操作