FileUtils.java 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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("protocol") != -1 || sign.indexOf("achievement") != -1 || sign.indexOf("honor") != -1
  91. || sign.indexOf("proof") != -1 || sign.indexOf("activity_cost_account") != -1
  92. || sign.indexOf("tech_product") != -1 || sign.indexOf("property_ritht") != -1
  93. || sign.indexOf("tech_project") != -1 || sign.indexOf("patent_prory_statement") != -1
  94. || sign.indexOf("patent_writing") != -1 || sign.indexOf("authorization_notice") != -1
  95. || sign.indexOf("patent_certificate") != -1 || sign.indexOf("standard") != -1
  96. || sign.indexOf("demand_picture") != -1 || sign.indexOf("demand_text_file") != -1
  97. || sign.indexOf("achievement_technical_picture") != -1
  98. || sign.indexOf("achievement_maturity_picture") != -1 || sign.indexOf("demand_order_file") != -1
  99. || sign.indexOf("lecture_picture") != -1|| sign.indexOf("cover_picture") != -1
  100. || sign.indexOf("news") != -1||sign.indexOf("customer_sys_file") != -1
  101. ||sign.indexOf("business_project_max_picture") != -1||sign.indexOf("business_project_min_picture") != -1
  102. ||sign.indexOf("varieties_picture") != -1) {
  103. uniq = true;
  104. }
  105. String fileName = "";
  106. if (isPrivate || StringUtils.isNotBlank(sign)) {
  107. if (uniq) {
  108. fileName = path + uid + "/" + System.nanoTime() + "_" + sign + suffix;
  109. } else {
  110. fileName = path + uid + "/" + sign + suffix;
  111. }
  112. } else {
  113. fileName = path + uid + "/" + System.nanoTime() + suffix;
  114. }
  115. return fileName;
  116. }
  117. /**
  118. * 获取下载文件名称
  119. *
  120. * @param path
  121. * @return
  122. */
  123. public static String getDownloadFileName(String path) {
  124. if (StringUtils.isBlank(path) || path.lastIndexOf(".") < 0) {
  125. return null;
  126. }
  127. String prefix = "附件";
  128. String suffix = path.substring(path.lastIndexOf("."));
  129. if (path.indexOf(AttachmentType.PATENT_PRORY_STATEMENT.getCode()) != -1) {
  130. prefix = "专利代理委托书";
  131. } else if (path.indexOf(AttachmentType.PATENT_WRITING.getCode()) != -1) {
  132. prefix = "专利稿件";
  133. } else if (path.indexOf(AttachmentType.AUTHORIZATION_NOTICE.getCode()) != -1) {
  134. prefix = "授权通知书";
  135. } else if (path.indexOf(AttachmentType.PATENT_CERTIFICATE.getCode()) != -1) {
  136. prefix = "专利证书";
  137. } else if (path.indexOf(AttachmentType.PROOF.getCode()) != -1) {
  138. prefix = "立项证明材料";
  139. } else if (path.indexOf(AttachmentType.ACTIVITY_COST_ACCOUNT.getCode()) != -1) {
  140. prefix = "研发活动费用台帐";
  141. } else if (path.indexOf(AttachmentType.TECH_PRODUCT.getCode()) != -1) {
  142. prefix = "高新技术产品台帐";
  143. } else if (path.indexOf(AttachmentType.PROPERTY_RIGHT.getCode()) != -1) {
  144. prefix = "知识产权证书";
  145. } else if (path.indexOf(AttachmentType.ROSTER.getCode()) != -1) {
  146. prefix = subStr(path) + "花名册";
  147. } else if (path.indexOf(AttachmentType.SOCIAL_SECURITY.getCode()) != -1) {
  148. prefix = subStr(path) + "社保情况";
  149. } else if (path.indexOf(AttachmentType.HONOR.getCode()) != -1) {
  150. prefix = "荣誉材料证明";
  151. } else if (path.indexOf(AttachmentType.ACHIEVEMENT.getCode()) != -1) {
  152. prefix = "科技成果附件";
  153. } else if (path.indexOf(AttachmentType.INSTITUTION.getCode()) != -1) {
  154. prefix = "制度目录";
  155. } else if (path.indexOf(AttachmentType.PROTOCOL.getCode()) != -1) {
  156. prefix = "技术协议";
  157. } else if (path.indexOf(AttachmentType.FINANCE.getCode()) != -1) {
  158. prefix = subStr(path) + "财务报表";
  159. } else if (path.indexOf(AttachmentType.RATEPAY.getCode()) != -1) {
  160. prefix = subStr(path) + "纳税申报表";
  161. } else if (path.indexOf(AttachmentType.TECH_PROJECT.getCode()) != -1) {
  162. prefix = "科技项目资料";
  163. } else if (path.indexOf(AttachmentType.STANDARD.getCode()) != -1) {
  164. prefix = "标准制定";
  165. } else if (path.indexOf(AttachmentType.MANUSCRIPT.getCode()) != -1) {
  166. prefix = "稿件";
  167. } else if (path.indexOf(AttachmentType.DEMAND_TEXT_FILE.getCode()) != -1) {
  168. prefix = "科技需求文本文件资料";
  169. } else if (path.indexOf(AttachmentType.ACHIEVEMENT_MATURITY_TEXT_FILE.getCode()) != -1) {
  170. prefix = "科技成果成熟度资料文本文件";
  171. } else if (path.indexOf(AttachmentType.ACHIEVEMENT_TECH_PLAN.getCode()) != -1) {
  172. prefix = "科技成果技术方案文件";
  173. } else if (path.indexOf(AttachmentType.ACHIEVEMENT_BUSINESS_PLAN.getCode()) != -1) {
  174. prefix = "科技成果商业计划书";
  175. }
  176. return prefix + suffix;
  177. }
  178. public static String getSuffix(String path) {
  179. if (StringUtils.isBlank(path) || path.lastIndexOf(".") < 0) {
  180. return "";
  181. }
  182. return path.substring(path.lastIndexOf("."));
  183. }
  184. /**
  185. * 拼接文件路径
  186. *
  187. * @param root
  188. * @param params
  189. * @return
  190. */
  191. public static String buildFilePath(String... params) {
  192. StringBuilder sb = new StringBuilder();
  193. if (params != null) {
  194. for (String s : params) {
  195. sb.append(SLASH);
  196. sb.append(SLASH_PATTERN.matcher(s).replaceAll(""));
  197. }
  198. }
  199. return sb.toString();
  200. }
  201. /**
  202. * 拼接文件名字
  203. *
  204. * @param root
  205. * @param params
  206. * @return
  207. */
  208. public static String buildFileName(String... params) {
  209. return StringUtils.join(params, UNDERLINE);
  210. }
  211. // year
  212. private static String subStr(String s) {
  213. return "企业" + s.substring(s.lastIndexOf(UNDERLINE) + 1, s.lastIndexOf("."));
  214. }
  215. public static boolean deleteFile(String realFilePath) {
  216. return new File(realFilePath).delete();
  217. }
  218. /**
  219. *
  220. * @param response
  221. * @param fileName
  222. * @param realFilePath
  223. */
  224. public static void downloadFile(HttpServletResponse response, String fileName, String realFilePath) {
  225. InputStream in = null;
  226. OutputStream out = null;
  227. byte[] buffer = new byte[8 * 1024];
  228. try {
  229. File file = new File(realFilePath);
  230. in = new FileInputStream(file);
  231. out = response.getOutputStream();
  232. // 设置文件MIME类型
  233. response.setContentType("application/octet-stream");
  234. fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
  235. response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  236. response.setHeader("Content-Length", String.valueOf(file.length()));
  237. for (;;) {
  238. int bytes = in.read(buffer);
  239. if (bytes == -1) {
  240. break;
  241. }
  242. out.write(buffer, 0, bytes);
  243. }
  244. } catch (IOException e) {
  245. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  246. } finally {
  247. try {
  248. if (in != null) {
  249. in.close();
  250. }
  251. } catch (IOException e) {
  252. in = null;
  253. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  254. }
  255. try {
  256. if (out != null) {
  257. out.close();
  258. }
  259. } catch (IOException e) {
  260. out = null;
  261. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  262. }
  263. }
  264. }
  265. }