SpringBoot整合zimg图片服务器
依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.75</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpmime</artifactId> </dependency>
也有
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
yml配置
zimg:
server: http://192.168.80.135:4869
ZimgConfig.java
import lombok.Data; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; @Data @Configuration public class ZimgConfig { @Value("${zimg.server}") private String zimgServer; }
ZimgUtils.java
import com.example.zimg.config.ZimgConfig; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ByteArrayEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import javax.annotation.PostConstruct; /** * zimg工具类 * */ @Component @Slf4j public class ZimgUtils { @Autowired private ZimgConfig zimgConfig; public static ZimgUtils zimgUtils; /** * 初始化minio配置 */ @PostConstruct public void init() { zimgUtils = this; zimgUtils.zimgConfig = this.zimgConfig; } private static final String uploadPath = "/upload"; private static final String deletePath = "/admin"; /** * 成功返回: * { * "ret": true, * "info": { * "md5": "a48998d3095079c71d1c72054b847dcf", * "size": 110823 * } * } * <p> * 失败返回: * { * "ret": false, * "error": { * "code": 5, * "message": "Content-Length error." * } * } * * @param multipartFile * @return * @throws Exception */ public String uploadImage(MultipartFile multipartFile) throws Exception { String url = zimgConfig.getZimgServer() + uploadPath; String fileName = multipartFile.getOriginalFilename(); String ext = fileName.substring(fileName.lastIndexOf(".") + 1); //创建post请求对象 HttpPost post = new HttpPost(url); //文件类型 post.addHeader("Content-Type", ext.toLowerCase()); ByteArrayEntity byteArrayEntity = null; byteArrayEntity = new ByteArrayEntity(multipartFile.getBytes()); post.setEntity(byteArrayEntity); CloseableHttpClient client = HttpClients.createDefault(); //启动执行请求,并获得返回值 CloseableHttpResponse response = client.execute(post); //得到返回的entity对象 HttpEntity entity = response.getEntity(); //把实体对象转换为string String result = EntityUtils.toString(entity, "UTF-8"); return result; } /** * 需要zimg的配置开启权限 * 修改配置文件 zimg.lua 修改 admin_rule='allow 127.0.0.1' =》 admin_rule='allow all' */ public void deleteImage(String md5) throws Exception { String url = zimgConfig.getZimgServer() + deletePath; //创建URLBuilder URIBuilder uriBuilder = new URIBuilder(url); //设置参数 uriBuilder.setParameter("md5", md5); uriBuilder.setParameter("t", "1"); HttpGet httpGet = new HttpGet(uriBuilder.build()); CloseableHttpClient client = HttpClients.createDefault(); //启动执行请求,并获得返回值 CloseableHttpResponse response = client.execute(httpGet); //得到返回的entity对象 HttpEntity entity = response.getEntity(); //把实体对象转换为string 这返回的html页面代码,,没找到json方式 String result = EntityUtils.toString(entity, "UTF-8"); log.info(result); } }
使用
import com.alibaba.fastjson.JSONObject; import com.example.zimg.utils.ZimgUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; /** * demo控制器 */ @RestController @Slf4j public class ZimgController { @Autowired private ZimgUtils zimgUtils; /** * 上传附件 * @param file * @return */ @PostMapping(value = "/upload") public void uploadImage(MultipartFile file) { try { if (file.isEmpty()) { log.error(">>>>>>>>>>>>>>文件为空"); return; } String s = zimgUtils.uploadImage(file); JSONObject object = JSONObject.parseObject(s); if (object.getBoolean("ret")){ //成功 JSONObject info = object.getJSONObject("info"); log.info(">>>>>>>>> 文件md5:{},文件大小:{}",info.get("md5"),info.get("size")); }else { //失败 JSONObject error = object.getJSONObject("error"); log.error(">>>>>>>> 文件上传失败:{}",error.get("message")); } } catch (Exception e) { e.printStackTrace(); } } /** * 删除文件 * @param md5 上传成功后返回的md5 * @return */ @DeleteMapping(value = "/delete") public void deleteImage(String md5) { try { zimgUtils.deleteImage(md5); } catch (Exception e) { e.printStackTrace(); } } }
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2020-11-26 JAVA发送POST请求携带JSON格式字符串参数
2020-11-26 Spring Boot整合Thymeleaf及Thymeleaf页面基本语法,遍历、拼接、判空、日期格式化、显示部分字符串
2019-11-26 PC点击链接打开QQ聊天窗口
2019-11-26 JS验证身份证是否符合规则