提交 897fcf5a 编写于 作者: lakernote's avatar lakernote

(add)[整体](增加缩略图接口)

上级 630f4468
......@@ -161,6 +161,12 @@
<artifactId>nginxparser</artifactId>
<version>0.9.6</version>
</dependency>
<!-- 图片处理 -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.14</version>
</dependency>
</dependencies>
<build>
......
......@@ -34,7 +34,8 @@ public class WebMvcConfig implements WebMvcConfigurer {
"/error",
"/swagger-resources/**",
"/api/v1/login",
"/captcha");
"/captcha",
"/thumbnail");
}
@Override
......
package com.laker.admin.framework.utils;
import net.coobird.thumbnailator.Thumbnails;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
/**
* @author laker
* 图片处理类
*/
public class EasyImageUtils {
/**
* 按照宽高尺寸压缩图片,
*
* @param sourceUrl 图片url
* @param out 输出的流 预览流
* @param width
* @param height
* @throws IOException
*/
public static void compressBysize(String sourceUrl, OutputStream out, int width, int height) throws IOException {
Thumbnails
.of(new URL(sourceUrl))
.width(width)
.height(height)
.toOutputStream(out);
}
public static void scale(String source, String out, double scale) throws IOException {
Thumbnails
.of(source)
.scale(scale)
.size(100, 100)
.toFile(out);
}
}
package com.laker.admin.module.sys.controller;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.IdUtil;
import com.laker.admin.framework.model.Response;
import com.laker.admin.framework.cache.ICache;
import com.laker.admin.framework.model.Response;
import com.laker.admin.framework.utils.EasyImageUtils;
import com.wf.captcha.ArithmeticCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* /admin/** 无需check login
*/
......@@ -50,4 +58,31 @@ public class IndexController {
return Response.ok(Dict.create().set("uid", uid).set("image", captcha.toBase64()));
}
/**
* 缩略图
* http://localhost:8080/thumbnail?url=http://localhost:8080/admin/admin/images/wx.jpg
*/
@GetMapping("/thumbnail")
public void thumbnail(String url, HttpServletResponse response,
@RequestParam(required = false, defaultValue = "1") int type,
@RequestParam(required = false, defaultValue = "100") int width,
@RequestParam(required = false, defaultValue = "100") int height) throws IOException {
OutputStream out = new BufferedOutputStream(response.getOutputStream());
switch (type) {
case 1: // 预览
response.setContentType("image/" + FileUtil.getSuffix(url) + "; charset=utf-8");
break;
case 2: // 下载
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(FileUtil.mainName(url).getBytes("utf-8"), "iso-8859-1") + "." + FileUtil.getSuffix(url));
response.setContentType("application/octet-stream");
break;
default:
response.setContentType("image/" + FileUtil.getSuffix(url) + "; charset=utf-8");
}
EasyImageUtils.compressBysize(url, out, width, height);
}
}
\ No newline at end of file
package com.laker.admin.service;
import net.coobird.thumbnailator.Thumbnails;
import java.io.IOException;
public class EasyImageUtilsTest {
public static void main(String[] args) throws IOException {
size("d://1.jpg", "d://laker_200x300.jpg");
}
public static void size(String source, String out) throws IOException {
Thumbnails
.of(source)
.size(100, 100)
.toFile(out);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册