DownloadController.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * 版权所有:厦门市巨龙信息科技有限公司
  3. * Copyright 2011 Xiamen Dragon Info. Tech. Co. Ltd.
  4. * All right reserved.
  5. * ====================================================
  6. * 文件名称: DownloadController.java
  7. * 修订记录:
  8. * No 日期 作者(操作:具体内容)
  9. * 1. 2018-11-16 黄华源(创建:创建文件并实现模版下载)
  10. * ====================================================
  11. * 类描述:
  12. */
  13. package com.dragoninfo.dcuc.authweb.restcontroller.download;
  14. import com.dragonsoft.duceap.commons.util.string.StringUtils;
  15. import io.swagger.annotations.Api;
  16. import io.swagger.annotations.ApiImplicitParam;
  17. import io.swagger.annotations.ApiOperation;
  18. import org.apache.commons.io.IOUtils;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;
  21. import org.springframework.boot.system.ApplicationHome;
  22. import org.springframework.core.io.ClassPathResource;
  23. import org.springframework.core.io.Resource;
  24. import org.springframework.web.bind.annotation.GetMapping;
  25. import org.springframework.web.bind.annotation.RequestMapping;
  26. import org.springframework.web.bind.annotation.RequestParam;
  27. import org.springframework.web.bind.annotation.RestController;
  28. import javax.servlet.http.HttpServletResponse;
  29. import java.io.*;
  30. import java.net.URLEncoder;
  31. /**
  32. * <pre><b><font color="blue">DownloadController</font></b></pre>
  33. * <p/>
  34. * <pre><b>&nbsp;--描述说明--</b></pre>
  35. * <pre>
  36. * 提供警员模版、辅警模版、施工人员模版下载功能,采用io流实现。
  37. * </pre>
  38. * <pre>
  39. * <b>--样例--</b>
  40. * 前端发起请求:
  41. * http://localhost:port/context/downloadTemplate/downPolice/{type}.action
  42. * </pre>
  43. * JDK版本:JDK1.6
  44. *
  45. * @author <b>黄华源</b>
  46. */
  47. @Api(tags = {"附件下载接口"})
  48. @RestController
  49. @RequestMapping(value = "/downloadTemplate/")
  50. public class DownloadController {
  51. public final String POLICE_TEMPLATE_RLPATH = "/template/PoliceTemplate.xls";
  52. public final String AUXILIARY_TEMPLATE_RLPATH = "/template/auxiliarypoliceTemplate.xls";
  53. public final String EXTERNALPERSONNEL_TEMPLATE_RLPATH = "/template/externalpersonnelTemplate.xls";
  54. public final String MANAGER_TEMPLATE_RLPATH = "/template/managerTemplate.xls";
  55. public final String GOVERNMENTPERSON_TEMPLATE_RLPATH = "/template/governmentpersonnelTemplate.xls";
  56. public final String ORG_TEMPLATE_RLPATH = "/template/OrgTemplate.xls";
  57. public final String USER_TEMPLATE_RLPATH = "/template/UserTemplate.xls";
  58. /**
  59. * 客体_服务资源模板
  60. */
  61. public final String SERVICE_RESOURCE_TEMPLATE_RLPATH = "/template/serviceResourceTemplate.xls";
  62. /**
  63. * 客体_功能资源模板
  64. */
  65. public final String APP_FUN_TEMPLATE_RLPATH = "/template/appFunTemplate.xls";
  66. /**
  67. * 主客体_应用资源模板
  68. */
  69. public final String APPLY_TEMPLATE_RLPATH = "/template/applyTemplate.xls";
  70. /**
  71. * 主客体_数据分级模板
  72. */
  73. public final String DATA_LEVEL_TEMPLATE_RLPATH = "/template/dataLevelTemplate.xls";
  74. /**
  75. * 主客体_数据安全级别模板
  76. */
  77. public final String DATA_SEC_TEMPLATE_RLPATH = "/template/dataSecTemplate.xls";
  78. private Logger logger = LoggerFactory.getLogger(DownloadController.class);
  79. @ApiOperation(value = "获取xls附件")
  80. @ApiImplicitParam(name = "type", value = "附件类型:police 警员,auxiliaryPolice 辅警, " +
  81. "externalPersonnel 外部人员, manager 管理员, govUser 政府人员, serviceResource 服务资源,appFun 功能资源, apply 应用资源," +
  82. "org 机构, user 人员 dataLevel 数据分级 dataSec 数据安全级别",
  83. required = true)
  84. @GetMapping(value = "/downTemplate", produces = "application/octet-stream")
  85. public void downTemplate(@RequestParam("type") String type, HttpServletResponse response) {
  86. ApplicationHome home = new ApplicationHome(getClass());
  87. File sysfile = home.getSource();
  88. String jarPath = sysfile.getPath();//classes路径
  89. if (StringUtils.equals(type, "police")) {
  90. response = down(response, POLICE_TEMPLATE_RLPATH);
  91. } else if (StringUtils.equals(type, "auxiliaryPolice")) {
  92. response = down(response, AUXILIARY_TEMPLATE_RLPATH);
  93. } else if (StringUtils.equals(type, "externalPersonnel")) {
  94. response = down(response, EXTERNALPERSONNEL_TEMPLATE_RLPATH);
  95. } else if (StringUtils.equals(type, "manager")) {
  96. response = down(response,MANAGER_TEMPLATE_RLPATH);
  97. } else if (StringUtils.equals(type, "govUser")) {
  98. response = down(response, GOVERNMENTPERSON_TEMPLATE_RLPATH);
  99. } else if (StringUtils.equals(type, "org")) {
  100. response = down(response, ORG_TEMPLATE_RLPATH);
  101. } else if (StringUtils.equals(type, "user")) {
  102. response = down(response, USER_TEMPLATE_RLPATH);
  103. }else if (StringUtils.equals(type, "serviceResource")){
  104. //授权主客体管理_服务资源模板
  105. response = down(response, SERVICE_RESOURCE_TEMPLATE_RLPATH);
  106. }else if (StringUtils.equals(type, "appFun")){
  107. //授权主客体管理_功能资源模板
  108. response = down(response, APP_FUN_TEMPLATE_RLPATH);
  109. }else if (StringUtils.equals(type, "apply")){
  110. //授权主客体管理_应用资源模板
  111. response = down(response, APPLY_TEMPLATE_RLPATH);
  112. }else if (StringUtils.equals(type, "dataLevel")){
  113. //授权主客体管理_应用资源模板
  114. response = down(response, DATA_LEVEL_TEMPLATE_RLPATH);
  115. }else if (StringUtils.equals(type, "dataSec")){
  116. //授权主客体管理_应用资源模板
  117. response = down(response, DATA_SEC_TEMPLATE_RLPATH);
  118. }
  119. }
  120. private HttpServletResponse down(HttpServletResponse response, String path) {
  121. InputStream fis = null;
  122. OutputStream out = null;
  123. try {
  124. // 以流的形式下载文件。
  125. Resource resource = new ClassPathResource(path);
  126. fis = resource.getInputStream();
  127. // 清空response
  128. response.reset();
  129. // 设置response的Header
  130. String fileName = "";
  131. if ("PoliceTemplate.xls".equals( resource.getFilename())) {
  132. fileName = "警员模版.xls";
  133. } else if ("auxiliarypoliceTemplate.xls".equals( resource.getFilename())) {
  134. fileName = "辅警模版.xls";
  135. } else if ("externalpersonnelTemplate.xls".equals( resource.getFilename())) {
  136. fileName = "施工人员模版.xls";
  137. } else if ("managerTemplate.xls".equals( resource.getFilename())) {
  138. fileName = "管理员模板.xls";
  139. } else if ("governmentpersonnelTemplate.xls".equals( resource.getFilename())) {
  140. fileName = "政府人员模板.xls";
  141. } else if ("OrgTemplate.xls".equals( resource.getFilename())) {
  142. fileName = "机构信息模板.xls";
  143. } else if ("UserTemplate.xls".equals( resource.getFilename())) {
  144. fileName = "人员信息模板.xls";
  145. }else if ("serviceResourceTemplate.xls".equals( resource.getFilename())) {
  146. fileName = "客体_服务资源导入.xls";
  147. }else if ("appFunTemplate.xls".equals( resource.getFilename())) {
  148. fileName = "客体_功能资源导入.xls";
  149. }else if ("applyTemplate.xls".equals( resource.getFilename())) {
  150. fileName = "主客体_应用资源导入.xls";
  151. }else if ("dataLevelTemplate.xls".equals( resource.getFilename())) {
  152. fileName = "主客体_数据分级导入.xls";
  153. }else if ("dataSecTemplate.xls".equals( resource.getFilename())) {
  154. fileName = "主客体_数据安全级别导入.xls";
  155. }
  156. response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  157. response.addHeader("Content-Length", "" + resource.contentLength());
  158. out = new BufferedOutputStream(response.getOutputStream());
  159. response.setContentType("application/octet-stream");
  160. IOUtils.copy(fis,out);
  161. } catch (IOException ex) {
  162. ex.printStackTrace();
  163. } finally {
  164. IOUtils.closeQuietly(fis, out);
  165. }
  166. return response;
  167. }
  168. }