1. /**
  2. * 上传文件类
  3. */
  4. import java.io.BufferedInputStream;
  5. import java.io.BufferedOutputStream;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.FileWriter;
  10. import java.io.FilenameFilter;
  11. import java.io.PrintWriter;
  12. import java.text.SimpleDateFormat;
  13. import java.util.Calendar;
  14. import java.util.Date;
  15. import org.apache.commons.logging.Log;
  16. import org.apache.commons.logging.LogFactory;
  17. public class UploadHelper {
  18. public final static String separator = "/";
  19. public final static String split = "_";
  20. protected final Log log = LogFactory.getLog(getClass());
  21. class FilenameFilterImpl implements FilenameFilter
  22. {
  23. private String filter = ".";
  24. public FilenameFilterImpl(String aFilter)
  25. {
  26. filter = aFilter;
  27. }
  28. public boolean accept(File dir, String name)
  29. {
  30. return name.startsWith(filter);
  31. }
  32. };
  33. /**
  34. * 获得当前的文件路径(通过当前日期生成)
  35. * @param basePath
  36. * @return
  37. */
  38. public static String getNowFilePath(String basePath){
  39. SimpleDateFormat formater =new SimpleDateFormat("yyyy-MM-dd");
  40. String pathName = formater.format(new Date());
  41. File dir = new File(basePath + separator + pathName);
  42. if(!dir.exists())
  43. dir.mkdir();
  44. return pathName;
  45. }
  46. public static String getNewFileName(String oldFileName){
  47. oldFileName = oldFileName.replaceAll("'", "").replaceAll("\"", "");
  48. Calendar date = Calendar.getInstance();
  49. int hour = date.get(Calendar.HOUR_OF_DAY);
  50. int minute = date.get(Calendar.MINUTE);
  51. int second = date.get(Calendar.SECOND);
  52. if(oldFileName.length()>30)
  53. oldFileName = oldFileName.substring(oldFileName.length()-30);
  54. return (new Integer(hour*3600 + minute*60 + second).toString())
  55. + split + oldFileName;
  56. }
  57. public static String getThumbFileName(String fileName){
  58. int pos = fileName.lastIndexOf(".");
  59. if(pos>=0)
  60. return fileName.substring(0, pos) + "s" + fileName.substring(pos);
  61. else
  62. return fileName + "s";
  63. }
  64. /**
  65. * This method checks if the given file exists on disk. If it does it's ignored because
  66. * that means that the file is allready cached on the server. If not we dump
  67. * the text on it.
  68. */
  69. public void dumpAttributeToFile(String attributeValue, String fileName, String filePath) throws Exception
  70. {
  71. File outputFile = new File(filePath + separator + fileName);
  72. PrintWriter pw = new PrintWriter(new FileWriter(outputFile));
  73. pw.println(attributeValue);
  74. pw.close();
  75. }
  76. /**
  77. * 保存文件
  78. * This method checks if the given file exists on disk. If it does it's ignored because
  79. * that means that the file is allready cached on the server. If not we take out the stream from the
  80. * digitalAsset-object and dumps it.
  81. */
  82. public void dumpAsset(File file, String fileName, String filePath) throws Exception
  83. {
  84. long timer = System.currentTimeMillis();
  85. File outputFile = new File(filePath + separator + fileName);
  86. if(outputFile.exists())
  87. {
  88. log.info("The file allready exists so we don't need to dump it again..");
  89. return;
  90. }
  91. FileOutputStream fis = new FileOutputStream(outputFile);
  92. BufferedOutputStream bos = new BufferedOutputStream(fis);
  93. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
  94. int character;
  95. while ((character = bis.read()) != -1)
  96. {
  97. bos.write(character);
  98. }
  99. bos.flush();
  100. bis.close();
  101. fis.close();
  102. bos.close();
  103. log.info("Time for dumping file " + fileName + ":" + (System.currentTimeMillis() - timer));
  104. }
  105. /**
  106. * 保存缩略图
  107. * This method checks if the given file exists on disk. If it does it's ignored because
  108. * that means that the file is allready cached on the server. If not we take out the stream from the
  109. * digitalAsset-object and dumps a thumbnail to it.
  110. */
  111. public void dumpAssetThumbnail(File file, String fileName, String thumbnailFile, String filePath, int width, int height, int quality) throws Exception
  112. {
  113. long timer = System.currentTimeMillis();
  114. log.info("fileName:" + fileName);
  115. log.info("thumbnailFile:" + thumbnailFile);
  116. File outputFile = new File(filePath + separator + thumbnailFile);
  117. if(outputFile.exists())
  118. {
  119. log.info("The file allready exists so we don't need to dump it again..");
  120. return;
  121. }
  122. ThumbnailGenerator tg = new ThumbnailGenerator();
  123. tg.transform(filePath + separator + fileName, filePath + separator + thumbnailFile, width, height, quality);
  124. log.info("Time for dumping file " + fileName + ":" + (System.currentTimeMillis() - timer));
  125. }
  126. /**
  127. * This method removes all images in the digitalAsset directory which belongs to a certain digital asset.
  128. */
  129. public void deleteDigitalAssets(String filePath, String filePrefix) throws Exception
  130. {
  131. try
  132. {
  133. File assetDirectory = new File(filePath);
  134. File[] files = assetDirectory.listFiles(new FilenameFilterImpl(filePrefix));
  135. for(int i=0; i<files.length; i++)
  136. {
  137. File file = files[i];
  138. log.info("Deleting file " + file.getPath());
  139. file.delete();
  140. }
  141. }
  142. catch(Exception e)
  143. {
  144. log.error("Could not delete the assets for the digitalAsset " + filePrefix + ":" + e.getMessage(), e);
  145. }
  146. }
  147. }

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. sshpass和做软链接

    参考: https://help.aliyun.com/document_detail/54530.html?spm=5176.11065259.1996646101.searchclickresul ...

  2. poj1741(入门点分治)

    题目链接:https://vjudge.net/problem/POJ-1741 题意:给出一棵树,求出树上距离不超过k的点对数量. 思路:点分治经典题.先找重心作为树根,然后求出子树中所有点到重心的 ...

  3. [HAOI2010]软件安装 题解

    题面 这道题比较显然地,是一道树形背包: 但是会有环,怎么办呢? 缩点!tarjan缩点! 然后在新图上跑树形背包就可以AC了 #include <bits/stdc++.h> #defi ...

  4. python爬虫实战--抖音

    申明&警告: 请在相关网站的许可范围内爬取数据.以免影响网站正常运行, 如果我的文章有触犯权益的地方, 请告知删除. 上一篇爬取知乎的文章基本就是大多数网站的爬取思路了(headers部分其实 ...

  5. 算法:二叉树的层次遍历(递归实现+非递归实现,lua)

    二叉树知识参考:深入学习二叉树(一) 二叉树基础 递归实现层次遍历算法参考:[面经]用递归方法对二叉树进行层次遍历 && 二叉树深度 上面第一篇基础写得不错,不了解二叉树的值得一看. ...

  6. Validator自动验证与手动验证

    自动: public JResult projectAdd(@Valid Project project, BindingResult result) {Map<String,Object> ...

  7. Python算法题(二)——国际象棋棋盘(排列组合问题,最小的K个数)

    题目一(输出国际象棋棋盘)  分析: 用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格.   主要代码: for i in range(8): for j in range(8 ...

  8. sqlserver关于发布订阅replication_subscription的总结

    (转载)sqlserver关于发布订阅replication_subscription的总结 来自 “ ITPUB博客 ” ,原文地址:http://blog.itpub.net/30126024/v ...

  9. ONNX源码安装

    ONNX是facebook提出的一个 Open Neural Network Exchange协议,能够让训练好的模型在不同的框架间进行交互. ONNX的安装相对来说不是特别麻烦,麻烦的是其依赖库的安 ...

  10. pycharm问题合集

    一  打开pycharm出现 点击右上角的配置之后 配置正确的python路径 又出现 解决办法 删除所有的解释器,据说是重名导致的. 然后在配置一次 二  ModuleNotFoundError: ...