FileUtils.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package com.goafanti.common.utils;
  2. import com.goafanti.common.enums.AttachmentType;
  3. import com.goafanti.common.error.BusinessException;
  4. import com.mchange.v1.io.InputStreamUtils;
  5. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.*;
  10. import java.net.URLEncoder;
  11. import java.util.regex.Pattern;
  12. public class FileUtils {
  13. @Value(value = "${upload.private.path}")
  14. private String uploadPrivatePath = null;
  15. private static final Pattern SLASH_PATTERN = Pattern.compile("^/|/$");
  16. private static final String SLASH = "/";
  17. private static final String UNDERLINE = "_";
  18. /**
  19. * response 输出JSON
  20. *
  21. * @throws IOException
  22. */
  23. public static void out(HttpServletResponse response, String jsonStr) {
  24. PrintWriter out = null;
  25. try {
  26. response.setHeader("Content-Type", "application/json");
  27. response.setCharacterEncoding("UTF-8");
  28. out = response.getWriter();
  29. out.println(jsonStr);
  30. } catch (Exception e) {
  31. LoggerUtils.fmtError(FileUtils.class, e, "输出数据失败");
  32. } finally {
  33. if (null != out) {
  34. out.flush();
  35. out.close();
  36. }
  37. }
  38. }
  39. /**
  40. * Excel报表导出
  41. *
  42. * @param response
  43. * @param fileName
  44. * @param workbook
  45. */
  46. public static void downloadExcel(HttpServletResponse response, String fileName, HSSFWorkbook workbook) {
  47. String fn = null;
  48. try {
  49. fn = URLEncoder.encode(fileName, "UTF-8");
  50. } catch (UnsupportedEncodingException e1) {
  51. fn = fileName;
  52. }
  53. response.setContentType("application/x-download;charset=UTF-8");
  54. response.setHeader("Content-Disposition", "attachment; filename=\"" + fn + "\";");
  55. OutputStream out = null;
  56. try {
  57. out = response.getOutputStream();
  58. workbook.write(out);
  59. } catch (IOException e) {
  60. LoggerUtils.fmtError(FileUtils.class, e, "输出Excel失败");
  61. } finally {
  62. if (null != out) {
  63. try {
  64. out.flush();
  65. out.close();
  66. } catch (IOException e) {
  67. out = null;
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. * 根据上传文件 拼接fileName
  74. *
  75. * @param mf
  76. * @param isPrivate
  77. * @param sign
  78. * @param path
  79. * @return
  80. */
  81. public static String mosaicFileName(MultipartFile mf, boolean isPrivate, String sign, String path, String uid) {
  82. String suffix = mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf("."));
  83. boolean uniq = false;
  84. if (sign.indexOf("protocol") != -1 || sign.indexOf("achievement") != -1 || sign.indexOf("honor") != -1
  85. || sign.indexOf("proof") != -1 || sign.indexOf("activity_cost_account") != -1
  86. || sign.indexOf("tech_product") != -1 || sign.indexOf("property_ritht") != -1
  87. || sign.indexOf("tech_project") != -1 || sign.indexOf("patent_prory_statement") != -1
  88. || sign.indexOf("patent_writing") != -1 || sign.indexOf("authorization_notice") != -1
  89. || sign.indexOf("patent_certificate") != -1 || sign.indexOf("standard") != -1
  90. || sign.indexOf("demand_picture") != -1 || sign.indexOf("demand_text_file") != -1
  91. || sign.indexOf("achievement_technical_picture") != -1|| sign.indexOf("order_file") != -1
  92. || sign.indexOf("achievement_maturity_picture") != -1 || sign.indexOf("demand_order_file") != -1
  93. || sign.indexOf("lecture_picture") != -1|| sign.indexOf("cover_picture") != -1
  94. || sign.indexOf("news") != -1||sign.indexOf("customer_sys_file") != -1 || sign.indexOf("order_refund_file") != -1
  95. ||sign.indexOf("business_project_max_picture") != -1||sign.indexOf("business_project_min_picture") != -1
  96. ||sign.indexOf("varieties_picture") != -1 || sign.indexOf("organization_application") != -1
  97. ||sign.indexOf("order_task_file") != -1||sign.indexOf("patent_certificate") != -1
  98. ||sign.indexOf("order_invoice_file") != -1 ||sign.indexOf("order_outsource") != -1|| sign.indexOf("dun_log_attachment") != -1||
  99. sign.indexOf("legal_log_attachment") != -1){
  100. uniq = true;
  101. }
  102. String fileName = "";
  103. if (isPrivate || StringUtils.isNotBlank(sign)) {
  104. if (uniq) {
  105. fileName = path + uid + "/" + System.nanoTime() + "_" + sign + suffix;
  106. } else {
  107. fileName = path + uid + "/" + sign + suffix;
  108. }
  109. } else {
  110. fileName = path + uid + "/" + System.nanoTime() + suffix;
  111. }
  112. return fileName;
  113. }
  114. /**
  115. * 根据上传文件 拼接fileName
  116. *
  117. * @param mf
  118. * @param isPrivate
  119. * @param sign
  120. * @param path
  121. * @return
  122. */
  123. public static String splicingFileName(MultipartFile mf, boolean isPrivate, String sign, String path, String uid) {
  124. String suffix = mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf("."));
  125. boolean uniq = false;
  126. if (sign!= null ){
  127. uniq = true;
  128. }
  129. String fileName = "";
  130. if (isPrivate || StringUtils.isNotBlank(sign)) {
  131. if (uniq) {
  132. fileName = path + "/" + System.nanoTime() + "_" + sign + suffix;
  133. } else {
  134. fileName = path + "/" + sign + suffix;
  135. }
  136. } else {
  137. fileName = path + "/" + System.nanoTime() + suffix;
  138. }
  139. return fileName;
  140. }
  141. /**
  142. * 根据上传文件 拼接fileName
  143. *
  144. * @param mf
  145. * @param sign
  146. * @param path
  147. * @return
  148. */
  149. public static String orderFileName(MultipartFile mf, String id, String path,String sign) {
  150. String suffix = mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf("."));
  151. String fileName = path + id + "/" + System.nanoTime()+"_"+sign + suffix;
  152. return fileName;
  153. }
  154. public static String orderFileName(MultipartFile mf, String path,String sign) {
  155. String suffix = mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf("."));
  156. String fileName = path + sign + "/" + System.nanoTime()+"_"+sign + suffix;
  157. return fileName;
  158. }
  159. /**
  160. * 获取下载文件名称
  161. *
  162. * @param path
  163. * @return
  164. */
  165. public static String getDownloadFileName(String path) {
  166. if (StringUtils.isBlank(path) || path.lastIndexOf(".") < 0) {
  167. return null;
  168. }
  169. String prefix = "附件";
  170. String suffix = path.substring(path.lastIndexOf("."));
  171. if (path.indexOf(AttachmentType.PATENT_PRORY_STATEMENT.getCode()) != -1) {
  172. prefix = "专利代理委托书";
  173. } else if (path.indexOf(AttachmentType.PATENT_WRITING.getCode()) != -1) {
  174. prefix = "专利稿件";
  175. } else if (path.indexOf(AttachmentType.AUTHORIZATION_NOTICE.getCode()) != -1) {
  176. prefix = "授权通知书";
  177. } else if (path.indexOf(AttachmentType.PATENT_CERTIFICATE.getCode()) != -1) {
  178. prefix = "专利证书";
  179. } else if (path.indexOf(AttachmentType.PROOF.getCode()) != -1) {
  180. prefix = "立项证明材料";
  181. } else if (path.indexOf(AttachmentType.ACTIVITY_COST_ACCOUNT.getCode()) != -1) {
  182. prefix = "研发活动费用台帐";
  183. } else if (path.indexOf(AttachmentType.TECH_PRODUCT.getCode()) != -1) {
  184. prefix = "高新技术产品台帐";
  185. } else if (path.indexOf(AttachmentType.PROPERTY_RIGHT.getCode()) != -1) {
  186. prefix = "知识产权证书";
  187. } else if (path.indexOf(AttachmentType.ROSTER.getCode()) != -1) {
  188. prefix = subStr(path) + "花名册";
  189. } else if (path.indexOf(AttachmentType.SOCIAL_SECURITY.getCode()) != -1) {
  190. prefix = subStr(path) + "社保情况";
  191. } else if (path.indexOf(AttachmentType.HONOR.getCode()) != -1) {
  192. prefix = "荣誉材料证明";
  193. } else if (path.indexOf(AttachmentType.ACHIEVEMENT.getCode()) != -1) {
  194. prefix = "科技成果附件";
  195. } else if (path.indexOf(AttachmentType.INSTITUTION.getCode()) != -1) {
  196. prefix = "制度目录";
  197. } else if (path.indexOf(AttachmentType.PROTOCOL.getCode()) != -1) {
  198. prefix = "技术协议";
  199. } else if (path.indexOf(AttachmentType.FINANCE.getCode()) != -1) {
  200. prefix = subStr(path) + "财务报表";
  201. } else if (path.indexOf(AttachmentType.RATEPAY.getCode()) != -1) {
  202. prefix = subStr(path) + "纳税申报表";
  203. } else if (path.indexOf(AttachmentType.TECH_PROJECT.getCode()) != -1) {
  204. prefix = "科技项目资料";
  205. } else if (path.indexOf(AttachmentType.STANDARD.getCode()) != -1) {
  206. prefix = "标准制定";
  207. } else if (path.indexOf(AttachmentType.MANUSCRIPT.getCode()) != -1) {
  208. prefix = "稿件";
  209. } else if (path.indexOf(AttachmentType.DEMAND_TEXT_FILE.getCode()) != -1) {
  210. prefix = "科技需求文本文件资料";
  211. } else if (path.indexOf(AttachmentType.ACHIEVEMENT_MATURITY_TEXT_FILE.getCode()) != -1) {
  212. prefix = "科技成果成熟度资料文本文件";
  213. } else if (path.indexOf(AttachmentType.ACHIEVEMENT_TECH_PLAN.getCode()) != -1) {
  214. prefix = "科技成果技术方案文件";
  215. } else if (path.indexOf(AttachmentType.ACHIEVEMENT_BUSINESS_PLAN.getCode()) != -1) {
  216. prefix = "科技成果商业计划书";
  217. }
  218. return prefix + suffix;
  219. }
  220. public static String getSuffix(String path) {
  221. if (StringUtils.isBlank(path) || path.lastIndexOf(".") < 0) {
  222. return "";
  223. }
  224. return path.substring(path.lastIndexOf("."));
  225. }
  226. /**
  227. * 拼接文件路径
  228. *
  229. * @param params
  230. * @return
  231. */
  232. public static String buildFilePath(String... params) {
  233. StringBuilder sb = new StringBuilder();
  234. if (params != null) {
  235. for (String s : params) {
  236. sb.append(SLASH);
  237. sb.append(SLASH_PATTERN.matcher(s).replaceAll(""));
  238. }
  239. }
  240. return sb.toString();
  241. }
  242. /**
  243. * 拼接文件名字
  244. *
  245. * @param params
  246. * @return
  247. */
  248. public static String buildFileName(String... params) {
  249. return StringUtils.join(params, UNDERLINE);
  250. }
  251. // year
  252. private static String subStr(String s) {
  253. return "企业" + s.substring(s.lastIndexOf(UNDERLINE) + 1, s.lastIndexOf("."));
  254. }
  255. public static boolean deleteFile(String realFilePath) {
  256. return new File(realFilePath).delete();
  257. }
  258. /**
  259. *
  260. * @param response
  261. * @param fileName
  262. * @param realFilePath
  263. */
  264. public static void downloadFile(HttpServletResponse response, String fileName, String realFilePath) {
  265. InputStream in = null;
  266. OutputStream out = null;
  267. byte[] buffer = new byte[8 * 1024];
  268. try {
  269. if(!com.goafanti.common.utils.excel.FileUtils.checkAllowDownload(realFilePath)){
  270. throw new BusinessException("文件名称非法,不允许下载。 ");
  271. }
  272. File file = new File(realFilePath);
  273. in = new FileInputStream(file);
  274. out = response.getOutputStream();
  275. // 设置文件MIME类型
  276. response.setContentType("application/octet-stream");
  277. fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
  278. response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  279. response.setHeader("Content-Length", String.valueOf(file.length()));
  280. for (;;) {
  281. int bytes = in.read(buffer);
  282. if (bytes == -1) {
  283. break;
  284. }
  285. out.write(buffer, 0, bytes);
  286. }
  287. }catch (BusinessException e) {
  288. LoggerUtils.fmtError(FileUtils.class, e, "名称错误:%s", e.getMessage());
  289. } catch (IOException e) {
  290. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  291. } finally {
  292. try {
  293. if (in != null) {
  294. in.close();
  295. }
  296. } catch (IOException e) {
  297. in = null;
  298. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  299. }
  300. try {
  301. if (out != null) {
  302. out.close();
  303. }
  304. } catch (IOException e) {
  305. out = null;
  306. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  307. }
  308. }
  309. }
  310. public static byte[] getBytes(File file) throws IOException {
  311. BufferedInputStream var1 = new BufferedInputStream(new FileInputStream(file));
  312. byte[] var2;
  313. try {
  314. var2 = InputStreamUtils.getBytes(var1);
  315. } finally {
  316. InputStreamUtils.attemptClose(var1);
  317. }
  318. return var2;
  319. }
  320. }