mirror of https://github.com/halo-dev/halo
👽 修改图片上传压缩方式
parent
a4d5d5252c
commit
a62b9f8df5
7
pom.xml
7
pom.xml
|
@ -156,6 +156,13 @@
|
||||||
<version>4.0.1</version>
|
<version>4.0.1</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 图片操作 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.coobird</groupId>
|
||||||
|
<artifactId>thumbnailator</artifactId>
|
||||||
|
<version>0.4.8</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<repositories>
|
<repositories>
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package cc.ryanc.halo;
|
package cc.ryanc.halo;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@ -7,9 +8,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
* @author RYAN0UP
|
* @author RYAN0UP
|
||||||
* SpringBoot启动类
|
* SpringBoot启动类
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class Application {
|
public class Application {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(Application.class, args);
|
SpringApplication.run(Application.class, args);
|
||||||
|
log.info("Halo started at http://localhost:8090");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import cc.ryanc.halo.utils.HaloUtils;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.extra.servlet.ServletUtil;
|
import cn.hutool.extra.servlet.ServletUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import net.coobird.thumbnailator.Thumbnails;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
@ -131,7 +132,7 @@ public class AttachmentController {
|
||||||
* @param request request
|
* @param request request
|
||||||
* @return Map<String , Object></>
|
* @return Map<String , Object></>
|
||||||
*/
|
*/
|
||||||
public Map<String, Object> uploadAttachment(MultipartFile file, HttpServletRequest request) {
|
private Map<String, Object> uploadAttachment(MultipartFile file, HttpServletRequest request) {
|
||||||
Map<String, Object> result = new HashMap<String, Object>();
|
Map<String, Object> result = new HashMap<String, Object>();
|
||||||
if (!file.isEmpty()) {
|
if (!file.isEmpty()) {
|
||||||
try {
|
try {
|
||||||
|
@ -151,25 +152,18 @@ public class AttachmentController {
|
||||||
String fileName = nameWithOutSuffix+"."+fileSuffix;
|
String fileName = nameWithOutSuffix+"."+fileSuffix;
|
||||||
file.transferTo(new File(mediaPath.getAbsoluteFile(), fileName));
|
file.transferTo(new File(mediaPath.getAbsoluteFile(), fileName));
|
||||||
|
|
||||||
|
//压缩图片
|
||||||
|
Thumbnails.of(new StringBuffer(mediaPath.getAbsolutePath()).append("/").append(fileName).toString()).size(256,256).keepAspectRatio(false).toFile(new StringBuffer(mediaPath.getAbsolutePath()).append("/").append(nameWithOutSuffix).append("_small.").append(fileSuffix).toString());
|
||||||
|
|
||||||
//保存在数据库
|
//保存在数据库
|
||||||
Attachment attachment = new Attachment();
|
Attachment attachment = new Attachment();
|
||||||
attachment.setAttachName(fileName);
|
attachment.setAttachName(fileName);
|
||||||
attachment.setAttachPath(new StringBuffer("/upload/").append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/").append(fileName).toString());
|
attachment.setAttachPath(new StringBuffer("/upload/").append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/").append(fileName).toString());
|
||||||
//判断图片大小,如果长宽都小于500,则保存原始图片路径
|
attachment.setAttachSmallPath(new StringBuffer("/upload/").append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/").append(nameWithOutSuffix).append("_small.").append(fileSuffix).toString());
|
||||||
BufferedImage sourceImg = ImageIO.read(new FileInputStream(mediaPath.getPath() + "/" + fileName));
|
|
||||||
if (sourceImg.getWidth() < 500 || sourceImg.getHeight() < 500) {
|
|
||||||
attachment.setAttachSmallPath(new StringBuffer("/upload/").append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/").append(fileName).toString());
|
|
||||||
} else {
|
|
||||||
attachment.setAttachSmallPath(new StringBuffer("/upload/").append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/").append(nameWithOutSuffix).append("_small.").append(fileSuffix).toString());
|
|
||||||
//剪裁图片
|
|
||||||
HaloUtils.cutCenterImage(new StringBuffer(mediaPath.getAbsolutePath()).append("/").append(fileName).toString(), new StringBuffer(mediaPath.getAbsolutePath()).append("/").append(nameWithOutSuffix).append("_small.").append(fileSuffix).toString(), 500, 500, fileSuffix);
|
|
||||||
}
|
|
||||||
|
|
||||||
attachment.setAttachType(file.getContentType());
|
attachment.setAttachType(file.getContentType());
|
||||||
attachment.setAttachSuffix(new StringBuffer(".").append(fileSuffix).toString());
|
attachment.setAttachSuffix(new StringBuffer(".").append(fileSuffix).toString());
|
||||||
attachment.setAttachCreated(DateUtil.date());
|
attachment.setAttachCreated(DateUtil.date());
|
||||||
attachmentService.saveByAttachment(attachment);
|
attachmentService.saveByAttachment(attachment);
|
||||||
|
|
||||||
updateConst();
|
updateConst();
|
||||||
log.info("上传文件[" + fileName + "]到[" + mediaPath.getAbsolutePath() + "]成功");
|
log.info("上传文件[" + fileName + "]到[" + mediaPath.getAbsolutePath() + "]成功");
|
||||||
logsService.saveByLogs(
|
logsService.saveByLogs(
|
||||||
|
@ -180,7 +174,8 @@ public class AttachmentController {
|
||||||
result.put("message", "上传成功!");
|
result.put("message", "上传成功!");
|
||||||
result.put("url", attachment.getAttachPath());
|
result.put("url", attachment.getAttachPath());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("未知错误:{0}", e.getMessage());
|
log.error("未知错误:", e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
result.put("success", 0);
|
result.put("success", 0);
|
||||||
result.put("message", "上传失败!");
|
result.put("message", "上传失败!");
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,10 +77,10 @@
|
||||||
<#break>
|
<#break>
|
||||||
<#case "gallery.ftl">
|
<#case "gallery.ftl">
|
||||||
图库页面
|
图库页面
|
||||||
|
<#break >
|
||||||
<#case "module/options.ftl">
|
<#case "module/options.ftl">
|
||||||
设置选项
|
设置选项
|
||||||
<#break >
|
<#break >
|
||||||
<#break>
|
|
||||||
</#switch>
|
</#switch>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
Loading…
Reference in New Issue