/**
* 上传文件类
*/
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. ARC099E. Independence

    考虑一个子问题.给定无向图 $G$,如何判断能否将 $G$ 的点集分成两部分 $S$.$T$ 使得 $S$ 和 $T$ 导出的子图都是完全图? 这个问题把我难住了.解法是考虑 $G$ 的补图 $G'$ ...

  2. redis哨兵sentinel.conf文件

    关闭保护模式 //17行 protected-mode no 端口号 //21 port 26379 后台启动 //26 daemonize yes //84行 主机的ip加端口号 2 为票数 sen ...

  3. Remainder Problem(分块) Educational Codeforces Round 71 (Rated for Div. 2)

    引用:https://blog.csdn.net/qq_41879343/article/details/100565031 下面代码写错了,注意要上面这种.查:2  800  0,下面代码就错了. ...

  4. Ruby Rails学习中:Sass 和 Asset Pipeline,布局中的链接(Rails路由,具名路由),用户注册: 第一步

    接上篇: 一.Sass 和 Asset Pipeline Rails 中最有用的功能之一是 Asset Pipeline, 它极大地简化了静态资源文件(CSS.JavaScript 和图像)的生成和管 ...

  5. T100 事务的开始与结束

    例如: IF cl_ask_confirm('apm-00801') THEN CALL s_transaction_begin() IF NOT axmt500_change_xmdc015('u' ...

  6. python基础知识0-2

    # !/usr/bin/env python # 提示输入用户名和密码 # 验证用户名和密码#     如果错误,则输出用户名或密码错误#     如果成功,则输出 欢迎,XXX! import ge ...

  7. mybatis调用java类

    在mybatis的映射xml文件调用java类的方法: 使用的是OGNL表达式,表达式格式为:${@prefix@methodName(传递参数名称)} 1.如下代码所示:方法必须为静态方法:以下我只 ...

  8. 进阶Java编程(13)反射与Annotation

    1,反射取得Annotation信息 从JDK1.5之后Java提供了Annotation技术支持,这种技术为项目的编写带来了新的模型,而后经过了十年的发展,Annotation的技术得到了非常广泛的 ...

  9. tracert命令详解_tracert结果详解_tracert命令使用详解

    17:06:40 正在等待客服售后工程师令狐冲接入,您可以先简单描述所要咨询的问题,如果长时间没有响应,您也可以 重新选择客服 . 17:06:42 您好,客服售后工程师令狐冲为您服务.售后工程师令狐 ...

  10. PMP - 控制图(质量控制工具)

    这里用一个具体例子来解释什么是控制图. 假设现在工厂需要生成直径为1000mm (允许偏差为振幅4个标准差,即:±4mm)的螺丝钉.我们要如何保证生成过程的质量呢? 这时候我们应该对生成出来的产品进行 ...