BaseApiController.java 7.5 KB

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