/**
* 上传文件类
*/
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);
}
} }

Java-UploadHelper工具类的更多相关文章

  1. Java Properties工具类详解

    1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠 ...

  2. Java json工具类,jackson工具类,ObjectMapper工具类

    Java json工具类,jackson工具类,ObjectMapper工具类 >>>>>>>>>>>>>>> ...

  3. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

  4. Java并发工具类 - CountDownLatch

    Java并发工具类 - CountDownLatch 1.简介 CountDownLatch是Java1.5之后引入的Java并发工具类,放在java.util.concurrent包下面 http: ...

  5. MinerUtil.java 爬虫工具类

    MinerUtil.java 爬虫工具类 package com.iteye.injavawetrust.miner; import java.io.File; import java.io.File ...

  6. MinerDB.java 数据库工具类

    MinerDB.java 数据库工具类 package com.iteye.injavawetrust.miner; import java.sql.Connection; import java.s ...

  7. 小记Java时间工具类

    小记Java时间工具类 废话不多说,这里主要记录以下几个工具 两个时间只差(Data) 获取时间的格式 格式化时间 返回String 两个时间只差(String) 获取两个时间之间的日期.月份.年份 ...

  8. Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie

    Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie >>>>>>>>>>>>& ...

  9. UrlUtils工具类,Java URL工具类,Java URL链接工具类

    UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...

  10. java日期工具类DateUtil-续一

    上篇文章中,我为大家分享了下DateUtil第一版源码,但就如同文章中所说,我发现了还存在不完善的地方,所以我又做了优化和扩展. 更新日志: 1.修正当字符串日期风格为MM-dd或yyyy-MM时,若 ...

随机推荐

  1. 学习笔记:CentOS7学习之十四:linux文件系统

    目录 1. 机械硬盘结构 1.1 机械硬盘结构 1.2 簇和block 2.文件系统结构 2.1 文件名 2.2 inode的内容 2.3 inode的大小 2.4 目录文件 2.5 block块大小 ...

  2. Postfix to Infix

    Infix expression: The expression of the form a op b. When an operator is in-between every pair of op ...

  3. Similar String Groups

    Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that it ...

  4. python+pycharm+PyQt5 图形化界面安装教程

    python图形化界面安装教程 配置环境变量 主目录 pip所在目录,及script目录 更新pip(可选) python -m pip install --upgrade pip ps:更新出错一般 ...

  5. 【Java学习】类、对象、实例—类是对象的抽象,对象是类的实例

    类.对象.实例的关系是什么,如果不能很好的理解什么是类什么是对象就无法讲清楚, 类:某种事物与另一种事物具有相似性,比如哈士奇和泰迪,我们发现他们有一些相似的特性和行为,在生物学上,他们都属于“狗”, ...

  6. HDU - 1045 Fire Net (二分图最大匹配-匈牙利算法)

    (点击此处查看原题) 匈牙利算法简介 个人认为这个算法是一种贪心+暴力的算法,对于二分图的两部X和Y,记x为X部一点,y为Y部一点,我们枚举X的每个点x,如果Y部存在匹配的点y并且y没有被其他的x匹配 ...

  7. llinux 进阶篇

    df 查看磁盘空间 df -h (加了之后就有单位了) free 指令,查看内存使用情况 free -m(表示一mb单位进行查看) head 查看一个文件的前n行,如果没有n 就默认为前10行 hea ...

  8. Feign的雪崩处理

    在声明式远程服务调用Feign中,实现服务灾难性雪崩效应处理也是通过Hystrix实现的.而feign启动器spring-cloud-starter-feign中是包含Hystrix相关依赖的.如果只 ...

  9. WPF 异步加载窗体

    加载某个界面时,需要获取数据,而数据返回的时间比较长,这个时候可以异步加载界面. 1.在该窗体的加载事件(Load)中编写以下代码: new Thread(p=>{DataBinding();} ...

  10. SQL SERVER 相关

    while循环 declare @sum int; declare @i int; ; ; ) begin ) begin set @sum=@sum+@i; end ; end select @su ...