BaseApiController.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package com.goafanti.common.controller;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import com.goafanti.order.enums.OrderImgEnums;
  10. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import org.springframework.web.multipart.MultipartFile;
  16. import org.springframework.web.multipart.MultipartRequest;
  17. import com.goafanti.common.bo.Result;
  18. import com.goafanti.common.utils.FileUtils;
  19. import com.goafanti.common.utils.LoggerUtils;
  20. import com.goafanti.core.shiro.token.TokenManager;
  21. @RestController
  22. @RequestMapping(value = "/api")
  23. public class BaseApiController extends BaseController {
  24. @Value(value = "${upload.path}")
  25. private String uploadPath = null;
  26. @Value(value = "${upload.private.path}")
  27. private String uploadPrivatePath = null;
  28. /**
  29. *
  30. * @param request
  31. * @return
  32. */
  33. protected List<MultipartFile> getFiles(HttpServletRequest request) {
  34. if (ServletFileUpload.isMultipartContent(request)) {
  35. Iterator<String> itr = ((MultipartRequest) request).getFileNames();
  36. List<MultipartFile> files = new ArrayList<MultipartFile>();
  37. if (itr.hasNext()) {
  38. files.add(((MultipartRequest) request).getFile(itr.next()));
  39. }
  40. return files;
  41. }
  42. return new ArrayList<MultipartFile>();
  43. }
  44. /**
  45. *
  46. * @param res
  47. * @param path
  48. * @param isPrivate
  49. * @param req
  50. * @param sign
  51. * @return
  52. */
  53. protected String handleFile(Result res, String path, boolean isPrivate, HttpServletRequest req, String sign) {
  54. List<MultipartFile> files = getFiles(req);
  55. String fileName = "";
  56. if (!files.isEmpty()) {
  57. try {
  58. MultipartFile mf = files.get(0);
  59. fileName = FileUtils.splicingFileName(mf, isPrivate, sign, path, TokenManager.getUserId());
  60. mf.transferTo(isPrivate ? toPrivateFile(fileName) : toFile(fileName));
  61. LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
  62. } catch (Exception e) {
  63. LoggerUtils.error(getClass(), "文件上传失败", e);
  64. res.getError().add(buildError("", "文件上传失败!"));
  65. }
  66. } else {
  67. res.getError().add(buildError("", "文件为空,上传失败!"));
  68. }
  69. return fileName;
  70. }
  71. protected String uploadFile(Result res, String path, HttpServletRequest req,String id, String sign) {
  72. List<MultipartFile> files = getFiles(req);
  73. String fileName = "";
  74. if (!files.isEmpty()) {
  75. try {
  76. MultipartFile mf = files.get(0);
  77. fileName = FileUtils.orderFileName(mf,id,path,sign);
  78. mf.transferTo(toFile(fileName));
  79. LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
  80. } catch (Exception e) {
  81. LoggerUtils.error(getClass(), "文件上传失败", e);
  82. res.getError().add(buildError("", "文件上传失败!"));
  83. }
  84. } else {
  85. res.getError().add(buildError("", "文件为空,上传失败!"));
  86. }
  87. return fileName;
  88. }
  89. protected Result deleteFile (Result res,String fileName){
  90. if (StringUtils.isBlank(fileName)) {
  91. res.getError().add(buildError("文件路径错误"));
  92. return res;
  93. }
  94. try {
  95. if (!com.goafanti.common.utils.excel.FileUtils.checkAllowDownload(fileName)) {
  96. throw new Exception("文件名称非法,不允许删除。 ");
  97. }
  98. String filePath = uploadPath + "/" + fileName;
  99. com.goafanti.common.utils.excel.FileUtils.deleteFile(filePath);
  100. if (fileName.contains(OrderImgEnums.CONTRACT.getCode())){
  101. }
  102. res.setData(1);
  103. } catch (Exception e) {
  104. LoggerUtils.error(PublicController.class, "删除文件失败", e);
  105. }
  106. return res;
  107. }
  108. /**
  109. *
  110. * @param res
  111. * @param isPrivate
  112. * @param req
  113. * @return
  114. */
  115. protected String handleFile(Result res, boolean isPrivate, HttpServletRequest req, String... fileName) {
  116. List<MultipartFile> files = getFiles(req);
  117. String realFilePath = "";
  118. if (!files.isEmpty()) {
  119. try {
  120. MultipartFile mf = files.get(0);
  121. realFilePath = FileUtils.buildFilePath(fileName) + FileUtils.getSuffix(mf.getOriginalFilename());
  122. File f = isPrivate ? toPrivateFile(realFilePath) : toFile(realFilePath);
  123. mf.transferTo(f);
  124. LoggerUtils.debug(getClass(), realFilePath + " 文件上传成功");
  125. } catch (Exception e) {
  126. LoggerUtils.error(getClass(), "文件上传失败", e);
  127. res.getError().add(buildError("", "文件上传失败!"));
  128. }
  129. } else {
  130. res.getError().add(buildError("", "文件上传失败!"));
  131. }
  132. return realFilePath;
  133. }
  134. /**
  135. *
  136. * @param res
  137. * @param fileName
  138. * @param filePath
  139. */
  140. protected void downloadFile(HttpServletResponse res, String fileName, String filePath) {
  141. FileUtils.downloadFile(res, fileName, uploadPath+ filePath);
  142. }
  143. protected void downloadUnPrivateFile(HttpServletResponse res, String fileName, String filePath) {
  144. FileUtils.downloadFile(res, fileName, uploadPrivatePath + filePath);
  145. }
  146. private File buildFile(String filePath) {
  147. File toFile = new File(filePath);
  148. toFile.mkdirs();
  149. return toFile;
  150. }
  151. /**
  152. *
  153. * @param fileName
  154. * @return
  155. */
  156. protected File toFile(String fileName) {
  157. return buildFile(uploadPath + fileName);
  158. }
  159. /**
  160. *
  161. * @param fileName
  162. * @return
  163. */
  164. protected File toPrivateFile(String fileName) {
  165. return buildFile(uploadPrivatePath + fileName);
  166. }
  167. /**
  168. *
  169. * @param fileName
  170. * @return
  171. */
  172. protected File toAdminPrivateFile(String fileName) {
  173. return buildFile(uploadPrivatePath + FileUtils.buildFilePath("/admin/", fileName));
  174. }
  175. protected Integer getPageNo(String pageNo) {
  176. if (StringUtils.isNumeric(pageNo)) {
  177. Integer pn = Integer.parseInt(pageNo);
  178. return pn < 1 ? 1 : pn;
  179. }
  180. return 1;
  181. }
  182. protected Integer getPageSize(String pageSize) {
  183. if (StringUtils.isNumeric(pageSize)) {
  184. Integer ps = Integer.parseInt(pageSize);
  185. return ps > 50 ? 50 : (ps < 10 ? 10 : ps);
  186. }
  187. return 10;
  188. }
  189. protected String handleFiles(Result res, String path, boolean isPrivate, HttpServletRequest req, String sign,
  190. String uid) {
  191. List<MultipartFile> files = getFiles(req);
  192. MultipartFile mf = files.get(0);
  193. String fileName = FileUtils.mosaicFileName(mf, isPrivate, sign, path,
  194. StringUtils.isBlank(uid) ? TokenManager.getUserId() : uid);
  195. if (!files.isEmpty()) {
  196. try {
  197. if (isPrivate) {
  198. mf.transferTo(toPrivateFile(fileName));
  199. } else {
  200. mf.transferTo(toFile(fileName));
  201. }
  202. LoggerUtils.debug(getClass(), fileName + " 文件上传成功");
  203. } catch (IllegalStateException | IOException e) {
  204. LoggerUtils.error(getClass(), "文件上传失败", e);
  205. res.getError().add(buildError("", "文件上传失败!"));
  206. return "";
  207. }
  208. } else {
  209. res.getError().add(buildError("", "文件上传失败!"));
  210. return "";
  211. }
  212. return fileName;
  213. }
  214. protected Integer handlePageNo(String pageNo) {
  215. try {
  216. Integer n = Integer.valueOf(pageNo);
  217. return n < 1 ? 1 : n;
  218. } catch (Exception e) {
  219. return 1;
  220. }
  221. }
  222. protected Integer handlePageSize(String pageSize) {
  223. try {
  224. Integer s = Integer.valueOf(pageSize);
  225. return s < 5 ? 5 : s > 100 ? 100 : s;
  226. } catch (Exception e) {
  227. return 10;
  228. }
  229. }
  230. protected Result res() {
  231. return new Result();
  232. }
  233. }