FileUtils.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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) {
  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. return prefix + suffix;
  173. }
  174. public static String getSuffix(String path) {
  175. if (StringUtils.isBlank(path) || path.lastIndexOf(".") < 0) {
  176. return "";
  177. }
  178. return path.substring(path.lastIndexOf("."));
  179. }
  180. /**
  181. * 拼接文件路径
  182. *
  183. * @param root
  184. * @param params
  185. * @return
  186. */
  187. public static String buildFilePath(String... params) {
  188. StringBuilder sb = new StringBuilder();
  189. if (params != null) {
  190. for (String s : params) {
  191. sb.append(SLASH);
  192. sb.append(SLASH_PATTERN.matcher(s).replaceAll(""));
  193. }
  194. }
  195. return sb.toString();
  196. }
  197. /**
  198. * 拼接文件名字
  199. *
  200. * @param root
  201. * @param params
  202. * @return
  203. */
  204. public static String buildFileName(String... params) {
  205. return StringUtils.join(params, UNDERLINE);
  206. }
  207. // year
  208. private static String subStr(String s) {
  209. return "企业" + s.substring(s.lastIndexOf(UNDERLINE) + 1, s.lastIndexOf("."));
  210. }
  211. /**
  212. *
  213. * @param response
  214. * @param fileName
  215. * @param realFilePath
  216. */
  217. public static void downloadFile(HttpServletResponse response, String fileName, String realFilePath) {
  218. InputStream in = null;
  219. OutputStream out = null;
  220. byte[] buffer = new byte[8 * 1024];
  221. try {
  222. File file = new File(realFilePath);
  223. in = new FileInputStream(file);
  224. out = response.getOutputStream();
  225. // 设置文件MIME类型
  226. response.setContentType("application/octet-stream");
  227. fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
  228. response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  229. response.setHeader("Content-Length", String.valueOf(file.length()));
  230. for (;;) {
  231. int bytes = in.read(buffer);
  232. if (bytes == -1) {
  233. break;
  234. }
  235. out.write(buffer, 0, bytes);
  236. }
  237. } catch (IOException e) {
  238. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  239. } finally {
  240. try {
  241. if (in != null) {
  242. in.close();
  243. }
  244. } catch (IOException e) {
  245. in = null;
  246. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  247. }
  248. try {
  249. if (out != null) {
  250. out.close();
  251. }
  252. } catch (IOException e) {
  253. out = null;
  254. LoggerUtils.fmtError(FileUtils.class, e, "IO错误:%s", e.getMessage());
  255. }
  256. }
  257. }
  258. }