👽 修改图片上传压缩方式

pull/18/head
ruibaby 2018-06-23 15:22:00 +08:00
parent a4d5d5252c
commit a62b9f8df5
4 changed files with 19 additions and 14 deletions

View File

@ -156,6 +156,13 @@
<version>4.0.1</version>
</dependency>
<!-- 图片操作 -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
</dependencies>
<repositories>

View File

@ -1,5 +1,6 @@
package cc.ryanc.halo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@ -7,9 +8,11 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @author RYAN0UP
* SpringBoot
*/
@Slf4j
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
log.info("Halo started at http://localhost:8090");
}
}

View File

@ -11,6 +11,7 @@ import cc.ryanc.halo.utils.HaloUtils;
import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.servlet.ServletUtil;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
@ -131,7 +132,7 @@ public class AttachmentController {
* @param request request
* @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>();
if (!file.isEmpty()) {
try {
@ -151,25 +152,18 @@ public class AttachmentController {
String fileName = nameWithOutSuffix+"."+fileSuffix;
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.setAttachName(fileName);
attachment.setAttachPath(new StringBuffer("/upload/").append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/").append(fileName).toString());
//判断图片大小如果长宽都小于500则保存原始图片路径
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.setAttachSmallPath(new StringBuffer("/upload/").append(HaloUtils.YEAR).append("/").append(HaloUtils.MONTH).append("/").append(nameWithOutSuffix).append("_small.").append(fileSuffix).toString());
attachment.setAttachType(file.getContentType());
attachment.setAttachSuffix(new StringBuffer(".").append(fileSuffix).toString());
attachment.setAttachCreated(DateUtil.date());
attachmentService.saveByAttachment(attachment);
updateConst();
log.info("上传文件[" + fileName + "]到[" + mediaPath.getAbsolutePath() + "]成功");
logsService.saveByLogs(
@ -180,7 +174,8 @@ public class AttachmentController {
result.put("message", "上传成功!");
result.put("url", attachment.getAttachPath());
} catch (Exception e) {
log.error("未知错误:{0}", e.getMessage());
log.error("未知错误:", e.getMessage());
e.printStackTrace();
result.put("success", 0);
result.put("message", "上传失败!");
}

View File

@ -77,10 +77,10 @@
<#break>
<#case "gallery.ftl">
图库页面
<#break >
<#case "module/options.ftl">
设置选项
<#break >
<#break>
</#switch>
</td>
</tr>