FileUtils.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. public class FileUtils {
  16. @Value(value = "${upload.private.path}")
  17. private String uploadPrivatePath = null;
  18. private static final Pattern SLASH_PATTERN = Pattern.compile("^/|/$");
  19. private static final String SLASH = "/";
  20. private static final String UNDERLINE = "_";
  21. /**
  22. * response 输出JSON
  23. *
  24. * @param hresponse
  25. * @param resultMap
  26. * @throws IOException
  27. */
  28. public static void out(HttpServletResponse response, String jsonStr) {
  29. PrintWriter out = null;
  30. try {
  31. response.setHeader("Content-Type", "application/json");
  32. response.setCharacterEncoding("UTF-8");
  33. out = response.getWriter();
  34. out.println(jsonStr);
  35. } catch (Exception e) {
  36. LoggerUtils.fmtError(FileUtils.class, e, "输出数据失败");
  37. } finally {
  38. if (null != out) {
  39. out.flush();
  40. out.close();
  41. }
  42. }
  43. }
  44. /**
  45. * Excel报表导出
  46. *
  47. * @param response
  48. * @param fileName
  49. * @param workbook
  50. */
  51. public static void downloadExcel(HttpServletResponse response, String fileName, HSSFWorkbook workbook) {
  52. String fn = null;
  53. try {
  54. fn = URLEncoder.encode(fileName, "UTF-8");
  55. } catch (UnsupportedEncodingException e1) {
  56. fn = fileName;
  57. }
  58. response.setContentType("application/x-download;charset=UTF-8");
  59. response.setHeader("Content-Disposition", "attachment; filename=\"" + fn + "\";");
  60. OutputStream out = null;
  61. try {
  62. out = response.getOutputStream();
  63. workbook.write(out);
  64. } catch (IOException e) {
  65. LoggerUtils.fmtError(FileUtils.class, e, "输出Excel失败");
  66. } finally {
  67. if (null != out) {
  68. try {
  69. out.flush();
  70. out.close();
  71. } catch (IOException e) {
  72. out = null;
  73. }
  74. }
  75. }
  76. }
  77. /**
  78. * 根据上传文件 拼接fileName
  79. *
  80. * @param mf
  81. * @param isPrivate
  82. * @param sign
  83. * @param path
  84. * @return
  85. */
  86. public static String mosaicFileName(MultipartFile mf, boolean isPrivate, String sign, String path, String uid) {
  87. String suffix = mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf("."));
  88. boolean uniq = false;
  89. if (sign.indexOf("protocol") != -1 || sign.indexOf("achievement") != -1 || sign.indexOf("honor") != -1
  90. || sign.indexOf("proof") != -1 || sign.indexOf("activity_cost_account") != -1
  91. || sign.indexOf("tech_product") != -1 || sign.indexOf("property_ritht") != -1
  92. || sign.indexOf("tech_project") != -1 || sign.indexOf("patent_prory_statement") != -1
  93. || sign.indexOf("patent_writing") != -1 || sign.indexOf("authorization_notice") != -1
  94. || sign.indexOf("patent_certificate") != -1 || sign.indexOf("standard") != -1) {
  95. uniq = true;
  96. }
  97. String fileName = "";
  98. if (isPrivate || sign != "") {
  99. if (uniq) {
  100. fileName = path + uid + "/" + System.nanoTime() + "_" + sign + suffix;
  101. } else {
  102. fileName = path + uid + "/" + sign + suffix;
  103. }
  104. } else {
  105. fileName = path + uid + "/" + System.nanoTime() + suffix;
  106. }
  107. return fileName;
  108. }
  109. /**
  110. * 获取下载文件名称
  111. *
  112. * @param path
  113. * @return
  114. */
  115. public static String getDownloadFileName(String path) {
  116. if (StringUtils.isBlank(path) || path.lastIndexOf(".") < 0) {
  117. return null;
  118. }
  119. String prefix = "附件";
  120. String suffix = path.substring(path.lastIndexOf("."));
  121. if (path.indexOf("patent_prory_statement") != -1) {
  122. prefix = "专利代理委托书";
  123. }
  124. if (path.indexOf("patent_writing") != -1) {
  125. prefix = "专利稿件";
  126. }
  127. if (path.indexOf("authorization_notice") != -1) {
  128. prefix = "授权通知书";
  129. }
  130. if (path.indexOf("patent_certificate") != -1) {
  131. prefix = "专利证书";
  132. }
  133. if (path.indexOf("proof") != -1) {
  134. prefix = "立项证明材料";
  135. }
  136. if (path.indexOf("activity_cost_account") != -1) {
  137. prefix = "研发活动费用台帐";
  138. }
  139. if (path.indexOf("tech_product") != -1) {
  140. prefix = "高新技术产品台帐";
  141. }
  142. if (path.indexOf("property_ritht") != -1) {
  143. prefix = "知识产权证书";
  144. }
  145. if (path.indexOf("roster") != -1) {
  146. prefix = subStr(path) + "花名册";
  147. }
  148. if (path.indexOf("social_security") != -1) {
  149. prefix = subStr(path) + "社保情况";
  150. }
  151. if (path.indexOf("honor") != -1) {
  152. prefix = "荣誉材料证明";
  153. }
  154. if (path.indexOf("achievement") != -1) {
  155. prefix = "科技成果附件";
  156. }
  157. if (path.indexOf("institution") != -1) {
  158. prefix = "制度目录";
  159. }
  160. if (path.indexOf("protocol") != -1) {
  161. prefix = "技术协议";
  162. }
  163. if (path.indexOf("finance") != -1) {
  164. prefix = subStr(path) + "财务报表";
  165. }
  166. if (path.indexOf("ratepay") != -1) {
  167. prefix = subStr(path) + "纳税申报表";
  168. }
  169. if (path.indexOf("tech_project") != -1) {
  170. prefix = "科技项目资料";
  171. }
  172. if (path.indexOf("standard") != -1) {
  173. prefix = "标准制定";
  174. }
  175. if (path.indexOf("manuscript") != -1) {
  176. prefix = "稿件";
  177. }
  178. return prefix + suffix;
  179. }
  180. public static String getSuffix(String path) {
  181. if (StringUtils.isBlank(path) || path.lastIndexOf(".") < 0) {
  182. return "";
  183. }
  184. return path.substring(path.lastIndexOf("."));
  185. }
  186. /**
  187. * 拼接文件路径
  188. *
  189. * @param root
  190. * @param params
  191. * @return
  192. */
  193. public static String buildFilePath(String... params) {
  194. StringBuilder sb = new StringBuilder();
  195. if (params != null) {
  196. for (String s : params) {
  197. sb.append(SLASH);
  198. sb.append(SLASH_PATTERN.matcher(s).replaceAll(""));
  199. }
  200. }
  201. return sb.toString();
  202. }
  203. /**
  204. * 拼接文件名字
  205. *
  206. * @param root
  207. * @param params
  208. * @return
  209. */
  210. public static String buildFileName(String... params) {
  211. return StringUtils.join(params, UNDERLINE);
  212. }
  213. // year
  214. private static String subStr(String s) {
  215. return "企业" + s.substring(s.lastIndexOf(UNDERLINE) + 1, s.lastIndexOf("."));
  216. }
  217. /**
  218. *
  219. * @param response
  220. * @param fileName
  221. * @param realFilePath
  222. */
  223. public static void downloadFile(HttpServletResponse response, String fileName, String realFilePath) {
  224. InputStream in = null;
  225. OutputStream out = null;
  226. byte[] buffer = new byte[8 * 1024];
  227. try {
  228. File file = new File(realFilePath);
  229. in = new FileInputStream(file);
  230. out = response.getOutputStream();
  231. // 设置文件MIME类型
  232. response.setContentType("application/octet-stream");
  233. fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
  234. response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  235. response.setHeader("Content-Length", String.valueOf(file.length()));
  236. for (;;) {
  237. int bytes = in.read(buffer);
  238. if (bytes == -1) {
  239. break;
  240. }
  241. out.write(buffer, 0, bytes);
  242. }
  243. } catch (IOException e) {
  244. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  245. } finally {
  246. try {
  247. if (in != null) {
  248. in.close();
  249. }
  250. } catch (IOException e) {
  251. in = null;
  252. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  253. }
  254. try {
  255. if (out != null) {
  256. out.close();
  257. }
  258. } catch (IOException e) {
  259. out = null;
  260. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  261. }
  262. }
  263. }
  264. }