Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
9291851dd6
6
pom.xml
6
pom.xml
|
|
@ -360,6 +360,12 @@
|
|||
<artifactId>easypoi-base</artifactId>
|
||||
<version>4.1.2</version>
|
||||
</dependency>
|
||||
<!--thumbnailator图片处理-->
|
||||
<dependency>
|
||||
<groupId>net.coobird</groupId>
|
||||
<artifactId>thumbnailator</artifactId>
|
||||
<version>0.4.8</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,63 @@
|
|||
package com.bonus.imgTool.system.controller;
|
||||
|
||||
import com.bonus.imgTool.utils.DateTimeHelper;
|
||||
import com.bonus.imgTool.utils.IDUtils;
|
||||
import com.bonus.imgTool.utils.SystemUtils;
|
||||
import com.bonus.imgTool.webResult.AjaxResult;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @className:FileUploadController
|
||||
* @author:cwchen
|
||||
* @date:2025-04-02-9:20
|
||||
* @version:1.0
|
||||
* @description:文件上传
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/common/file/")
|
||||
@Slf4j
|
||||
public class FileUploadController {
|
||||
|
||||
@ApiOperation(value = "文件上传")
|
||||
@PostMapping("uploadFile")
|
||||
public AjaxResult appUploadFile(@RequestParam(value = "files") MultipartFile[] files) {
|
||||
try {
|
||||
List<Map<String,Object>> fileList = new ArrayList<>();
|
||||
for (MultipartFile file : files) {
|
||||
String fileName = file.getOriginalFilename();
|
||||
String suffix = IDUtils.getSuffix(fileName);
|
||||
String fileMbSize = IDUtils.getFileMbSize(file.getSize());
|
||||
String path = File.separator + DateTimeHelper.getNowYMD() + File.separator + IDUtils.createID() + suffix;
|
||||
String newPath = SystemUtils.getUploadPath() + path;
|
||||
File uploadFile = new File(newPath);
|
||||
//生成文件夹
|
||||
if (!uploadFile.getParentFile().exists()) {
|
||||
uploadFile.getParentFile().mkdirs();
|
||||
}
|
||||
// 存入临时文件
|
||||
file.transferTo(uploadFile);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("filePath", path);
|
||||
map.put("fileName", fileName);
|
||||
map.put("fileSize", fileMbSize);
|
||||
fileList.add(map);
|
||||
}
|
||||
return AjaxResult.success("上传成功", fileList);
|
||||
} catch (Exception e) {
|
||||
log.error(e.toString(), e);
|
||||
return AjaxResult.error("文件上传异常");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -20,4 +20,24 @@ public class IDUtils {
|
|||
return System.currentTimeMillis() + String.valueOf(r).substring(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 传入文件 名称
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
public static String getSuffix(String fileName){
|
||||
return fileName.substring(fileName.lastIndexOf('.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将文件大小转为Mb
|
||||
* @param bytes
|
||||
* @return
|
||||
*/
|
||||
public static String getFileMbSize(long bytes){
|
||||
double mb = bytes / (1024.0 * 1024.0);
|
||||
System.out.println("文件大小: " + String.format("%.2f MB", mb));
|
||||
return String.format("%.2f", mb);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,111 @@
|
|||
package com.bonus.imgTool.utils;
|
||||
|
||||
import net.coobird.thumbnailator.Thumbnails;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* @author 10488
|
||||
* 图片压缩工具类
|
||||
*/
|
||||
public class PicUtils {
|
||||
//以下是常量,按照阿里代码开发规范,不允许代码中出现魔法值
|
||||
private static final Logger logger = LoggerFactory.getLogger(PicUtils.class);
|
||||
private static final Integer ZERO = 0;
|
||||
private static final Integer ONE_ZERO_TWO_FOUR = 1024;
|
||||
private static final Integer NINE_ZERO_ZERO = 900;
|
||||
private static final Integer THREE_TWO_SEVEN_FIVE = 3275;
|
||||
private static final Integer TWO_ZERO_FOUR_SEVEN = 2047;
|
||||
private static final Double ZERO_EIGHT_FIVE = 0.85;
|
||||
private static final Double ZERO_SIX = 0.6;
|
||||
private static final Double ZERO_FOUR_FOUR = 0.44;
|
||||
private static final Double ZERO_FOUR = 0.4;
|
||||
|
||||
/**
|
||||
* 根据指定大小压缩图片
|
||||
*
|
||||
* @param imageBytes 源图片字节数组
|
||||
* @param desFileSize 指定图片大小,单位kb
|
||||
* @return 压缩质量后的图片字节数组
|
||||
*/
|
||||
public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize) {
|
||||
if (imageBytes == null || imageBytes.length <= ZERO || imageBytes.length < desFileSize * ONE_ZERO_TWO_FOUR) {
|
||||
return imageBytes;
|
||||
}
|
||||
long srcSize = imageBytes.length;
|
||||
double accuracy = getAccuracy(srcSize / ONE_ZERO_TWO_FOUR);
|
||||
try {
|
||||
while (imageBytes.length > desFileSize * ONE_ZERO_TWO_FOUR) {
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
|
||||
Thumbnails.of(inputStream)
|
||||
.scale(accuracy)
|
||||
.outputQuality(accuracy)
|
||||
.toOutputStream(outputStream);
|
||||
imageBytes = outputStream.toByteArray();
|
||||
}
|
||||
logger.info("图片原大小={}kb | 压缩后大小={}kb",
|
||||
srcSize / ONE_ZERO_TWO_FOUR, imageBytes.length / ONE_ZERO_TWO_FOUR);
|
||||
} catch (Exception e) {
|
||||
logger.error("【图片压缩】msg=图片压缩失败!", e);
|
||||
}
|
||||
return imageBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动调节精度(经验数值)
|
||||
*
|
||||
* @param size 源图片大小
|
||||
* @return 图片压缩质量比
|
||||
*/
|
||||
private static double getAccuracy(long size) {
|
||||
double accuracy;
|
||||
if (size < NINE_ZERO_ZERO) {
|
||||
accuracy = ZERO_EIGHT_FIVE;
|
||||
} else if (size < TWO_ZERO_FOUR_SEVEN) {
|
||||
accuracy = ZERO_SIX;
|
||||
} else if (size < THREE_TWO_SEVEN_FIVE) {
|
||||
accuracy = ZERO_FOUR_FOUR;
|
||||
} else {
|
||||
accuracy = ZERO_FOUR;
|
||||
}
|
||||
return accuracy;
|
||||
}
|
||||
|
||||
public static byte[] FileTobyte(File file) {
|
||||
FileInputStream fileInputStream = null;
|
||||
byte[] imgData = null;
|
||||
|
||||
try {
|
||||
imgData = new byte[(int) file.length()];
|
||||
|
||||
//read file into bytes[]
|
||||
fileInputStream = new FileInputStream(file);
|
||||
fileInputStream.read(imgData);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
if (fileInputStream != null) {
|
||||
try {
|
||||
fileInputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return imgData;
|
||||
}
|
||||
|
||||
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);
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(fOut);
|
||||
fileOutputStream.write(bytes);
|
||||
fileOutputStream.close();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue