FileUtil.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package cn.com.ctop.shiwan.modules;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.fileupload.FileItem;
  4. import org.apache.commons.fileupload.FileItemFactory;
  5. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  6. import org.springframework.web.multipart.MultipartFile;
  7. import org.springframework.web.multipart.commons.CommonsMultipartFile;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13. import java.io.OutputStream;
  14. import java.net.URL;
  15. import java.util.Date;
  16. import java.util.zip.ZipEntry;
  17. import java.util.zip.ZipOutputStream;
  18. /**
  19. * @ClassName ZipUtil
  20. * @Author ZHAOXA
  21. * @date 2020-10-23 10:05
  22. */
  23. @Slf4j
  24. public class FileUtil {
  25. /**
  26. * 转MultipartFile格式文件
  27. *
  28. * @param
  29. * @return org.springframework.web.multipart.MultipartFile
  30. * @throws
  31. * @author ZHAOXA
  32. */
  33. public static MultipartFile getMulFileByPath(String picPath) {
  34. FileItem fileItem = createFileItem(picPath);
  35. MultipartFile mfile = new CommonsMultipartFile(fileItem);
  36. return mfile;
  37. }
  38. private static FileItem createFileItem(String filePath) {
  39. FileItemFactory factory = new DiskFileItemFactory(16, null);
  40. String textFieldName = "textField";
  41. int num = filePath.lastIndexOf("/");
  42. String fileName = filePath.substring(num);
  43. FileItem item = factory.createItem(textFieldName, "text/plain", true,
  44. fileName);
  45. File newfile = new File(filePath);
  46. int bytesRead = 0;
  47. byte[] buffer = new byte[8192];
  48. try {
  49. FileInputStream fis = new FileInputStream(newfile);
  50. OutputStream os = item.getOutputStream();
  51. while ((bytesRead = fis.read(buffer, 0, 8192))
  52. != -1) {
  53. os.write(buffer, 0, bytesRead);
  54. }
  55. os.close();
  56. fis.close();
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. return item;
  61. }
  62. /**
  63. * 下载文件到本地
  64. *
  65. * @param locationPath 本地路径
  66. * @param fileUrl 文件下载路径
  67. * @param fileName 下载后的名称含后缀
  68. * @return java.lang.String
  69. * @throws
  70. * @author ZHAOXA
  71. */
  72. public static String writeFiles(String locationPath, String fileUrl, String fileName) {
  73. InputStream is = null;
  74. OutputStream os = null;
  75. Date date = new Date();
  76. String realPath = null;
  77. try {
  78. URL url = new URL(fileUrl);
  79. is = url.openStream();
  80. File file = new File(locationPath);
  81. if (!file.exists()) {
  82. boolean mkdirFlag = file.mkdirs();
  83. if (!mkdirFlag) {
  84. log.error("[ ========== 下载,创建临时文件夹失败 ========== ]");
  85. return null;
  86. }
  87. }
  88. realPath = locationPath.concat(fileName);
  89. os = new FileOutputStream(realPath);
  90. int bytesRead = 0;
  91. byte[] buffer = new byte[8192];
  92. while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
  93. os.write(buffer, 0, bytesRead);
  94. os.flush();
  95. }
  96. } catch (Exception e) {
  97. log.error(" ========== 文件下载到本地异常:", e);
  98. return null;
  99. } finally {
  100. try {
  101. if (os != null) {
  102. os.close();
  103. }
  104. if (is != null) {
  105. is.close();
  106. }
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. return realPath;
  112. }
  113. /**
  114. * 通过MultipartFile获取本地下载路径
  115. *
  116. * @param
  117. * @return java.lang.String
  118. * @throws
  119. * @author ZHAOXA
  120. */
  121. public static String approvalFile(MultipartFile filecontent, String path) {
  122. OutputStream os = null;
  123. InputStream inputStream = null;
  124. String fileName = filecontent.getOriginalFilename();
  125. try {
  126. inputStream = filecontent.getInputStream();
  127. } catch (IOException e) {
  128. log.error("获取文件流异常");
  129. }
  130. try {
  131. byte[] bs = new byte[1024];
  132. // 读取到的数据长度
  133. int len;
  134. // 输出的文件流保存到本地文件
  135. File tempFile = new File(path);
  136. if (!tempFile.exists()) {
  137. tempFile.mkdirs();
  138. }
  139. os = new FileOutputStream(tempFile.getPath() + "/" + File.separator + fileName);
  140. // 开始读取
  141. while ((len = inputStream.read(bs)) != -1) {
  142. os.write(bs, 0, len);
  143. }
  144. } catch (Exception e) {
  145. log.error("获取文件的本地路径失败", e);
  146. } finally {
  147. // 完毕,关闭所有链接
  148. try {
  149. os.close();
  150. inputStream.close();
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. return path + fileName;
  156. }
  157. /**
  158. * @param zipSavePath 压缩好的zip包存放路径
  159. * @param sourceFile 待压缩的文件(单个文件或者整个文件目录)
  160. * @return
  161. * @Description
  162. * @author xukaixun
  163. */
  164. public static String zipCompress(String zipSavePath, File sourceFile) {
  165. try {
  166. //创建zip输出流
  167. ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipSavePath));
  168. File[] fileList = sourceFile.listFiles();
  169. if (fileList.length != 0)//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
  170. {
  171. for (File file : fileList) {
  172. compress(zos, file, sourceFile.getName() + "/" + file.getName());
  173. }
  174. zos.close();
  175. }
  176. } catch (Exception e) {
  177. log.error("zip compress file exception: {}, zipSavePath={}, sourceFile={}", e, zipSavePath, sourceFile.getName());
  178. }
  179. return zipSavePath;
  180. }
  181. /**
  182. * 递归压缩文件
  183. *
  184. * @param
  185. * @return void
  186. * @throws
  187. * @author ZHAOXA
  188. */
  189. private static void compress(ZipOutputStream zos, File sourceFile, String fileName) throws Exception {
  190. if (sourceFile.isDirectory()) {
  191. //如果是文件夹,取出文件夹中的文件(或子文件夹)
  192. File[] fileList = sourceFile.listFiles();
  193. if (fileList.length == 0)//如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
  194. {
  195. zos.putNextEntry(new ZipEntry(fileName + "/"));
  196. } else//如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
  197. {
  198. for (File file : fileList) {
  199. compress(zos, file, fileName + "/" + file.getName());
  200. }
  201. }
  202. } else {
  203. if (!sourceFile.exists()) {
  204. zos.putNextEntry(new ZipEntry("/"));
  205. zos.closeEntry();
  206. } else {
  207. //单个文件,直接将其压缩到zip包中
  208. zos.putNextEntry(new ZipEntry(fileName));
  209. FileInputStream fis = new FileInputStream(sourceFile);
  210. byte[] buf = new byte[1024];
  211. int len;
  212. //将源文件写入到zip文件中
  213. while ((len = fis.read(buf)) != -1) {
  214. zos.write(buf, 0, len);
  215. }
  216. zos.closeEntry();
  217. fis.close();
  218. }
  219. }
  220. }
  221. }