BaseApiController.java 8.0 KB

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