FileUtils.java 9.1 KB

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