AttachmngsController.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package com.ruoyi.web.controller.wx;
  2. import com.ruoyi.common.MapUtils;
  3. import com.ruoyi.common.core.domain.AjaxResult;
  4. import com.ruoyi.common.core.domain.entity.SysUser;
  5. import com.ruoyi.common.utils.AddWatermarkUtil;
  6. import com.ruoyi.common.utils.SecurityUtils;
  7. import com.ruoyi.system.domain.AttachMngs;
  8. import com.ruoyi.system.service.IAttachMngsService;
  9. import net.coobird.thumbnailator.Thumbnails;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.http.MediaType;
  12. import org.springframework.web.bind.annotation.*;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import javax.servlet.http.HttpServletRequest;
  15. import java.awt.*;
  16. import java.io.*;
  17. @RestController
  18. @RequestMapping("/attachmngs")
  19. public class AttachmngsController {
  20. @Autowired
  21. private IAttachMngsService attachMngsService;
  22. @Autowired
  23. private MapUtils mapUtils;
  24. /**
  25. * 上传图片
  26. *
  27. * @param file 图片
  28. * @param longitude 经度
  29. * @param latitude 纬度
  30. * @return
  31. * @throws Exception
  32. */
  33. @PostMapping
  34. public AjaxResult updateAttachmngs(@RequestParam("avatarfile") MultipartFile file,
  35. @RequestParam("attachId") Long attachId,
  36. @RequestParam("longitude") String longitude,
  37. @RequestParam("latitude") String latitude,
  38. @RequestParam("updateDate") String date,
  39. HttpServletRequest request) throws Exception {
  40. if (file.isEmpty()) {
  41. return AjaxResult.error("上传失败请重试1");
  42. }
  43. System.out.println("水印时间");
  44. System.out.println(date);
  45. // 水印
  46. // String watermark = longitude + "," + latitude + "-" + date;
  47. String address = mapUtils.pointsToLocationsAll(longitude + "," + latitude);
  48. SysUser user = SecurityUtils.getLoginUser().getUser();
  49. // String watermark = longitude + "," + latitude + "-" + date + "-" + user.getNickName();
  50. String watermark = address + "||" + date + "||" + user.getNickName();
  51. System.out.println( );
  52. // String watermark = date;
  53. File file2 = AddWatermarkUtil.transferToFile(file);
  54. // addWaterMark(file2, file2, watermark);
  55. // 如果图片大于一兆压缩
  56. if (AddWatermarkUtil.fileToTransfer(file2).getBytes().length > 240800) {
  57. // 压缩
  58. Thumbnails.of(file2)
  59. .scale(1f) //图片大小(长宽)压缩比例 从0-1,1表示原图
  60. .outputQuality(0.5f) //图片质量压缩比例 从0-1,越接近1质量越好
  61. .toFile(file2);
  62. }
  63. AddWatermarkUtil.waterPress(file2, file2, Color.DARK_GRAY, 48, watermark);
  64. // AddWatermarkUtil.addWaterMark(file2, file2, Color.DARK_GRAY, 48, watermark);
  65. // 转二进制
  66. byte[] bytes = AddWatermarkUtil.fileToTransfer(file2).getBytes();
  67. // 存储到数据库
  68. AttachMngs attachMngs = new AttachMngs();
  69. attachMngs.setSysId(1L);
  70. attachMngs.setAttachId(attachId);
  71. attachMngs.setAttachFile(toObjects(bytes));
  72. Integer i = attachMngsService.insertAttachMngs(attachMngs);
  73. if (i != 1) {
  74. return AjaxResult.error("上传失败请重试2");
  75. }
  76. // 拼接url
  77. String serverName = request.getServerName();
  78. int serverPort = request.getServerPort();
  79. // return AjaxResult.success("操作成功", "http://" + serverName + ":" + serverPort + "/attachmngs/img/" + attachId);
  80. return AjaxResult.success("操作成功", "https://yd.echepei.com/prod-api/attachmngs/img/" + attachId);
  81. }
  82. /**
  83. * 获取图片
  84. *
  85. * @param attachId
  86. * @return
  87. */
  88. @GetMapping(value = "/img/{attachId}", produces = MediaType.IMAGE_JPEG_VALUE)
  89. public byte[] getImg(@PathVariable("attachId") String attachId) {
  90. Byte[] img = attachMngsService.getImg(attachId);
  91. return toPrimitives(img);
  92. }
  93. private Byte[] toObjects(byte[] bytesPrim) {
  94. Byte[] bytes = new Byte[bytesPrim.length];
  95. int i = 0;
  96. for (byte b : bytesPrim) bytes[i++] = b; // Autoboxing
  97. return bytes;
  98. }
  99. private byte[] toPrimitives(Byte[] oBytes) {
  100. byte[] bytes = new byte[oBytes.length];
  101. for (int i = 0; i < oBytes.length; i++) {
  102. bytes[i] = oBytes[i];
  103. }
  104. return bytes;
  105. }
  106. }