综合查询接口

This commit is contained in:
cwchen 2025-04-02 18:20:17 +08:00
parent 9f9689f9e2
commit 93b45e0a14
9 changed files with 227 additions and 33 deletions

View File

@ -14,6 +14,8 @@ import java.util.List;
@Data
public class QueryParamDto {
private String imgPath;
/**
* 1.我的收藏 2.最近上传
*/

View File

@ -54,4 +54,7 @@ public class SynthesisQueryVo {
* 收藏状态 0.未收藏 1.已收藏
*/
private String collectStatus;
/**重要事项宣传类标题*/
private String title;
}

View File

@ -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<QueryParamDto> dto) {
String basicPath = SystemUtils.getUploadPath();
String imgPath = dto.getData().getImgPath();
ImageDownloadHandler.downloadImage(basicPath +File.separator+ imgPath, response, true);
}
}

View File

@ -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;
}
}
}

View File

@ -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();

View File

@ -27,20 +27,25 @@
<select id="getImgList" resultType="com.bonus.imgTool.backstage.entity.SynthesisQueryVo">
SELECT sfr.id,
sfr.original_file_path AS originalFilePath,
sfr.original_file_path AS compressFilePath,
sfr.original_file_path AS watermarkFilePath,
sfr.compress_file_path AS compressFilePath,
sfr.watermark_file_path AS watermarkFilePath,
sfr.upload_type AS uploadType,
CASE sfr.upload_type WHEN '1' THEN '安全违章' WHEN '2' THEN '质量检查' WHEN '3' THEN '安全措施落实'
WHEN '4' THEN '协调照片' WHEN '5' THEN '重要事项及宣传类' ELSE '' END AS uploadTypeName,
sfr.create_time AS uploadTime,
source_type AS sourceType,
sd2.dict_name AS sourceTypeName,
IF(tpc.file_resource_id IS NULL,'0','1') AS collectStatus
A.dict_name AS sourceTypeName,
IF(tpc.file_resource_id IS NULL,'0','1') AS collectStatus,
tcq.title
FROM tb_comprehensive_query tcq
LEFT JOIN sys_file_resource sfr ON tcq.id = sfr.source_id AND tcq.upload_type = sfr.upload_type AND sfr.is_active = '1'
LEFT JOIN tb_photo_collect tpc ON sfr.id = tpc.file_resource_id AND tpc.collect_user_id = #{userId}
LEFT JOIN sys_distinct sd ON sfr.source_type = sd.dict_value
LEFT JOIN sys_distinct sd2 ON sd.p_id = sd2.id
LEFT JOIN (
SELECT sd.dict_value,sd.dict_name
FROM sys_distinct sd
LEFT JOIN sys_distinct sd2 ON sd.p_id = sd2.id
WHERE sd2.dict_code = 'file_source_type'
) A ON A.dict_value = sfr.source_type
<where>
<if test="roleLevel = 0 and proIds != null and proIds.size() > 0">
AND tcq.pro_id IN
@ -48,7 +53,7 @@
#{proId}
</foreach>
</if>
AND tcq.is_active = '1' AND sd2.dict_value = 'file_source_type'
AND tcq.is_active = '1'
</where>
ORDER BY sfr.create_time DESC
</select>

View File

@ -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("<div class='img-info'>" +
" <div class='imgData layout'>\n" +
" <img src='" + item.compressFilePath + "'>" +
" <img src='" + filePath + "'>" +
" </div>" +
" <div class='imgData2 layout'>" +
" <p>" + item.uploadTime + "</p>" +
" <p class='img-color" + (item.uploadType) + "'>" + item.uploadTypeName + "</p>" +
" </div>" +
" <div class='imgData3 layout'>" +
" <p>" + item.sourceTypeName + "</p>" +
" <p>" + setSourceTypeName(item) + "</p>" +
setCollectData(item) +
" </div>" +
" <div class='hidden-actions'><div class='hidden-btn layout'>" +
@ -136,30 +137,20 @@ function initImgData(list) {
return "<img style='display:none;' src='../../img/synthesisQuery/collect_check.png'>";
}
// 设置标题
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) {

View File

@ -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();
}

View File

@ -104,4 +104,5 @@
</body>
<script src="../../js/synthesisQuery/synthesisQuery.js" charset="UTF-8" type="text/javascript"></script>
<script src="../../js/synthesisQuery/synthesisQueryAjax.js" charset="UTF-8" type="text/javascript"></script>
<script src="../../js/synthesisQuery/synthesisQueryCommon.js" charset="UTF-8" type="text/javascript"></script>
</html>