ImageFileUtils.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package com.steerinfo.route.config;
  2. import com.steerinfo.framework.utils.misc.IdGenerator;
  3. import com.steerinfo.framework.utils.upload.UploadUtils;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Component;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.IOException;
  11. import java.util.*;
  12. @Component
  13. public class ImageFileUtils implements FileUtils {
  14. @Value(value = "${piction.path:/shared}" )
  15. private String path;
  16. public static final HashMap fileTypes = new HashMap();
  17. static {
  18. // images
  19. fileTypes.put("FFD8FF", "jpg");
  20. fileTypes.put("89504E47", "png");
  21. fileTypes.put("47494638", "gif");
  22. fileTypes.put("49492A00", "tif");
  23. fileTypes.put("424D", "bmp");
  24. // CAD
  25. fileTypes.put("41433130", "dwg");
  26. fileTypes.put("38425053", "psd");
  27. // 日记本
  28. fileTypes.put("7B5C727466", "rtf");
  29. fileTypes.put("3C3F786D6C", "xml");
  30. fileTypes.put("68746D6C3E", "html");
  31. // 邮件
  32. fileTypes.put("44656C69766572792D646174653A", "eml");
  33. fileTypes.put("D0CF11E0", "doc");
  34. //excel2003版本文件
  35. fileTypes.put("D0CF11E0", "xls");
  36. fileTypes.put("5374616E64617264204A", "mdb");
  37. fileTypes.put("252150532D41646F6265", "ps");
  38. fileTypes.put("255044462D312E", "pdf");
  39. fileTypes.put("504B0304", "docx");
  40. //excel2007以上版本文件
  41. fileTypes.put("504B0304", "xlsx");
  42. fileTypes.put("52617221", "rar");
  43. fileTypes.put("57415645", "wav");
  44. fileTypes.put("41564920", "avi");
  45. fileTypes.put("2E524D46", "rm");
  46. fileTypes.put("000001BA", "mpg");
  47. fileTypes.put("000001B3", "mpg");
  48. fileTypes.put("6D6F6F76", "mov");
  49. fileTypes.put("3026B2758E66CF11", "asf");
  50. fileTypes.put("4D546864", "mid");
  51. fileTypes.put("1F8B08", "gz");
  52. }
  53. /**
  54. * 上传多张图片
  55. * @param files
  56. * @return
  57. * @throws Exception
  58. */
  59. @Override
  60. public synchronized List<String> updateFiles(MultipartFile files[]) throws Exception {
  61. //首先通过Calendard对象获得年月日
  62. Calendar calendar= Calendar.getInstance();
  63. int year = calendar.get(Calendar.YEAR);
  64. int month = calendar.get(Calendar.MONTH);
  65. int day= calendar.get(Calendar.DAY_OF_MONTH);
  66. //上传文件夹路径
  67. List<String> urls=new ArrayList<>();
  68. for (int i=0;i<files.length;i++){
  69. String url = UploadUtils.uploadFile(files[i], new IdGenerator(i, 10), path, File.separator+year+ File.separator+month+ File.separator+day+ File.separator);
  70. urls.add(url);
  71. }
  72. return urls;
  73. }
  74. /**
  75. * 上传单张图片
  76. * @param file
  77. * @return
  78. * @throws Exception
  79. */
  80. public synchronized String updateFile(MultipartFile file) throws Exception {
  81. //首先通过Calendard对象获得年月日
  82. Calendar calendar= Calendar.getInstance();
  83. int year = calendar.get(Calendar.YEAR);
  84. int month = calendar.get(Calendar.MONTH);
  85. int day= calendar.get(Calendar.DAY_OF_MONTH);
  86. String url = UploadUtils.uploadFile(file, new IdGenerator(0, 10), path, File.separator+year+ File.separator+month+ File.separator+day+ File.separator);
  87. return url;
  88. }
  89. /**
  90. * 下载图片获得字节码
  91. * @param filePath
  92. * @return
  93. * @throws Exception
  94. */
  95. @Override
  96. public Object downloadFile(String filePath) throws Exception {
  97. File file = new File(filePath);
  98. if(file.isDirectory()){
  99. throw new RuntimeException("当前路径是目录");
  100. }
  101. byte[] b = bytes(file);
  102. String type =getFileHeader(b);
  103. String src="data:image/"+type+";base64,"+ Base64.getEncoder().encodeToString(b);
  104. return src;
  105. }
  106. /**
  107. * @return 文件头信息
  108. * @author liang.pan
  109. * <p>
  110. * 方法描述:根据输入流获取文件头信息
  111. */
  112. public static String getFileHeader(byte[] b) {
  113. String value = bytesToHexString(b);
  114. if (StringUtils.startsWith(value, "FFD8FF")) {
  115. value = value.substring(0, 6);
  116. }
  117. //判断什么类型的
  118. Set set = fileTypes.keySet();
  119. Iterator iterator = set.iterator();
  120. while(iterator.hasNext()){
  121. String key=iterator.next().toString();
  122. if (value.contains(key)){
  123. return fileTypes.get(key).toString();
  124. }
  125. }
  126. return null;
  127. }
  128. /**
  129. * @param src 要读取文件头信息的文件的byte数组
  130. * @return 文件头信息
  131. * @author liang.pan
  132. * <p>
  133. * 方法描述:将要读取文件头信息的文件的byte数组转换成string类型表示
  134. */
  135. private static String bytesToHexString(byte[] src) {
  136. StringBuilder builder = new StringBuilder();
  137. if (src == null || src.length <= 0) {
  138. return null;
  139. }
  140. String hv;
  141. for (int i = 0; i < src.length; i++) {
  142. // 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式,并转换为大写
  143. hv = Integer.toHexString(src[i] & 0xFF).toUpperCase();
  144. if (hv.length() < 2) {
  145. builder.append(0);
  146. }
  147. builder.append(hv);
  148. }
  149. return builder.toString();
  150. }
  151. public byte[] bytes(File file) throws Exception {
  152. FileInputStream fin = new FileInputStream(file);
  153. try {
  154. //可能溢出,简单起见就不考虑太多,如果太大就要另外想办法,比如一次传入固定长度byte[]
  155. byte[] bytes = new byte[fin.available()];
  156. //将文件内容写入字节数组,提供测试的case
  157. fin.read(bytes);
  158. fin.close();
  159. return bytes;
  160. }catch (Exception ex){
  161. throw ex;
  162. }
  163. finally {
  164. if (null != fin) {
  165. try {
  166. fin.close();
  167. } catch (IOException e) {
  168. }
  169. }
  170. }
  171. }
  172. }