FileUtil.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.gyee.impala.common.util;
  2. import com.gyee.impala.common.exception.CustomException;
  3. import com.gyee.impala.common.result.ResultCode;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.core.io.Resource;
  6. import org.springframework.core.io.UrlResource;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.io.*;
  9. import java.net.MalformedURLException;
  10. import java.net.URLEncoder;
  11. import java.nio.charset.StandardCharsets;
  12. import java.nio.file.Path;
  13. import java.util.List;
  14. @Slf4j
  15. public class FileUtil {
  16. /**
  17. * 写入文件
  18. *
  19. * @param fileName
  20. * @param content
  21. */
  22. public static void writeFile(String fileName, String content) {
  23. BufferedWriter bw = null;
  24. try {
  25. File file = new File(fileName);
  26. if (!file.exists())
  27. file.createNewFile();
  28. bw = new BufferedWriter(new FileWriter(file, true));
  29. String[] list = content.split("\n");
  30. for(int i = 0; i < list.length; i++){
  31. bw.write(list[i]);
  32. bw.write("\n");
  33. if (i % 500 == 0)
  34. bw.flush();
  35. }
  36. bw.flush();
  37. } catch (Exception e) {
  38. log.error(e.getMessage());
  39. } finally {
  40. try {
  41. bw.close();
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. }
  46. }
  47. /**
  48. * 写入文件
  49. *
  50. * @param path
  51. * @param fileName
  52. * @param inputStream
  53. */
  54. public static void writeFile(String path, String fileName, InputStream inputStream) {
  55. OutputStream os = null;
  56. try {
  57. byte[] bs = new byte[1024];
  58. int len;
  59. File file = new File(path);
  60. if (!file.exists()) {
  61. file.mkdirs();
  62. }
  63. os = new FileOutputStream(file.getPath() + File.separator + fileName);
  64. // 开始读取
  65. while ((len = inputStream.read(bs)) != -1) {
  66. os.write(bs, 0, len);
  67. }
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. log.error(e.getMessage());
  71. } catch (Exception e) {
  72. e.printStackTrace();
  73. log.error(e.getMessage());
  74. } finally {
  75. // 完毕,关闭所有链接
  76. try {
  77. os.close();
  78. inputStream.close();
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. log.error(e.getMessage());
  82. }
  83. }
  84. }
  85. // String -> InputStream
  86. public static InputStream convertStringToInputStream(String content) {
  87. InputStream result = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
  88. return result;
  89. }
  90. /**
  91. * 加载文件
  92. *
  93. * @param fileName
  94. * @return
  95. */
  96. public static void download(String fileName, HttpServletResponse response) {
  97. FileInputStream fis = null;
  98. BufferedInputStream bis = null;
  99. try {
  100. // 获取文件
  101. File file = new File(fileName);
  102. // 清空缓冲区,状态码和响应头(headers)
  103. response.reset();
  104. // 设置ContentType,响应内容为二进制数据流,编码为utf-8,此处设定的编码是文件内容的编码
  105. response.setContentType("application/octet-stream;charset=utf-8");
  106. // 以(Content-Disposition: attachment; filename="filename.jpg")格式设定默认文件名,设定utf编码,此处的编码是文件名的编码,使能正确显示中文文件名
  107. response.setHeader("Content-Disposition", "attachment;fileName=" + file.getName() + ";filename*=utf-8''" + URLEncoder.encode(file.getName(), "utf-8"));
  108. // 实现文件下载
  109. byte[] buffer = new byte[1024];
  110. fis = new FileInputStream(file);
  111. bis = new BufferedInputStream(fis);
  112. // 获取字节流
  113. OutputStream os = response.getOutputStream();
  114. int i = bis.read(buffer);
  115. while (i != -1) {
  116. os.write(buffer, 0, i);
  117. i = bis.read(buffer);
  118. }
  119. } catch (FileNotFoundException e) {
  120. throw new CustomException(ResultCode.ERROR_FILE_NO);
  121. } catch (Exception e) {
  122. throw new CustomException(ResultCode.ERROR);
  123. } finally {
  124. if (bis != null) {
  125. try {
  126. bis.close();
  127. } catch (IOException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. if (fis != null) {
  132. try {
  133. fis.close();
  134. } catch (IOException e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. }
  139. }
  140. }