FileUtil.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package com.caimei.utils;
  2. import org.apache.commons.io.FileUtils;
  3. import javax.imageio.ImageIO;
  4. import javax.imageio.ImageReader;
  5. import javax.imageio.stream.ImageInputStream;
  6. import java.awt.image.BufferedImage;
  7. import java.io.*;
  8. import java.util.HashMap;
  9. import java.util.Iterator;
  10. import java.util.Map;
  11. public class FileUtil {
  12. public final static Map<String, String> FILE_TYPE_MAP = new HashMap<String, String>();
  13. static {
  14. getAllFileType(); // 初始化文件类型信息
  15. }
  16. /**
  17. * Created on 2013-1-21
  18. * <p>
  19. * Discription:[getAllFileType,常见文件头信息]
  20. *
  21. * @author:shaochangfu
  22. */
  23. private static void getAllFileType() {
  24. FILE_TYPE_MAP.put("jpg", "FFD8FF"); // JPEG (jpg)
  25. FILE_TYPE_MAP.put("png", "89504E47");// PNG (png)
  26. FILE_TYPE_MAP.put("gif", "47494638");// GIF (gif)
  27. FILE_TYPE_MAP.put("bmp", "424D"); // Windows Bitmap (bmp)
  28. FILE_TYPE_MAP.put("zip", "504B0304");// zip 压缩文件
  29. }
  30. public FileUtil() {
  31. }
  32. /**
  33. * 把from路径下的文件拷贝到to路径下
  34. *
  35. * @param from
  36. * @param to
  37. * @throws Exception
  38. */
  39. public static void copy(String from, String to) throws Exception {
  40. int BUFF_SIZE = 100000;
  41. byte[] buffer = new byte[BUFF_SIZE];
  42. InputStream in = null;
  43. OutputStream out = null;
  44. try {
  45. in = new FileInputStream(from);
  46. out = new FileOutputStream(to);
  47. while (true) {
  48. synchronized (buffer) {
  49. int amountRead = in.read(buffer);
  50. if (amountRead == -1) {
  51. break;
  52. }
  53. out.write(buffer, 0, amountRead);
  54. }
  55. }
  56. } finally {
  57. if (in != null) {
  58. in.close();
  59. }
  60. if (out != null) {
  61. out.close();
  62. }
  63. }
  64. }
  65. /**
  66. * 把文件移动到to路径下
  67. *
  68. * @param file
  69. * @param toPath
  70. * @throws IOException
  71. */
  72. public static void moveFile(File file, String toPath, String newName)
  73. throws IOException {
  74. /*
  75. * file.renameTo(new File((new StringBuilder(String.valueOf(s))).append(
  76. * File.separator).append(file.getName()).toString()));
  77. */
  78. FileUtils.copyFile(
  79. file,
  80. new File((new StringBuilder(String.valueOf(toPath)))
  81. .append(File.separator).append(newName).toString()));
  82. }
  83. /**
  84. * 按照图片命名规则生成一个不带后缀的图片名
  85. *
  86. * @param originalFullName
  87. * @return
  88. * @throws Exception
  89. */
  90. public static String getNewImageName(String originalFullName)
  91. throws Exception {
  92. /*
  93. * int dotIndex = originalFullName.lastIndexOf('.'); if (dotIndex == -1)
  94. * { dotIndex = 0; }
  95. */
  96. String fileName = RandomCodeGenerator.generateCodeString(10);
  97. return (new StringBuilder(String.valueOf(fileName))).append(
  98. DateTimeUtil.getCurrentDateTime().replaceAll("-", "")
  99. .replaceAll(":", "").replaceAll(" ", "")).toString();
  100. }
  101. /**
  102. * 删除path路径下的文件
  103. *
  104. * @param path
  105. */
  106. public static void deleteFile(String path) {
  107. File file = new File(path);
  108. if (file.exists())
  109. file.delete();
  110. }
  111. /**
  112. * 得到文件的真实后缀名 (文件种类)
  113. *
  114. * @param f
  115. * @return
  116. * @throws Exception
  117. */
  118. public final static String getImageFileType(File f)
  119. throws Exception {
  120. if (isImage(f)) {
  121. try {
  122. if (f.length() > 5242880) {
  123. throw new Exception("图片过大");
  124. }
  125. ImageInputStream iis = ImageIO.createImageInputStream(f);
  126. Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
  127. if (!iter.hasNext()) {
  128. return null;
  129. }
  130. ImageReader reader = iter.next();
  131. iis.close();
  132. return reader.getFormatName();
  133. } catch (Exception e) {
  134. e.printStackTrace();
  135. throw new Exception("图片后缀名检验异常");
  136. }
  137. } else {
  138. throw new Exception("图片后缀名检验异常");
  139. }
  140. }
  141. /**
  142. * Created on 2013-1-21
  143. * <p>
  144. * Discription:[isImage,判断文件是否为图片]
  145. * </p>
  146. *
  147. * @param file
  148. * @return true 是 | false 否
  149. * @author:shaochangfu
  150. */
  151. public static final boolean isImage(File file) {
  152. boolean flag = false;
  153. try {
  154. BufferedImage bufreader = ImageIO.read(file);
  155. int width = bufreader.getWidth();
  156. int height = bufreader.getHeight();
  157. if (width == 0 || height == 0) {
  158. flag = false;
  159. } else {
  160. flag = true;
  161. }
  162. } catch (IOException e) {
  163. flag = false;
  164. } catch (Exception e) {
  165. flag = false;
  166. }
  167. return flag;
  168. }
  169. }