BaseApiController.java 5.8 KB

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