依赖

  <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();
}
}
}

SpringBoot整合zimg图片服务器的更多相关文章

  1. 分布式文件系统FastDFS简介、搭建、与SpringBoot整合实现图片上传

    之前大学时搭建过一个FastDFS的图片服务器,当时只是抱着好奇的态度搭着玩一下,当时搭建采用了一台虚拟机,tracker和storage服务在一台机器上放着,最近翻之前的博客突然想着在两台机器上搭建 ...

  2. Linux使用docker安装zimg图片服务器

    官方地址:http://zimg.buaa.us/ 配置文件 zimg.lua --zimg server config --server config --是否后台运行 is_daemon = 1 ...

  3. springboot整合ueditor实现图片上传和文件上传功能

    springboot整合ueditor实现图片上传和文件上传功能 写在前面: 在阅读本篇之前,请先按照我的这篇随笔完成对ueditor的前期配置工作: springboot+layui 整合百度富文本 ...

  4. 高性能图片服务器–ZIMG

    2011年李彦宏在百度联盟峰会上就提到过互联网的读图时代已经到来1,图片服务早已成为一个互联网应用中占比很大的部分,对图片的处理能力也相应地变成企业和开发者的一项基本技能.需要处理海量图片的典型应用有 ...

  5. springboot整合thumbnailator实现图片压缩

    springboot整合thumbnailator实现图片压缩 前言 最近由于首页产品列表图片显示太慢,经过研究发现是用户上传的图片太大. 针对这个问题,想到的解决方案是: 1. 产品上传时,限定图片 ...

  6. 高性能图片服务器–ZIMG(转)

    2011年李彦宏在百度联盟峰会上就提到过互联网的读图时代已经到来1,图片服务早已成为一个互联网应用中占比很大的部分,对图片的处理能力也相应地变成企业和开发者的一项基本技能.需要处理海量图片的典型应用有 ...

  7. Zimg—轻量级图片服务器搭建利器

    在一个互联网应用中,图片扮演着越来越重要的角色.有稳定的可扩展的图片存储服务器就显得尤为的重要,云厂商们提供了便利的图片存储服务,花钱就可以解决了.这里简单介绍一个开源的一个分布式图片存储服务器--z ...

  8. summernote 上传图片到图片服务器的解决方案(springboot 成功)

    遇到的可以连接成功但是拒绝登录的问题 前提说一下,我自己在自己的服务器上配置了nginx的反向代理,所以请求的时候才会直接写的是我的ip地址,要配置nginx的话,可以看我的nginx的笔记 当代码感 ...

  9. SpringBoot整合Redis及Redis工具类撰写

            SpringBoot整合Redis的博客很多,但是很多都不是我想要的结果.因为我只需要整合完成后,可以操作Redis就可以了,并不需要配合缓存相关的注解使用(如@Cacheable). ...

随机推荐

  1. 洛谷 P5048 - [Ynoi2019 模拟赛] Yuno loves sqrt technology III(分块)

    题面传送门 qwq 感觉跟很多年前做过的一道题思路差不多罢,结果我竟然没想起那道题?!!所以说我 wtcl/wq 首先将 \(a_i\) 离散化. 如果允许离线那显然一遍莫队就能解决,复杂度 \(n\ ...

  2. 【R shiny】一些应用记录

    目录 DT和downloadButton应用 downloadButton 中验证结果输出 添加进度条 如何确保仅在使用Shiny按下操作按钮时才触发操作 其他 DT和downloadButton应用 ...

  3. 【Linux】tmux安装(非root)及其使用

    tmux(terminal multiplexer)是Linux上的终端复用神器. 1. 安装 (1)下载 下载及其依赖软件. wget -c https://github.com/tmux/tmux ...

  4. 54. Flatten Binary Tree to Linked List

    Flatten Binary Tree to Linked List My Submissions QuestionEditorial Solution Total Accepted: 81373 T ...

  5. Python查找最长回文暴力方法

    查找最长回文子串 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 例如1: 输入: "babad" 输出: "bab" ...

  6. DOM给表格添加新一行和删除整个行的内容

    DOM用appendChild()给表格添加新一行时,要注意,在HTML中没特别设置<thead>,<tbody>时,会自动添加上,所以要选择表格第一个元素在添加tr. // ...

  7. 巩固java第五天

    巩固内容: HTML 实例解析 <p> 元素: <p>这是第一个段落.</p> 这个 <p> 元素定义了 HTML 文档中的一个段落. 这个元素拥有一个 ...

  8. 巩固javaweb第三天

    巩固内容: HTML 标题 HTML 标题(Heading)是通过<h1> - <h6> 标签来定义的. HTML 段落 HTML 段落是通过标签 <p> 来定义的 ...

  9. LeetCode子矩形查询

    LeetCode 子矩形查询 题目描述 请你实现一个类SubrectangleQueries,它的构造函数的参数是一个rows * cols的矩形(这里用整数矩阵表示),并支持以下两种操作: upda ...

  10. 线性表A,B顺序存储合并

    线性表A,B顺序存储合并 有两张非递增有序的线性表A,B,采用顺序存储结构,两张表合并用c表存,要求C为非递减有序的,然后删除C表中值相同的多余元素.元素类型为整型 输入格式: 第一行输入输入表A的各 ...