123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- package com.ruoyi.web.controller.wx;
- import com.ruoyi.common.MapUtils;
- import com.ruoyi.common.core.domain.AjaxResult;
- import com.ruoyi.common.core.domain.entity.SysUser;
- import com.ruoyi.common.utils.AddWatermarkUtil;
- import com.ruoyi.common.utils.SecurityUtils;
- import com.ruoyi.system.domain.AttachMngs;
- import com.ruoyi.system.service.IAttachMngsService;
- import net.coobird.thumbnailator.Thumbnails;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.MediaType;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import javax.servlet.http.HttpServletRequest;
- import java.awt.*;
- import java.io.*;
- @RestController
- @RequestMapping("/attachmngs")
- public class AttachmngsController {
- @Autowired
- private IAttachMngsService attachMngsService;
- @Autowired
- private MapUtils mapUtils;
- /**
- * 上传图片
- *
- * @param file 图片
- * @param longitude 经度
- * @param latitude 纬度
- * @return
- * @throws Exception
- */
- @PostMapping
- public AjaxResult updateAttachmngs(@RequestParam("avatarfile") MultipartFile file,
- @RequestParam("attachId") Long attachId,
- @RequestParam("longitude") String longitude,
- @RequestParam("latitude") String latitude,
- @RequestParam("updateDate") String date,
- HttpServletRequest request) throws Exception {
- if (file.isEmpty()) {
- return AjaxResult.error("上传失败请重试1");
- }
- System.out.println("水印时间");
- System.out.println(date);
- // 水印
- // String watermark = longitude + "," + latitude + "-" + date;
- String address = mapUtils.pointsToLocationsAll(longitude + "," + latitude);
- SysUser user = SecurityUtils.getLoginUser().getUser();
- // String watermark = longitude + "," + latitude + "-" + date + "-" + user.getNickName();
- String watermark = address + "||" + date + "||" + user.getNickName();
- System.out.println( );
- // String watermark = date;
- File file2 = AddWatermarkUtil.transferToFile(file);
- // addWaterMark(file2, file2, watermark);
- // 如果图片大于一兆压缩
- if (AddWatermarkUtil.fileToTransfer(file2).getBytes().length > 240800) {
- // 压缩
- Thumbnails.of(file2)
- .scale(1f) //图片大小(长宽)压缩比例 从0-1,1表示原图
- .outputQuality(0.5f) //图片质量压缩比例 从0-1,越接近1质量越好
- .toFile(file2);
- }
- AddWatermarkUtil.waterPress(file2, file2, Color.DARK_GRAY, 48, watermark);
- // AddWatermarkUtil.addWaterMark(file2, file2, Color.DARK_GRAY, 48, watermark);
- // 转二进制
- byte[] bytes = AddWatermarkUtil.fileToTransfer(file2).getBytes();
- // 存储到数据库
- AttachMngs attachMngs = new AttachMngs();
- attachMngs.setSysId(1L);
- attachMngs.setAttachId(attachId);
- attachMngs.setAttachFile(toObjects(bytes));
- Integer i = attachMngsService.insertAttachMngs(attachMngs);
- if (i != 1) {
- return AjaxResult.error("上传失败请重试2");
- }
- // 拼接url
- String serverName = request.getServerName();
- int serverPort = request.getServerPort();
- // return AjaxResult.success("操作成功", "http://" + serverName + ":" + serverPort + "/attachmngs/img/" + attachId);
- return AjaxResult.success("操作成功", "https://yd.echepei.com/prod-api/attachmngs/img/" + attachId);
- }
- /**
- * 获取图片
- *
- * @param attachId
- * @return
- */
- @GetMapping(value = "/img/{attachId}", produces = MediaType.IMAGE_JPEG_VALUE)
- public byte[] getImg(@PathVariable("attachId") String attachId) {
- Byte[] img = attachMngsService.getImg(attachId);
- return toPrimitives(img);
- }
- private Byte[] toObjects(byte[] bytesPrim) {
- Byte[] bytes = new Byte[bytesPrim.length];
- int i = 0;
- for (byte b : bytesPrim) bytes[i++] = b; // Autoboxing
- return bytes;
- }
- private byte[] toPrimitives(Byte[] oBytes) {
- byte[] bytes = new byte[oBytes.length];
- for (int i = 0; i < oBytes.length; i++) {
- bytes[i] = oBytes[i];
- }
- return bytes;
- }
- }
|