FileUtils.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 javax.servlet.http.HttpServletResponse;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import com.goafanti.common.bo.Result;
  15. public class FileUtils {
  16. @Value(value = "${upload.private.path}")
  17. private String uploadPrivatePath = null;
  18. /**
  19. * response 输出JSON
  20. *
  21. * @param hresponse
  22. * @param resultMap
  23. * @throws IOException
  24. */
  25. public void out(HttpServletResponse response, String jsonStr) {
  26. PrintWriter out = null;
  27. try {
  28. response.setHeader("Content-Type", "application/json");
  29. response.setCharacterEncoding("UTF-8");
  30. out = response.getWriter();
  31. out.println(jsonStr);
  32. } catch (Exception e) {
  33. LoggerUtils.fmtError(getClass(), e, "输出数据失败");
  34. } finally {
  35. if (null != out) {
  36. out.flush();
  37. out.close();
  38. }
  39. }
  40. }
  41. /**
  42. * Excel报表导出
  43. *
  44. * @param response
  45. * @param fileName
  46. * @param workbook
  47. */
  48. public void downloadExcel(HttpServletResponse response, String fileName, HSSFWorkbook workbook) {
  49. String fn = null;
  50. try {
  51. fn = URLEncoder.encode(fileName, "UTF-8");
  52. } catch (UnsupportedEncodingException e1) {
  53. fn = fileName;
  54. }
  55. response.setContentType("application/x-download;charset=UTF-8");
  56. response.setHeader("Content-Disposition", "attachment; filename=\"" + fn + "\";");
  57. OutputStream out = null;
  58. try {
  59. out = response.getOutputStream();
  60. workbook.write(out);
  61. } catch (IOException e) {
  62. LoggerUtils.fmtError(getClass(), e, "输出Excel失败");
  63. } finally {
  64. if (null != out) {
  65. try {
  66. out.flush();
  67. out.close();
  68. } catch (IOException e) {
  69. out = null;
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. * 根据上传文件 拼接fileName
  76. *
  77. * @param mf
  78. * @param isPrivate
  79. * @param sign
  80. * @param path
  81. * @return
  82. */
  83. public static String mosaicFileName(MultipartFile mf, boolean isPrivate, String sign, String path, String uid) {
  84. String suffix = mf.getOriginalFilename().substring(mf.getOriginalFilename().lastIndexOf("."));
  85. boolean uniq = false;
  86. if (sign.indexOf("protocol") != -1 || sign.indexOf("achievement") != -1 || sign.indexOf("honor") != -1){
  87. uniq = true;
  88. }
  89. String fileName = "";
  90. if (isPrivate || sign != "") {
  91. if (uniq){
  92. fileName = path + uid + "/" + System.nanoTime()+ "_" + sign + suffix;
  93. } else {
  94. fileName = path + uid + "/" + sign + suffix;
  95. }
  96. } else {
  97. fileName = path + System.nanoTime() + suffix;
  98. }
  99. return fileName;
  100. }
  101. /**
  102. * 获取下载文件名称
  103. *
  104. * @param path
  105. * @return
  106. */
  107. public static String getDownloadFileName(String path) {
  108. if (StringUtils.isBlank(path)) {
  109. return null;
  110. }
  111. String fileName = "";
  112. String prefix = "";
  113. String suffix = path.substring(path.lastIndexOf("."));
  114. if (path.indexOf("patent_prory_statement") != -1) {
  115. prefix = "专利代理委托书";
  116. }
  117. if (path.indexOf("patent_writing") != -1) {
  118. prefix = "专利稿件";
  119. }
  120. if (path.indexOf("authorization_notice") != -1) {
  121. prefix = "授权通知书";
  122. }
  123. if (path.indexOf("patent_certificate") != -1) {
  124. prefix = "专利证书";
  125. }
  126. if (path.indexOf("roster") != -1) {
  127. prefix = subStr(path) + "花名册";
  128. }
  129. if (path.indexOf("social_security") != -1) {
  130. prefix = subStr(path) + "社保情况";
  131. }
  132. if (path.indexOf("honor") != -1) {
  133. prefix = "企业荣誉及材料证明";
  134. }
  135. if (path.indexOf("achievement") != -1) {
  136. prefix = "科技成果";
  137. }
  138. if (path.indexOf("institution") != -1) {
  139. prefix = "技术中心制度";
  140. }
  141. if (path.indexOf("protocol") != -1) {
  142. prefix = "技术协议";
  143. }
  144. if (path.indexOf("finance") != -1) {
  145. prefix = subStr(path) + "财务报表";
  146. }
  147. if (path.indexOf("ratepay") != -1) {
  148. prefix = subStr(path) + "纳税申报表";
  149. }
  150. fileName = prefix + suffix;
  151. return fileName;
  152. }
  153. /**
  154. * 下载文件
  155. * @param res
  156. * @param fileName
  157. * @param response
  158. * @param path
  159. * @param request
  160. * @return
  161. */
  162. public Result downloadFile(Result res, String fileName, HttpServletResponse response, String path) {
  163. String fileSaveRootPath = uploadPrivatePath + path;
  164. InputStream in = null;
  165. OutputStream out = null;
  166. byte[] buffer = new byte[8 * 1024];
  167. try {
  168. File file = new File(fileSaveRootPath);
  169. in = new FileInputStream(file);
  170. out = response.getOutputStream();
  171. // 设置文件MIME类型
  172. response.setContentType("application/octet-stream");
  173. response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  174. for (;;) {
  175. int bytes = in.read(buffer);
  176. if (bytes == -1) {
  177. break;
  178. }
  179. out.write(buffer, 0, bytes);
  180. }
  181. } catch (IOException e) {
  182. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  183. } finally {
  184. try {
  185. in.close();
  186. } catch (IOException e) {
  187. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  188. }
  189. try {
  190. out.close();
  191. } catch (IOException e) {
  192. LoggerUtils.fmtError(getClass(), e, "IO错误:%s", e.getMessage());
  193. }
  194. }
  195. return res;
  196. }
  197. // year
  198. private static String subStr(String s) {
  199. return "企业" + s.substring(s.lastIndexOf("_") + 1, s.lastIndexOf("."));
  200. }
  201. }