FileUtils.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. package com.goafanti.common.utils;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.io.PrintWriter;
  8. import java.io.UnsupportedEncodingException;
  9. import java.net.URLEncoder;
  10. import java.util.regex.Pattern;
  11. import javax.servlet.http.HttpServletResponse;
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import com.goafanti.common.enums.AttachmentType;
  16. public class FileUtils {
  17. @Value(value = "${upload.private.path}")
  18. private String uploadPrivatePath = null;
  19. private static final Pattern SLASH_PATTERN = Pattern.compile("^/|/$");
  20. private static final String SLASH = "/";
  21. private static final String UNDERLINE = "_";
  22. /**
  23. * response 输出JSON
  24. *
  25. * @param hresponse
  26. * @param resultMap
  27. * @throws IOException
  28. */
  29. public static void out(HttpServletResponse response, String jsonStr) {
  30. PrintWriter out = null;
  31. try {
  32. response.setHeader("Content-Type", "application/json");
  33. response.setCharacterEncoding("UTF-8");
  34. out = response.getWriter();
  35. out.println(jsonStr);
  36. } catch (Exception e) {
  37. LoggerUtils.fmtError(FileUtils.class, e, "输出数据失败");
  38. } finally {
  39. if (null != out) {
  40. out.flush();
  41. out.close();
  42. }
  43. }
  44. }
  45. /**
  46. * Excel报表导出
  47. *
  48. * @param response
  49. * @param fileName
  50. * @param workbook
  51. */
  52. public static void downloadExcel(HttpServletResponse response, String fileName, HSSFWorkbook workbook) {
  53. String fn = null;
  54. try {
  55. fn = URLEncoder.encode(fileName, "UTF-8");
  56. } catch (UnsupportedEncodingException e1) {
  57. fn = fileName;
  58. }
  59. response.setContentType("application/x-download;charset=UTF-8");
  60. response.setHeader("Content-Disposition", "attachment; filename=\"" + fn + "\";");
  61. OutputStream out = null;
  62. try {
  63. out = response.getOutputStream();
  64. workbook.write(out);
  65. } catch (IOException e) {
  66. LoggerUtils.fmtError(FileUtils.class, e, "输出Excel失败");
  67. } finally {
  68. if (null != out) {
  69. try {
  70. out.flush();
  71. out.close();
  72. } catch (IOException e) {
  73. out = null;
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * 根据上传文件 拼接fileName
  80. *
  81. * @param mf
  82. * @param isPrivate
  83. * @param sign
  84. * @param path
  85. * @return
  86. */
  87. public static String mosaicFileName(MultipartFile mf, boolean isPrivate, String sign, String path, String uid) {
  88. String suffix = mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf("."));
  89. boolean uniq = false;
  90. if (sign.indexOf("demand_picture") != -1|| sign.indexOf("achievement_picture") != -1 || sign.indexOf("user_picture")!=-1) {
  91. uniq = true;
  92. }
  93. String fileName = "";
  94. if (isPrivate || StringUtils.isNotBlank(sign)) {
  95. if (uniq) {
  96. fileName = path + uid + "/" + System.nanoTime() + "_" + sign + suffix;
  97. } else {
  98. fileName = path + uid + "/" + sign + suffix;
  99. }
  100. } else {
  101. fileName = path + uid + "/" + System.nanoTime() + suffix;
  102. }
  103. return fileName;
  104. }
  105. /**
  106. * 获取下载文件名称
  107. *
  108. * @param path
  109. * @return
  110. */
  111. public static String getDownloadFileName(String path) {
  112. if (StringUtils.isBlank(path) || path.lastIndexOf(".") < 0) {
  113. return null;
  114. }
  115. String prefix = "附件";
  116. String suffix = path.substring(path.lastIndexOf("."));
  117. return prefix + suffix;
  118. }
  119. public static String getSuffix(String path) {
  120. if (StringUtils.isBlank(path) || path.lastIndexOf(".") < 0) {
  121. return "";
  122. }
  123. return path.substring(path.lastIndexOf("."));
  124. }
  125. /**
  126. * 拼接文件路径
  127. *
  128. * @param root
  129. * @param params
  130. * @return
  131. */
  132. public static String buildFilePath(String... params) {
  133. StringBuilder sb = new StringBuilder();
  134. if (params != null) {
  135. for (String s : params) {
  136. sb.append(SLASH);
  137. sb.append(SLASH_PATTERN.matcher(s).replaceAll(""));
  138. }
  139. }
  140. return sb.toString();
  141. }
  142. /**
  143. * 拼接文件名字
  144. *
  145. * @param root
  146. * @param params
  147. * @return
  148. */
  149. public static String buildFileName(String... params) {
  150. return StringUtils.join(params, UNDERLINE);
  151. }
  152. // year
  153. private static String subStr(String s) {
  154. return "企业" + s.substring(s.lastIndexOf(UNDERLINE) + 1, s.lastIndexOf("."));
  155. }
  156. public static boolean deleteFile(String realFilePath) {
  157. return new File(realFilePath).delete();
  158. }
  159. /**
  160. *
  161. * @param response
  162. * @param fileName
  163. * @param realFilePath
  164. */
  165. public static void downloadFile(HttpServletResponse response, String fileName, String realFilePath) {
  166. InputStream in = null;
  167. OutputStream out = null;
  168. byte[] buffer = new byte[8 * 1024];
  169. try {
  170. File file = new File(realFilePath);
  171. in = new FileInputStream(file);
  172. out = response.getOutputStream();
  173. // 设置文件MIME类型
  174. response.setContentType("application/octet-stream");
  175. fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
  176. response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  177. response.setHeader("Content-Length", String.valueOf(file.length()));
  178. for (;;) {
  179. int bytes = in.read(buffer);
  180. if (bytes == -1) {
  181. break;
  182. }
  183. out.write(buffer, 0, bytes);
  184. }
  185. } catch (IOException e) {
  186. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  187. } finally {
  188. try {
  189. if (in != null) {
  190. in.close();
  191. }
  192. } catch (IOException e) {
  193. in = null;
  194. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  195. }
  196. try {
  197. if (out != null) {
  198. out.close();
  199. }
  200. } catch (IOException e) {
  201. out = null;
  202. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  203. }
  204. }
  205. }
  206. }