fix
Helius
2022-08-11 58c4a118bad3d33a553915781c9711d7bf2f6ee2
fix
3 files modified
2 files added
80 ■■■■■ changed files
pom.xml 13 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/configurations/P6spySqlFormatConfigure.java 21 ●●●●● patch | view | raw | blame | history
src/main/java/com/xcong/farmer/cms/modules/system/controller/AdminCommonController.java 31 ●●●●● patch | view | raw | blame | history
src/main/resources/application.yml 2 ●●●●● patch | view | raw | blame | history
src/main/resources/spy.properties 13 ●●●●● patch | view | raw | blame | history
pom.xml
@@ -279,6 +279,19 @@
            <artifactId>commons-text</artifactId>
            <version>1.9</version>
        </dependency>
        <!-- 控制台 SQL日志打印插件 -->
        <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.8.1</version>
        </dependency>
        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>0.4.8</version>
        </dependency>
    </dependencies>
    <build>
src/main/java/com/xcong/farmer/cms/configurations/P6spySqlFormatConfigure.java
New file
@@ -0,0 +1,21 @@
package com.xcong.farmer.cms.configurations;
import cn.hutool.core.date.DateUtil;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import org.apache.commons.lang3.StringUtils;
import java.time.LocalDateTime;
/**
 * 自定义 p6spy sql输出格式
 *
 * @author MrBird
 */
public class P6spySqlFormatConfigure implements MessageFormattingStrategy {
    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
        return StringUtils.isNotBlank(sql) ? DateUtil.format(LocalDateTime.now(), "yyyy-MM-dd HH:mm:ss")
                + " | 耗时 " + elapsed + " ms | SQL 语句:" + StringUtils.LF + sql.replaceAll("[\\s]+", StringUtils.SPACE) + ";" : StringUtils.EMPTY;
    }
}
src/main/java/com/xcong/farmer/cms/modules/system/controller/AdminCommonController.java
@@ -1,5 +1,8 @@
package com.xcong.farmer.cms.modules.system.controller;
import cn.hutool.core.img.Img;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.xcong.farmer.cms.common.contants.AppContants;
import com.xcong.farmer.cms.common.response.Result;
@@ -8,15 +11,20 @@
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.validation.Valid;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
@@ -128,8 +136,18 @@
        if (!uploadDir.isDirectory()) {
            uploadDir.mkdir();
        }
        //获得文件的后缀
        String filename = IdUtil.simpleUUID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        if (isImage(file.getInputStream())) {
            Thumbnails.of(file.getInputStream())
                    // 图片大小(长宽)压缩比例 从0-1,1表示原图
                    .scale(1f)
                    // 图片质量压缩比例 从0-1,越接近1质量越好
                    .outputQuality(0.5f)
                    .toOutputStream(new FileOutputStream(savePath + filename));
        } else {
        File filepath = new File(savePath + filename);
        try {
            //存文件
@@ -137,10 +155,19 @@
        } catch (IOException e) {
            e.printStackTrace();
        }
        }
        String visitPath = (saveUrl + filename);
        return Result.ok("上传成功",visitPath);
    }
    private boolean isImage(InputStream inputStream) {
        BufferedImage read = null;
        try {
            read = ImageIO.read(inputStream);
        } catch (IOException e) {
            return false;
        }
        return read != null;
    }
}
src/main/resources/application.yml
@@ -10,6 +10,8 @@
    async:
      request-timeout: 20s
  datasource:
    # 是否开启 SQL日志输出,生产环境建议关闭,有性能损耗
    p6spy: true
    url: jdbc:mysql://120.27.238.55:3306/farmer_cms?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2b8
    username: ct_test
    password: 123456
src/main/resources/spy.properties
New file
@@ -0,0 +1,13 @@
# p6spy\u914D\u7F6E\uFF0C\u6587\u6863 https://p6spy.readthedocs.io/en/latest/configandusage.html
# \u4F7F\u7528\u65E5\u5FD7\u7CFB\u7EDF\u8BB0\u5F55 sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
# \u81EA\u5B9A\u4E49\u65E5\u5FD7\u6253\u5370
logMessageFormat=com.xcong.farmer.cms.configurations.P6spySqlFormatConfigure
# \u662F\u5426\u5F00\u542F\u6162 SQL\u8BB0\u5F55
outagedetection=true
# \u6162 SQL\u8BB0\u5F55\u6807\u51C6 2 \u79D2
outagedetectioninterval=2
# \u5F00\u542F\u8FC7\u6EE4
filter=true
# \u5305\u542B QRTZ\u7684\u4E0D\u6253\u5370
exclude=QRTZ,select 1