/**
 * 上传文件类
 */
package com.gootrip.util;

/**
 * @author advance
 *
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class UploadHelper {
	
	public final static String separator = "/";
	public final static String split = "_";
	
	protected final Log log = LogFactory.getLog(getClass());

	class FilenameFilterImpl implements FilenameFilter 
	{
		private String filter = ".";
		
		public FilenameFilterImpl(String aFilter)
		{
			filter = aFilter;
		}
		
    	public boolean accept(File dir, String name) 
    	{
        	return name.startsWith(filter);
    	}
	};
	/**
	 * 获得当前的文件路径(通过当前日期生成)
	 * @param basePath
	 * @return
	 */
	public static String getNowFilePath(String basePath){
		SimpleDateFormat formater =new SimpleDateFormat("yyyy-MM-dd");
		String pathName = formater.format(new Date());
		File dir = new File(basePath + separator + pathName);
		if(!dir.exists())
			dir.mkdir();
		return pathName;
	}
	
	public static String getNewFileName(String oldFileName){
		oldFileName = oldFileName.replaceAll("'", "").replaceAll("\"", "");
		Calendar date = Calendar.getInstance();
		int hour = date.get(Calendar.HOUR_OF_DAY);
		int minute = date.get(Calendar.MINUTE);
		int second = date.get(Calendar.SECOND);
		if(oldFileName.length()>30)
			oldFileName = oldFileName.substring(oldFileName.length()-30);
		return (new Integer(hour*3600 + minute*60 + second).toString()) 
					+ split + oldFileName;
	}
	
	public static String getThumbFileName(String fileName){
		int pos = fileName.lastIndexOf(".");
		if(pos>=0)
			return fileName.substring(0, pos) + "s" + fileName.substring(pos);
		else
			return	fileName + "s";
	}
	
   	/**
   	 * This method checks if the given file exists on disk. If it does it's ignored because
   	 * that means that the file is allready cached on the server. If not we dump
   	 * the text on it.
   	 */
	public void dumpAttributeToFile(String attributeValue, String fileName, String filePath) throws Exception
	{
		File outputFile = new File(filePath + separator + fileName);
		PrintWriter pw = new PrintWriter(new FileWriter(outputFile));
        pw.println(attributeValue);    
        pw.close();
	}
	
 	/**
 	 * 保存文件
   	 * This method checks if the given file exists on disk. If it does it's ignored because
   	 * that means that the file is allready cached on the server. If not we take out the stream from the 
   	 * digitalAsset-object and dumps it.
   	 */
	public void dumpAsset(File file, String fileName, String filePath) throws Exception
	{
		long timer = System.currentTimeMillis();
		
		File outputFile = new File(filePath + separator + fileName);
		if(outputFile.exists())
		{
			log.info("The file allready exists so we don't need to dump it again..");
			return;
		}
		
		FileOutputStream fis = new FileOutputStream(outputFile);
		BufferedOutputStream bos = new BufferedOutputStream(fis);
		
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
		
		int character;
        while ((character = bis.read()) != -1)
        {
			bos.write(character);
        }
		bos.flush();
		
        bis.close();
		fis.close();
		bos.close();
		log.info("Time for dumping file " + fileName + ":" + (System.currentTimeMillis() - timer));
	}

	/**
	 * 保存缩略图
	 * This method checks if the given file exists on disk. If it does it's ignored because
	 * that means that the file is allready cached on the server. If not we take out the stream from the 
	 * digitalAsset-object and dumps a thumbnail to it.
	 */
   	
	public void dumpAssetThumbnail(File file, String fileName, String thumbnailFile, String filePath, int width, int height, int quality) throws Exception
	{
		long timer = System.currentTimeMillis();
		log.info("fileName:" + fileName);
		log.info("thumbnailFile:" + thumbnailFile);
		
		File outputFile = new File(filePath + separator + thumbnailFile);
		if(outputFile.exists())
		{
			log.info("The file allready exists so we don't need to dump it again..");
			return;
		}
		
		ThumbnailGenerator tg = new ThumbnailGenerator();
		tg.transform(filePath + separator + fileName, filePath + separator + thumbnailFile, width, height, quality);
		
		log.info("Time for dumping file " + fileName + ":" + (System.currentTimeMillis() - timer));
	}
	
	/**
	 * This method removes all images in the digitalAsset directory which belongs to a certain digital asset.
	 */
	public void deleteDigitalAssets(String filePath, String filePrefix) throws Exception
	{ 
		try
		{
			File assetDirectory = new File(filePath);
			File[] files = assetDirectory.listFiles(new FilenameFilterImpl(filePrefix)); 	
			for(int i=0; i<files.length; i++)
			{
				File file = files[i];
				log.info("Deleting file " + file.getPath());
				file.delete();
			}
		}
		catch(Exception e)
		{
			log.error("Could not delete the assets for the digitalAsset " + filePrefix + ":" + e.getMessage(), e);
		}
	}

}
最近下载更多
飘逸的云  LV1 2023年7月6日
linmou  LV8 2023年3月19日
tangjj7260  LV18 2021年11月12日
2469095052  LV8 2021年3月3日
txl_816278800190  LV1 2020年10月30日
moomin709  LV24 2020年7月6日
dhc0611  LV1 2020年4月12日
tim_chen  LV5 2020年3月28日
BCatZSY  LV7 2020年2月10日
洋洋阳阳  LV7 2020年2月3日
最近浏览更多
飘逸的云  LV1 2023年7月6日
linmou  LV8 2023年3月19日
二进制2  LV3 2023年1月6日
al-aaalll  LV3 2022年12月13日
刘君88888 2022年8月6日
暂无贡献等级
最代码-宋家辉  LV61 2022年7月16日
crosa_Don  LV18 2022年4月1日
暂无贡献等级
Java-smalllong 2022年1月24日
暂无贡献等级
duqiangedu  LV3 2021年12月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友