|
@@ -17,8 +17,12 @@ import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
|
|
+import java.awt.*;
|
|
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
|
|
+import java.math.RoundingMode;
|
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
import java.text.DecimalFormat;
|
|
import java.text.DecimalFormat;
|
|
@@ -68,7 +72,7 @@ public class FileUtils {
|
|
|
@Value("${aliyun.oss.bucket}")
|
|
@Value("${aliyun.oss.bucket}")
|
|
|
public String oss_bucketName;
|
|
public String oss_bucketName;
|
|
|
|
|
|
|
|
- private static void defalutOSS() {
|
|
|
|
|
|
|
+ private static void defaultOss() {
|
|
|
if (StringUtils.isBlank(endpoint)) {
|
|
if (StringUtils.isBlank(endpoint)) {
|
|
|
endpoint = DEFAULT_ENDPOINT;
|
|
endpoint = DEFAULT_ENDPOINT;
|
|
|
}
|
|
}
|
|
@@ -128,7 +132,7 @@ public class FileUtils {
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
public static String getOSSAddressPrefix() {
|
|
public static String getOSSAddressPrefix() {
|
|
|
- defalutOSS();
|
|
|
|
|
|
|
+ defaultOss();
|
|
|
return StringConstant.HTTPS_STRING
|
|
return StringConstant.HTTPS_STRING
|
|
|
+ StringConstant.COLON
|
|
+ StringConstant.COLON
|
|
|
+ StringConstant.LEFT_SEPARATORS
|
|
+ StringConstant.LEFT_SEPARATORS
|
|
@@ -240,7 +244,7 @@ public class FileUtils {
|
|
|
public static Map<String, String> uploadFileByInfo(MultipartFile file, FileEnum fileEnum) {
|
|
public static Map<String, String> uploadFileByInfo(MultipartFile file, FileEnum fileEnum) {
|
|
|
Map<String, String> infoMap = new HashMap<>(NumberConstant.SIXTEEN);
|
|
Map<String, String> infoMap = new HashMap<>(NumberConstant.SIXTEEN);
|
|
|
//创建OSSClient实例
|
|
//创建OSSClient实例
|
|
|
- defalutOSS();
|
|
|
|
|
|
|
+ defaultOss();
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
try {
|
|
try {
|
|
|
//容器不存在,就创建
|
|
//容器不存在,就创建
|
|
@@ -312,7 +316,7 @@ public class FileUtils {
|
|
|
conf.setSupportCname(false);
|
|
conf.setSupportCname(false);
|
|
|
try {
|
|
try {
|
|
|
//创建OSSClient实例
|
|
//创建OSSClient实例
|
|
|
- defalutOSS();
|
|
|
|
|
|
|
+ defaultOss();
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
|
|
|
//容器不存在,就创建
|
|
//容器不存在,就创建
|
|
@@ -367,7 +371,7 @@ public class FileUtils {
|
|
|
public static Map<String, String> uploadFileByInfo(InputStream inputStream, String originalFilename, FileEnum fileEnum) {
|
|
public static Map<String, String> uploadFileByInfo(InputStream inputStream, String originalFilename, FileEnum fileEnum) {
|
|
|
Map<String, String> infoMap = new HashMap<>(NumberConstant.SIXTEEN);
|
|
Map<String, String> infoMap = new HashMap<>(NumberConstant.SIXTEEN);
|
|
|
//创建OSSClient实例
|
|
//创建OSSClient实例
|
|
|
- defalutOSS();
|
|
|
|
|
|
|
+ defaultOss();
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
try {
|
|
try {
|
|
|
//容器不存在,就创建
|
|
//容器不存在,就创建
|
|
@@ -427,7 +431,7 @@ public class FileUtils {
|
|
|
*/
|
|
*/
|
|
|
public static FileInfo uploadFileInfo(MultipartFile file, FileInfo fileInfo, FileEnum fileEnum) {
|
|
public static FileInfo uploadFileInfo(MultipartFile file, FileInfo fileInfo, FileEnum fileEnum) {
|
|
|
//创建OSSClient实例
|
|
//创建OSSClient实例
|
|
|
- defalutOSS();
|
|
|
|
|
|
|
+ defaultOss();
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
try {
|
|
try {
|
|
|
//容器不存在,就创建
|
|
//容器不存在,就创建
|
|
@@ -536,8 +540,7 @@ public class FileUtils {
|
|
|
default:
|
|
default:
|
|
|
throw new RuntimeException("file size error");
|
|
throw new RuntimeException("file size error");
|
|
|
}
|
|
}
|
|
|
- bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
|
|
|
|
|
- return bigDecimal;
|
|
|
|
|
|
|
+ return bigDecimal.setScale(6, RoundingMode.HALF_UP);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -581,7 +584,7 @@ public class FileUtils {
|
|
|
public static void downOSSFile(String fileName, HttpServletResponse response) {
|
|
public static void downOSSFile(String fileName, HttpServletResponse response) {
|
|
|
BufferedInputStream input = null;
|
|
BufferedInputStream input = null;
|
|
|
OutputStream outputStream = null;
|
|
OutputStream outputStream = null;
|
|
|
- defalutOSS();
|
|
|
|
|
|
|
+ defaultOss();
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
// OSSObject ossObject = ossClient.getObject(bucketName, folder + fileName);
|
|
// OSSObject ossObject = ossClient.getObject(bucketName, folder + fileName);
|
|
|
OSSObject ossObject = ossClient.getObject(bucketName, fileName);
|
|
OSSObject ossObject = ossClient.getObject(bucketName, fileName);
|
|
@@ -628,7 +631,7 @@ public class FileUtils {
|
|
|
* 文件全路径 kll/uploads/20230605/146325493677821952598454132.txt 去除“https://kaiwu-saas.oss-cn-chengdu.aliyuncs.com/”
|
|
* 文件全路径 kll/uploads/20230605/146325493677821952598454132.txt 去除“https://kaiwu-saas.oss-cn-chengdu.aliyuncs.com/”
|
|
|
*/
|
|
*/
|
|
|
public static void downloadByFileName(HttpServletResponse response, String fileName) {
|
|
public static void downloadByFileName(HttpServletResponse response, String fileName) {
|
|
|
- defalutOSS();
|
|
|
|
|
|
|
+ defaultOss();
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
OSSObject ossObject = ossClient.getObject(bucketName, fileName);
|
|
OSSObject ossObject = ossClient.getObject(bucketName, fileName);
|
|
|
boolean exist = ossClient.doesObjectExist(bucketName, fileName);
|
|
boolean exist = ossClient.doesObjectExist(bucketName, fileName);
|
|
@@ -1234,6 +1237,151 @@ public class FileUtils {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ final static String IMG_FORMAT = "png_jpg_bmp_gif_tif_jpeg_PNG_JPG_BMP_GIF_TIF_JPEG_HEIC_heic";
|
|
|
|
|
+ final static String IMG_FORMAT_NUMBER = "8950_ffd8_424d_4749_4d4d_4949_5249";
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 针对图片内容的格式效验。
|
|
|
|
|
+ * 分别以:
|
|
|
|
|
+ * 1:判断后缀名的方式判断是否为图片
|
|
|
|
|
+ * 2:以魔术数字进行判断
|
|
|
|
|
+ * 3:以imageIO流的方式验证是否为图片
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param mFile
|
|
|
|
|
+ * @return boolean
|
|
|
|
|
+ * @author lihao
|
|
|
|
|
+ */
|
|
|
|
|
+ public static boolean isImage(MultipartFile mFile) {
|
|
|
|
|
+ File file = null;
|
|
|
|
|
+ InputStream is = null;
|
|
|
|
|
+ Image img = null;
|
|
|
|
|
+ byte[] bt = new byte[2];
|
|
|
|
|
+ try {
|
|
|
|
|
+ file = multipartFile2File(mFile);
|
|
|
|
|
+ is = new FileInputStream(file);
|
|
|
|
|
+ is.read(bt);
|
|
|
|
|
+ img = ImageIO.read(file);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //获取文件后缀进行判断
|
|
|
|
|
+ String suffix = mFile.getOriginalFilename().substring(mFile.getOriginalFilename().lastIndexOf(".") + 1);
|
|
|
|
|
+ if (IMG_FORMAT.indexOf(suffix) == -1) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //以魔术数字进行判断
|
|
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
|
+ for (int i = 0; i < bt.length; i++) {
|
|
|
|
|
+ int v = bt[i] & 0xFF;//byte to int
|
|
|
|
|
+ String hv = Integer.toHexString(v);
|
|
|
|
|
+ if (hv.length() < 2) {
|
|
|
|
|
+ stringBuilder.append(0);
|
|
|
|
|
+ }
|
|
|
|
|
+ stringBuilder.append(hv);
|
|
|
|
|
+ }
|
|
|
|
|
+ System.out.println(stringBuilder.toString());
|
|
|
|
|
+ if (IMG_FORMAT_NUMBER.indexOf(stringBuilder.toString()) == -1) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //以imageIO流的方式对图片进行格式检查
|
|
|
|
|
+ if (img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (file.exists()) {
|
|
|
|
|
+ file.delete();
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 给图片添加水印、可设置水印图片旋转角度
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param logoText 水印文字
|
|
|
|
|
+ * @param degree 水印图片旋转角度
|
|
|
|
|
+ * @param clarity 透明度(小于1的数)越接近0越透明
|
|
|
|
|
+ * @param mFile 被操作的图片
|
|
|
|
|
+ */
|
|
|
|
|
+ public byte[] waterMarkByText(String logoText,
|
|
|
|
|
+ Integer degree,
|
|
|
|
|
+ Float clarity, MultipartFile mFile) {
|
|
|
|
|
+ // 图片流
|
|
|
|
|
+ InputStream inputStream = null;
|
|
|
|
|
+ ByteArrayOutputStream byteStream = null;
|
|
|
|
|
+ Image srcImg = null;
|
|
|
|
|
+ File file = null;
|
|
|
|
|
+ byte[] byteImg = new byte[0];
|
|
|
|
|
+
|
|
|
|
|
+ //水印添加位置坐标 1像素=1磅*DPI/72 显示屏dpi 大多是72 少数96 这里不对
|
|
|
|
|
+ Integer width, height;
|
|
|
|
|
+ Integer fontSize = 50;
|
|
|
|
|
+ Integer rightButtomOffset = fontSize * 72 / 72 * 6;
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ file = File.createTempFile("tmp", null);
|
|
|
|
|
+ mFile.transferTo(file);
|
|
|
|
|
+ srcImg = ImageIO.read(file);
|
|
|
|
|
+ width = srcImg.getWidth(null) - rightButtomOffset;
|
|
|
|
|
+ height = srcImg.getHeight(null);
|
|
|
|
|
+ BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
|
|
|
|
|
+ srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
|
|
|
|
|
+ // 得到画笔对象
|
|
|
|
|
+ Graphics2D g = buffImg.createGraphics();
|
|
|
|
|
+ // 设置对线段的锯齿状边缘处理
|
|
|
|
|
+ g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
|
|
|
|
|
+ RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
|
|
|
+ g.drawImage(
|
|
|
|
|
+ srcImg.getScaledInstance(srcImg.getWidth(null),
|
|
|
|
|
+ srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
|
|
|
|
|
+ null);
|
|
|
|
|
+ if (null != degree) {
|
|
|
|
|
+ // 设置水印旋转
|
|
|
|
|
+ g.rotate(Math.toRadians(degree),
|
|
|
|
|
+ (double) buffImg.getWidth() / 2,
|
|
|
|
|
+ (double) buffImg.getHeight() / 2);
|
|
|
|
|
+ }
|
|
|
|
|
+ // 设置颜色
|
|
|
|
|
+ g.setColor(Color.red);
|
|
|
|
|
+ // 设置 Font
|
|
|
|
|
+ g.setFont(new Font("宋体", Font.BOLD, fontSize));
|
|
|
|
|
+ float alpha = clarity;
|
|
|
|
|
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
|
|
|
|
|
+ alpha));
|
|
|
|
|
+ // 第一参数->设置的内容,后面两个参数->文字在图片上的坐标位置(x,y) .
|
|
|
|
|
+ g.drawString(logoText, width, height);
|
|
|
|
|
+ g.dispose();
|
|
|
|
|
+ // 生成图片
|
|
|
|
|
+ ImageIO.write(buffImg, "JPG", file);
|
|
|
|
|
+ inputStream = new FileInputStream(file);
|
|
|
|
|
+ byteStream = new ByteArrayOutputStream();
|
|
|
|
|
+ int ch;
|
|
|
|
|
+ while ((ch = inputStream.read()) != -1) {
|
|
|
|
|
+ byteStream.write(ch);
|
|
|
|
|
+ }
|
|
|
|
|
+ byteImg = byteStream.toByteArray();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (null != inputStream) {
|
|
|
|
|
+ inputStream.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (null != byteStream) {
|
|
|
|
|
+ byteStream.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (file.exists()) {
|
|
|
|
|
+ file.delete();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return byteImg;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 测试
|
|
// 测试
|
|
|
public static void main(String[] args) throws FileNotFoundException {
|
|
public static void main(String[] args) throws FileNotFoundException {
|
|
|
//阿里云OSS账号自行到阿里云官网申请
|
|
//阿里云OSS账号自行到阿里云官网申请
|