/**
* 上传文件类
*/
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. [bzoj2288]【POJ Challenge】生日礼物_贪心_堆

    [POJ Challenge]生日礼物 题目大意:给定一个长度为$n$的序列,允许选择不超过$m$个连续的部分,求元素之和的最大值. 数据范围:$1\le n, m\le 10^5$. 题解: 显然的 ...

  2. 小记--------spark的worker原理分析及源码分析

     

  3. C++ MinGW 配合 Sublime Text 搭建

    本文主旨 使用MinGW 和 文本编辑器 Sublime Text,来搭建c++编译的平台. Sublime Text 安装 和 解除限制 http://rainss.cn/essay/1124.ht ...

  4. SDL2 程序 编译 错误 及 解决方案

    main函数应写为int main( int argc, char* args[] )而不是int main()形式 链接库时应注意顺序 mingw32;SDL2main;SDL2;          ...

  5. Scala学习五——类

    一.本章要点 类中的字段自动带有getter方法和setter方法 你可以用定制的getter/setter方法替换掉字段的定义,而不必修改使用类的客户端——这就是所谓的”统一访问原则“ 用@Bean ...

  6. mysql java jdbc 如何 update select

    2019年8月6日17:28:07 sql 不知道怎么写,也没去查,因为需求可能中途需要修改值,有点麻烦 直接用jdbc实现. 查询出来的值,直接根据update条件更新,写在一个方法里 public ...

  7. centos 配置mysql主从复制

    mysql+centos7+主从复制   MYSQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公 ...

  8. sql server 2012使用新特性offset和fetch next完成分页操作

    1 select * from HumanResources.Department order by DepartmentID offset rows fetch next rows only; of ...

  9. vuejs 深度监听

    data: { obj: { a: 123 } }, 监听obj中a属性 watch: { 'obj.a': { handler(newName, oldName) { console.log('ob ...

  10. 价值19.9元 <问药师 - 儿童维生素D的补充> 总结

    1岁以上的婴幼儿(一直到18岁), 每天应该补充维生素D 400-800UI, 这其中应包含当天的晒太阳时间. 1岁以下400UI户外晒太阳时间, 夏季30分钟, 冬季2小时, 再根据实际情况加减每天 ...