diff --git a/src/main/java/com/bonus/imgTool/backstage/entity/QueryParamDto.java b/src/main/java/com/bonus/imgTool/backstage/entity/QueryParamDto.java index 7d40eb2..af322b9 100644 --- a/src/main/java/com/bonus/imgTool/backstage/entity/QueryParamDto.java +++ b/src/main/java/com/bonus/imgTool/backstage/entity/QueryParamDto.java @@ -14,6 +14,8 @@ import java.util.List; @Data public class QueryParamDto { + private String imgPath; + /** * 1.我的收藏 2.最近上传 */ diff --git a/src/main/java/com/bonus/imgTool/backstage/entity/SynthesisQueryVo.java b/src/main/java/com/bonus/imgTool/backstage/entity/SynthesisQueryVo.java index 5b53f64..504f6d9 100644 --- a/src/main/java/com/bonus/imgTool/backstage/entity/SynthesisQueryVo.java +++ b/src/main/java/com/bonus/imgTool/backstage/entity/SynthesisQueryVo.java @@ -54,4 +54,7 @@ public class SynthesisQueryVo { * 收藏状态 0.未收藏 1.已收藏 */ private String collectStatus; + + /**重要事项宣传类标题*/ + private String title; } diff --git a/src/main/java/com/bonus/imgTool/system/controller/FileDownLoadController.java b/src/main/java/com/bonus/imgTool/system/controller/FileDownLoadController.java new file mode 100644 index 0000000..1a06065 --- /dev/null +++ b/src/main/java/com/bonus/imgTool/system/controller/FileDownLoadController.java @@ -0,0 +1,37 @@ +package com.bonus.imgTool.system.controller; + +import com.bonus.imgTool.annotation.DecryptAndVerify; +import com.bonus.imgTool.backstage.entity.QueryParamDto; +import com.bonus.imgTool.system.vo.EncryptedReq; +import com.bonus.imgTool.utils.ImageDownloadHandler; +import com.bonus.imgTool.utils.SystemUtils; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; + +/** + * @className:FileDownLoadController + * @author:cwchen + * @date:2025-04-02-17:21 + * @version:1.0 + * @description:文件下载 + */ +@RestController +@RequestMapping("/common/download/") +@Slf4j +public class FileDownLoadController { + + @ApiOperation("原图下载") + @GetMapping("/downloadImage") + @DecryptAndVerify(decryptedClass = QueryParamDto.class)//加解密统一管理 + public void downloadImage(HttpServletRequest request, HttpServletResponse response, EncryptedReq dto) { + String basicPath = SystemUtils.getUploadPath(); + String imgPath = dto.getData().getImgPath(); + ImageDownloadHandler.downloadImage(basicPath +File.separator+ imgPath, response, true); + } + +} diff --git a/src/main/java/com/bonus/imgTool/utils/ImageDownloadHandler.java b/src/main/java/com/bonus/imgTool/utils/ImageDownloadHandler.java new file mode 100644 index 0000000..6e2e663 --- /dev/null +++ b/src/main/java/com/bonus/imgTool/utils/ImageDownloadHandler.java @@ -0,0 +1,108 @@ +package com.bonus.imgTool.utils; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +/** + * @className:ImageDownloadHandler + * @author:cwchen + * @date:2025-04-02-17:30 + * @version:1.0 + * @description: 图片下载器 + */ +public class ImageDownloadHandler { + + /** + * 根据图片路径读取图片并返回到浏览器下载 + * @param imagePath 图片路径 + * @param response HttpServletResponse对象 + * @param download 是否作为附件下载(true-下载, false-直接显示) + */ + public static void downloadImage(String imagePath, HttpServletResponse response, boolean download) { + try { + Path path = Paths.get(imagePath); + + // 1. 检查文件是否存在 + if (!Files.exists(path)) { + response.sendError(HttpServletResponse.SC_NOT_FOUND, "图片不存在"); + return; + } + + // 2. 检查是否是文件 + if (!Files.isRegularFile(path)) { + response.sendError(HttpServletResponse.SC_BAD_REQUEST, "路径不是有效的文件"); + return; + } + + // 3. 获取文件类型并设置Content-Type + String contentType = determineContentType(imagePath); + if (contentType == null) { + response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE, "不支持的图片格式"); + return; + } + response.setContentType(contentType); + + // 4. 设置响应头 + String fileName = path.getFileName().toString(); + if (download) { + // 作为附件下载 + response.setHeader("Content-Disposition", + "attachment; filename=\"" + fileName + "\""); + } else { + // 直接显示在浏览器 + response.setHeader("Content-Disposition", + "inline; filename=\"" + fileName + "\""); + } + + // 5. 设置缓存控制 +// response.setHeader("Cache-Control", "max-age=3600"); // 1小时缓存 + + // 6. 读取文件并写入响应输出流 + try (InputStream in = Files.newInputStream(path); + OutputStream out = response.getOutputStream()) { + + byte[] buffer = new byte[4096]; + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + out.write(buffer, 0, bytesRead); + } + out.flush(); + } + + } catch (IOException e) { + try { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, + "读取图片时发生错误: " + e.getMessage()); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } + + /** + * 根据文件扩展名确定Content-Type + */ + private static String determineContentType(String filePath) { + String extension = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase(); + switch (extension) { + case "jpg": + case "jpeg": + return "image/jpeg"; + case "png": + return "image/png"; + case "gif": + return "image/gif"; + case "webp": + return "image/webp"; + case "bmp": + return "image/bmp"; + case "svg": + return "image/svg+xml"; + case "ico": + return "image/x-icon"; + default: + return null; + } + } +} diff --git a/src/main/java/com/bonus/imgTool/utils/PicUtils.java b/src/main/java/com/bonus/imgTool/utils/PicUtils.java index 0bb7f04..49bc675 100644 --- a/src/main/java/com/bonus/imgTool/utils/PicUtils.java +++ b/src/main/java/com/bonus/imgTool/utils/PicUtils.java @@ -101,9 +101,9 @@ public class PicUtils { } public static void main(String[] args) throws IOException { - byte[] bytes = compressPicForScale(FileTobyte(new File("L:\\其他\\测试图片\\6.jpg")), 200); - String newFileName = IDUtils.createID() + ".jpg"; - File fOut = new File("L:\\新建文件夹\\" + newFileName); + byte[] bytes = compressPicForScale(FileTobyte(new File("L:\\yn_img\\files\\yt\\2.png")), 200); + String newFileName = IDUtils.createID() + ".png"; + File fOut = new File("L:\\yn_img\\files\\ys\\" + newFileName); FileOutputStream fileOutputStream = new FileOutputStream(fOut); fileOutputStream.write(bytes); fileOutputStream.close(); diff --git a/src/main/resources/mappers/backstage/SynthesisQueryMapper.xml b/src/main/resources/mappers/backstage/SynthesisQueryMapper.xml index a13e2d7..5d6d1a6 100644 --- a/src/main/resources/mappers/backstage/SynthesisQueryMapper.xml +++ b/src/main/resources/mappers/backstage/SynthesisQueryMapper.xml @@ -27,20 +27,25 @@ diff --git a/src/main/resources/static/js/synthesisQuery/synthesisQuery.js b/src/main/resources/static/js/synthesisQuery/synthesisQuery.js index d07e401..6119735 100644 --- a/src/main/resources/static/js/synthesisQuery/synthesisQuery.js +++ b/src/main/resources/static/js/synthesisQuery/synthesisQuery.js @@ -93,16 +93,17 @@ function initImgData(list) { let htmlArr = []; if (list && list.length > 0) { $.each(list, function (index, item) { + let filePath = imgUrl + item.compressFilePath + "?token=" + tokens htmlArr.push("
" + "
\n" + - " " + + " " + "
" + "
" + "

" + item.uploadTime + "

" + "

" + item.uploadTypeName + "

" + "
" + "
" + - "

" + item.sourceTypeName + "

" + + "

" + setSourceTypeName(item) + "

" + setCollectData(item) + "
" + "
" + @@ -136,30 +137,20 @@ function initImgData(list) { return ""; } + // 设置标题 + function setSourceTypeName(item){ + if(item.sourceType === '9'){ + return item.title; + }else{ + return item.sourceTypeName.split('-')[1]; + } + } + } -/**放大*/ -function viewImg(item) { - layer.photos({ - shade: 0.5, - footer: false, - photos: { - "title": "图片预览", - "start": 0, - "data": [ - { - "pid": 1, - "src": "../../img/synthesisQuery/7.jpg", - } - ] - }, - }); -} -/**放大*/ -function imgDownLoad(item) { - alert(item.id) -} + + /**水印下载*/ function waterImgDownLoad(item) { diff --git a/src/main/resources/static/js/synthesisQuery/synthesisQueryCommon.js b/src/main/resources/static/js/synthesisQuery/synthesisQueryCommon.js new file mode 100644 index 0000000..91e490f --- /dev/null +++ b/src/main/resources/static/js/synthesisQuery/synthesisQueryCommon.js @@ -0,0 +1,47 @@ +/**放大*/ +function viewImg(item) { + layer.photos({ + shade: 0.5, + footer: false, + photos: { + "title": "图片预览", + "start": 0, + "data": [ + { + "pid": 1, + "src": imgUrl + item.originalFilePath + "?token=" + tokens, + } + ] + }, + }); +} + +/**原图下载*/ +function imgDownLoad(item) { + let orginalPath = item.originalFilePath; + let obj = { + imgPath: orginalPath, + } + let loadingMsg = layer.msg("原图下载中,请稍候...", {icon: 16, scrollbar: false, time: 0,}); + let url = dataUrl + "/common/download/downloadImage?token=" + tokens + "&encryptedData=" + encodeURIComponent(encryptCBC(JSON.stringify(obj))); + let xhr = new XMLHttpRequest(); + xhr.open("get", url, true); + xhr.responseType = "blob"; // 转换流 + xhr.setRequestHeader('Content-Type','application/json;charset=UTF-8') + xhr.onload = function () { + layer.close(loadingMsg); + if (this.status === 200) { + let blob = this.response; + var a = document.createElement("a"); + var url = window.URL.createObjectURL(blob); + a.href = url; + a.download = orginalPath.substring(orginalPath.lastIndexOf('/') + 1,orginalPath.length); // 文件名 + } else { + layer.msg("原图下载发生异常,请稍后重试", {icon: 16, scrollbar: false, time: 2000}); + } + a.click(); + window.URL.revokeObjectURL(url); + }; + // xhr.send(params); + xhr.send(); +} \ No newline at end of file diff --git a/src/main/resources/static/pages/synthesisQuery/synthesisQuery.html b/src/main/resources/static/pages/synthesisQuery/synthesisQuery.html index 6f5d1fb..767b4d9 100644 --- a/src/main/resources/static/pages/synthesisQuery/synthesisQuery.html +++ b/src/main/resources/static/pages/synthesisQuery/synthesisQuery.html @@ -104,4 +104,5 @@ + \ No newline at end of file