FileManageAction.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.minpay.shouhuo.system;
  2. import com.min.util.CommonUtil;
  3. import com.minpay.common.exception.BusinessCodeException;
  4. import com.minpay.common.service.IPublicService;
  5. import com.startup.minpay.frame.business.IMINAction;
  6. import com.startup.minpay.frame.business.MINHttpServletRequestContext;
  7. import com.startup.minpay.frame.business.res.MINActionResult;
  8. import com.startup.minpay.frame.exception.MINBusinessException;
  9. import com.startup.minpay.frame.service.base.Service;
  10. import com.startup.minpay.frame.target.MINAction;
  11. import com.startup.minpay.frame.target.MINComponent;
  12. import com.startup.minpay.frame.target.MINParam;
  13. import com.startup.minpay.util.Log;
  14. import org.apache.commons.fileupload.FileItem;
  15. import org.apache.commons.lang.StringUtils;
  16. import org.json.JSONObject;
  17. import org.springframework.web.multipart.MultipartFile;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.io.File;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * 文件上传公共接口
  26. *
  27. * @author ZHANGZZ
  28. *
  29. */
  30. @MINComponent
  31. public class FileManageAction implements IMINAction {
  32. private final static String UPLOAD_BYBYTES = "uploadByBytes";
  33. private final static String UPLOAD_FILE = "uploadFile";
  34. /**
  35. *
  36. * @param MINRequest
  37. * @param fileType 文件类型00图片01文档02视频
  38. * @param response
  39. * @param size 文件大小
  40. * @param proportion 图片宽高比区间 格式最小区间,最大区间 例如 1.50,1.80
  41. * @return
  42. * @throws MINBusinessException
  43. */
  44. @MINAction(value = UPLOAD_FILE, session = false)
  45. public MINActionResult uploadFile(MINHttpServletRequestContext MINRequest,
  46. @MINParam(key = "fileFiledId") String fileFiledId, @MINParam(key = "fileType") String fileType,
  47. @MINParam(key = "size") int size, @MINParam(key = "proportion") String proportion,
  48. HttpServletResponse response) throws MINBusinessException {
  49. MINActionResult res = new MINActionResult();
  50. if (StringUtils.isEmpty(fileFiledId)) {
  51. fileFiledId = "file";
  52. }
  53. FileItem file = MINRequest.getFile(fileFiledId);
  54. // 判断文件大小上传抛出异常
  55. Log.info("文件大小:" + String.valueOf((file.getSize() / 1024D / 1024D)) + "M");
  56. if (size > 0 && (1024L * 1024L * size) < file.getSize()) {
  57. throw new MINBusinessException("请上传小于" + String.valueOf(size) + "M的文件");
  58. }
  59. if (size < 0) {
  60. throw new MINBusinessException("size参数异常,size不能小于0");
  61. }
  62. Map<String, Object> map = Service.lookup(IPublicService.class).uploadFile(file, fileType, 0, true);
  63. if ("00".equals(fileType) && !CommonUtil.isEmpty(proportion)) {
  64. double minRatio = -1;
  65. double maxRatio = -1;
  66. String[] proportions = null;
  67. if (proportion != null && !proportion.isEmpty()) {
  68. // 截取参数 图片的宽高比
  69. proportions = proportion.split(",");
  70. minRatio = Double.parseDouble(proportions[0].trim());
  71. maxRatio = Double.parseDouble(proportions[1].trim());
  72. }
  73. // 获取宽高
  74. Double width = (Double) map.get("width");
  75. Double height = (Double) map.get("height");
  76. // 判断宽高比是否符合规定
  77. Double ratio = width / height;
  78. Log.info("上传图片宽高比:" + String.valueOf(ratio));
  79. if (ratio < minRatio || ratio > maxRatio) {
  80. throw new MINBusinessException(
  81. "请上传宽高比" + proportions[0].trim() + "到" + proportions[1].trim() + "之间的图片");
  82. }
  83. }
  84. String url = (String) map.get("url");
  85. JSONObject js = new JSONObject();
  86. js.put("src", url);// 获取图片路径
  87. res.set("code", 0);
  88. res.set("msg", "");
  89. res.set("data", js);
  90. return res;
  91. }
  92. }