Commit 55f85b34 authored by cuijun's avatar cuijun

文件压缩250图片格式

parent 94cc08c1
......@@ -104,6 +104,37 @@ public class UploadService {
}
// public String uploadFile250(MultipartFile file, String prefix) throws Exception {
//// // 创建本日存放目录
//// DateTime now = DateTime.now();
//// String dirPathToday = "/" + prefix + "/" + now.toString(DEFAULT_DATE_TIME_FORMATTER);
//// String redisNoKey = RedisKey.UPLOAD_FILE_NO_PREFIX + now.toString(DEFAULT_DATE_TIME_FORMATTER);
//// Long no = redisTemplate.opsForValue().increment(redisNoKey);
//// if (no.equals(1L)) {
//// redisTemplate.expire(redisNoKey, 1, TimeUnit.DAYS);
//// }
//// String fileName = file.getOriginalFilename();
//// String realFileRelPath = dirPathToday + "/" + no + fileName.substring(fileName.lastIndexOf("."));
//// // 文件存放路径
//// String filePath = baseUploadPath + realFileRelPath;
//// // 将文件写入指定位置
//// FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath));
////
//// // 将上传的图片压缩成250*250的尺寸
//// BufferedImage originalImage = ImageIO.read(new File(filePath));
//// BufferedImage resizedImage = new BufferedImage(250, 250, originalImage.getType());
//// Graphics2D g = resizedImage.createGraphics();
//// g.drawImage(originalImage, 0, 0, 250, 250, null);
//// g.dispose();
////
//// // 保存压缩后的图片
//// String compressedFilePath = baseUploadPath + dirPathToday + no + ".jpg_250";
//// ImageIO.write(resizedImage, "jpg", new File(compressedFilePath));
////
//// realFileRelPath = xx_url + SystemConfig.XXMP_URL + realFileRelPath;
//// return realFileRelPath;
//// }
public String uploadFile250(MultipartFile file, String prefix) throws Exception {
// 创建本日存放目录
DateTime now = DateTime.now();
......@@ -120,15 +151,28 @@ public class UploadService {
// 将文件写入指定位置
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(filePath));
// 将上传的图片压缩成250*250的尺寸
// 将上传的图片等比例缩放至250x250尺寸
BufferedImage originalImage = ImageIO.read(new File(filePath));
BufferedImage resizedImage = new BufferedImage(250, 250, originalImage.getType());
int originalWidth = originalImage.getWidth();
int originalHeight = originalImage.getHeight();
double scaleFactor = 1.0;
if (originalWidth > originalHeight) {
scaleFactor = (double) 250 / originalWidth;
} else {
scaleFactor = (double) 250 / originalHeight;
}
int newWidth = (int) (originalWidth * scaleFactor);
int newHeight = (int) (originalHeight * scaleFactor);
BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, originalImage.getType());
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, 250, 250, null);
g.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
g.dispose();
// 保存压缩后的图片
String compressedFilePath = baseUploadPath + dirPathToday + "/compressed_" + no + ".jpg";
String compressedFilePath = baseUploadPath + dirPathToday + no + ".jpg_250";
ImageIO.write(resizedImage, "jpg", new File(compressedFilePath));
realFileRelPath = xx_url + SystemConfig.XXMP_URL + realFileRelPath;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment