From dcd2ecc1d85e6a0ef7389076ed6264dc1c616440 Mon Sep 17 00:00:00 2001 From: huangfeng Date: Tue, 22 Apr 2025 11:46:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=B9=B6=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E6=96=87=E4=BB=B6=E5=90=8D=E8=A7=A3=E6=9E=90=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E5=85=A5=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TerminalPhotoController.java | 14 +++- .../config/SecurityConfig.java | 1 + .../impl/TerminalPhotoServiceImpl.java | 72 ++++++++++++++++++- .../service/TerminalPhotoService.java | 2 + 4 files changed, 86 insertions(+), 3 deletions(-) diff --git a/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/TerminalPhotoController.java b/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/TerminalPhotoController.java index 5e0b4d5..9f0af3a 100644 --- a/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/TerminalPhotoController.java +++ b/xymanager_admin/src/main/java/com/shxy/xymanager_admin/controller/TerminalPhotoController.java @@ -21,9 +21,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; -import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Date; @@ -292,6 +292,18 @@ public class TerminalPhotoController extends BaseController { return result; } + @PostMapping("uploadPhoto") + @ApiOperation("上传图片") + @Log(title = "上传图片", type = "上传") + public ResponseReult uploadPhoto(@RequestParam("file") MultipartFile file, + @RequestParam("cmdid") String cmdid) throws Exception { + if (file != null && file.getInputStream() != null) { + terminalPhotoService.upload(file, cmdid); + return ResponseReult.success("OK"); + } else { + return ResponseReult.error("缺少上传文件"); + } + } @ApiOperation(value = "图片轮询", notes = "图片轮询接口", httpMethod = "POST") @ApiResponses({@ApiResponse(code = 200, message = "请求成功"), @ApiResponse(code = 400, message = "请求参数没填好"), @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对")}) diff --git a/xymanager_common/src/main/java/com/shxy/xymanager_common/config/SecurityConfig.java b/xymanager_common/src/main/java/com/shxy/xymanager_common/config/SecurityConfig.java index 19315a8..c30b68e 100644 --- a/xymanager_common/src/main/java/com/shxy/xymanager_common/config/SecurityConfig.java +++ b/xymanager_common/src/main/java/com/shxy/xymanager_common/config/SecurityConfig.java @@ -77,6 +77,7 @@ public class SecurityConfig { urlWhiteList.add("/xymanager/kaptcha"); urlWhiteList.add("/xymanager/test/**"); urlWhiteList.add("/xymanager/mntn/**"); + urlWhiteList.add("/xymanager/uploadPhoto"); StreamReadConstraints streamReadConstraints = StreamReadConstraints .builder() diff --git a/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/TerminalPhotoServiceImpl.java b/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/TerminalPhotoServiceImpl.java index ef980e4..a7c008d 100644 --- a/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/TerminalPhotoServiceImpl.java +++ b/xymanager_service/src/main/java/com/shxy/xymanager_service/impl/TerminalPhotoServiceImpl.java @@ -15,6 +15,7 @@ import com.shxy.xymanager_common.dto.*; import com.shxy.xymanager_common.entity.*; import com.shxy.xymanager_common.enums.CommonStatus; import com.shxy.xymanager_common.enums.TakePicStatus; +import com.shxy.xymanager_common.exception.ApiException; import com.shxy.xymanager_common.exception.Asserts; import com.shxy.xymanager_common.model.*; import com.shxy.xymanager_common.page.PageUtils; @@ -25,7 +26,6 @@ import com.shxy.xymanager_common.vo.*; import com.shxy.xymanager_dao.dao.*; import com.shxy.xymanager_service.service.*; import lombok.extern.slf4j.Slf4j; -import org.apache.poi.ss.formula.functions.T; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -34,10 +34,13 @@ import org.springframework.util.CollectionUtils; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import javax.validation.constraints.NotNull; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.math.BigInteger; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.*; /** @@ -423,6 +426,71 @@ public class TerminalPhotoServiceImpl implements TerminalPhotoService { return Asserts.success("OK"); } + @Override + public void upload(MultipartFile file, String cmdid) throws Exception { + Terminals term = terminalExtService.getByCmdid(cmdid); + if (term == null) { + throw new ApiException(cmdid + "该装置不存在"); + } + String suffix; + String hexC = ""; + Long time; + TerminalPhoto record = new TerminalPhoto(); + try { + String fileName = org.springframework.util.StringUtils.cleanPath(file.getOriginalFilename()); + suffix = org.springframework.util.StringUtils.getFilenameExtension(fileName); + String prefix = fileName.replace("." + suffix, ""); + String[] str = prefix.split("_"); + Long photoId = Long.parseLong(str[1], 16); + Integer channel = Integer.parseInt(str[2], 16); + hexC = str[3]; + Integer preset = Integer.parseInt(hexC, 16); + time = Long.parseLong(str[6], 16); + record.setChannelId(channel); + record.setPresetId(preset); + record.setPhotoTime(BigInteger.valueOf(time)); + record.setOrginalId(BigInteger.valueOf(photoId)); + } catch (Exception ex) { + throw new ApiException("文件名不符合规范,解析失败"); + } + int length = cmdid.length(); + Date now = new Date(); + String folder = DateUtil.format(now, "yyyy/MM/dd") + "/" + cmdid.substring(length - 2); + File dir = new File(C_PATH + folder); + dir.mkdirs(); + Date photoTime = new Date(time * 1000); + String strTime = DateUtil.format(photoTime, "yyyyMMddHHmmss"); + String filename = cmdid + "_" + record.getChannelId() + "_" + hexC + "_" + strTime + "." + suffix; + String filePath = folder + "/" + filename; + + File dest = new File(C_PATH + filePath); + file.transferTo(dest); + log.info("成功上传一张图片" + filePath); + + record.setTermId(term.getId()); + record.setMediaType(0); + record.setPath(filePath); + record.setRecvTime(BigInteger.valueOf(now.getTime() / 1000)); + record.setRecvEndTime(BigInteger.valueOf(now.getTime() / 1000)); + record.setCreateTime(now); + record.setFlags(0); + record.setIsMark(0); + try { + BufferedImage image = ImageIO.read(dest); + if (image != null) { + int width = image.getWidth(); + int height = image.getHeight(); + record.setWidth(width); + record.setHeight(height); + } + } catch (IOException ignore) { + } + long fileSize = Files.size(Paths.get(C_PATH + filePath)); + record.setFileSize((int) fileSize); + + terminalPhotoDao.insert(record); + } + /** * 图片查询 */ diff --git a/xymanager_service/src/main/java/com/shxy/xymanager_service/service/TerminalPhotoService.java b/xymanager_service/src/main/java/com/shxy/xymanager_service/service/TerminalPhotoService.java index f3e43f9..a6dcede 100644 --- a/xymanager_service/src/main/java/com/shxy/xymanager_service/service/TerminalPhotoService.java +++ b/xymanager_service/src/main/java/com/shxy/xymanager_service/service/TerminalPhotoService.java @@ -117,5 +117,7 @@ public interface TerminalPhotoService { ServiceBody takeAlarm(TerminalPhotoTestVo vo); + void upload(MultipartFile file, String cmdid) throws Exception; + }