904f68013d415d5981f749b7145c4f1393265308.svn-base 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package cn.com.goldenwater.dcproj.utils.impexcel;
  2. import cn.com.goldenwater.dcproj.model.GwComFile;
  3. import cn.com.goldenwater.id.util.UuidUtil;
  4. import org.apache.commons.io.FileUtils;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.springframework.web.multipart.MultipartFile;
  7. import java.io.*;
  8. import java.nio.charset.Charset;
  9. import java.util.ArrayList;
  10. import java.util.Calendar;
  11. import java.util.List;
  12. import java.util.zip.ZipEntry;
  13. import java.util.zip.ZipInputStream;
  14. /**
  15. * Created by jinshui on 2019/11/1.
  16. */
  17. public class FileUtil {
  18. private FileUtil() {
  19. }
  20. private final static String PDF = "pdf";
  21. private final static String DOC = "doc";
  22. private final static String DOCX = "docX";
  23. private final static String HTML = "html";
  24. //压缩包列表
  25. private final static String[] CP = {"zip", "rar"};
  26. /**
  27. * 根据上传文件 得到文件列表
  28. *
  29. * @param file
  30. * @return
  31. */
  32. public static List<GwComFile> getFileList(MultipartFile file, String prefix, String fileDir) throws Exception {
  33. List<GwComFile> list = new ArrayList<>();
  34. //获取 文件名.格式
  35. String originalFilename = file.getOriginalFilename();
  36. String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
  37. if (StringUtils.isBlank(ext)) {
  38. return new ArrayList<>();
  39. }
  40. Calendar dat = Calendar.getInstance();
  41. String yearPath = dat.get(Calendar.YEAR) + File.separator + (dat.get(Calendar.MONTH) + 1) + File.separator + dat.get(Calendar.DAY_OF_MONTH);
  42. fileDir = fileDir + yearPath;
  43. prefix = prefix + yearPath;
  44. int index = isZip(ext);
  45. //是否是压缩包
  46. if (index < 0) {
  47. //单个文件
  48. GwComFile gwComFile = getGwComFile(file.getName(), (double) file.getSize(), prefix);
  49. FileUtils.copyInputStreamToFile(file.getInputStream(), new File(fileDir, gwComFile.getId() + "." + gwComFile.getFileExt()));
  50. list.add(gwComFile);
  51. return list;
  52. }
  53. switch (index) {
  54. case 0:
  55. list.addAll(dealZipFile(file, prefix, fileDir));
  56. break;
  57. case 1:
  58. break;
  59. case 2:
  60. break;
  61. case 3:
  62. break;
  63. }
  64. return list;
  65. }
  66. private static List<GwComFile> dealZipFile(MultipartFile file, String prefix, String fileDir) throws IOException {
  67. List<GwComFile> list = new ArrayList<>();
  68. ZipInputStream zpIs = new ZipInputStream(file.getInputStream(), Charset.forName(getFilecharset(file)));
  69. ZipEntry ze = null;
  70. String path = fileDir;
  71. File pathFile = new File(path);
  72. if (!pathFile.exists()) {
  73. pathFile.mkdirs();
  74. }
  75. while ((ze = zpIs.getNextEntry()) != null) {
  76. if (ze.isDirectory() || ze.getName().endsWith("/")) {
  77. //暂时不处理压缩包里有文件夹这种形式
  78. continue;
  79. }
  80. GwComFile gwComFile = getGwComFile(ze.getName(), (double) ze.getSize(), prefix);
  81. ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
  82. byte[] byte_s=new byte[1024];
  83. int num=-1;
  84. while((num=zpIs.read(byte_s,0,byte_s.length))>-1) {//通过read方法来读取文件内容
  85. byteArrayOutputStream.write(byte_s, 0, num);
  86. }
  87. File outFile = new File(fileDir, gwComFile.getId() + "." + gwComFile.getFileExt());
  88. FileOutputStream fileOutputStream = new FileOutputStream(outFile);
  89. fileOutputStream.write(byteArrayOutputStream.toByteArray());
  90. list.add(gwComFile);
  91. byteArrayOutputStream.close();
  92. fileOutputStream.close();
  93. }
  94. zpIs.close();
  95. return list;
  96. }
  97. private static int isZip(String ext) {
  98. for (int i = 0; i < CP.length; i++) {
  99. if (CP[i].equalsIgnoreCase(ext)) {
  100. return i;
  101. }
  102. }
  103. return -1;
  104. }
  105. public static GwComFile getGwComFile(String fileName, double size, String prefix) throws IOException {
  106. GwComFile gwComFile = new GwComFile();
  107. gwComFile.setId(UuidUtil.uuid());
  108. gwComFile.setCreateDate(Calendar.getInstance().getTime());
  109. gwComFile.setFileTitle(fileName);
  110. gwComFile.setFileName(fileName);
  111. gwComFile.setFileSize(size);
  112. gwComFile.setFileExt(fileName.substring(1 + fileName.lastIndexOf(".")));
  113. String filePath = prefix + File.separator + gwComFile.getId() + "." + gwComFile.getFileExt();
  114. gwComFile.setFilePath(filePath);
  115. return gwComFile;
  116. }
  117. //判断编码格式方法
  118. public static String getFilecharset(MultipartFile file) {
  119. String charset = "GBK";
  120. byte[] first3Bytes = new byte[3];
  121. try {
  122. boolean checked = false;
  123. BufferedInputStream bis = new BufferedInputStream(file.getInputStream());
  124. bis.mark(0);
  125. int read = bis.read(first3Bytes, 0, 3);
  126. if (read == -1) {
  127. return charset; //文件编码为 ANSI
  128. } else if (first3Bytes[0] == (byte) 0xFF
  129. && first3Bytes[1] == (byte) 0xFE) {
  130. charset = "UTF-16LE"; //文件编码为 Unicode
  131. checked = true;
  132. } else if (first3Bytes[0] == (byte) 0xFE
  133. && first3Bytes[1] == (byte) 0xFF) {
  134. charset = "UTF-16BE"; //文件编码为 Unicode big endian
  135. checked = true;
  136. } else if (first3Bytes[0] == (byte) 0xEF
  137. && first3Bytes[1] == (byte) 0xBB
  138. && first3Bytes[2] == (byte) 0xBF) {
  139. charset = "UTF-8"; //文件编码为 UTF-8
  140. checked = true;
  141. }
  142. bis.reset();
  143. if (!checked) {
  144. int loc = 0;
  145. while ((read = bis.read()) != -1) {
  146. loc++;
  147. if (read >= 0xF0) {
  148. break;
  149. }
  150. if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
  151. {
  152. break;
  153. }
  154. if (0xC0 <= read && read <= 0xDF) {
  155. read = bis.read();
  156. if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)
  157. {
  158. continue;
  159. } else {
  160. break;
  161. }
  162. } else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小
  163. read = bis.read();
  164. if (0x80 <= read && read <= 0xBF) {
  165. read = bis.read();
  166. if (0x80 <= read && read <= 0xBF) {
  167. charset = "UTF-8";
  168. break;
  169. } else {
  170. break;
  171. }
  172. } else {
  173. break;
  174. }
  175. }
  176. }
  177. }
  178. bis.close();
  179. } catch (Exception e) {
  180. e.printStackTrace();
  181. }
  182. return charset;
  183. }
  184. }