依赖

  <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. P6585 中子衰变

    我们偶数的时候只要对称操作. 奇数的话,我们定义\(1\)的相反数为\(-1\),\(0\)相反数为\(0\). 我们维护最长的两边的相反串,中间一段除了一个端点,其他均被染成同色. 那么如果对方染端 ...

  2. FJD1T3(LOJ 2773 学习轨迹)

    发现了\(FJOI\)原题 没什么想法,想到自己考场上连\(n^2\)做法都不会就很感慨. 考虑如果只选择一个序列的任务,那么肯定全部选择会更加优秀. 那么考虑如果我们选择了两个序列的一部分. 如果\ ...

  3. 各种多项式操作的 n^2 递推

    zszz,使用 NTT 可以在 \(\mathcal O(n\log n)\) 的时间内求出两个多项式的卷积.以及一个多项式的 \(\text{inv},\ln,\exp,\text{sqrt}\) ...

  4. Nginx 动态增加扩展

    Nginx 动态增加扩展 1. 先查看目前nginx已加载模块 /home/nginx-1.18.0 # nginx -V nginx version: nginx/1.18.0 built by g ...

  5. Docker将容器制作成镜像并提交到远程仓库

    Docker将容器制作成镜像并提交到远程仓库 步骤如下 先在dockerhub上创建一个自己的用户https://hub.docker.com/.或者在阿里云也可以. 2. 然后先创建一个空的镜像名. ...

  6. 8核cpu,,负载

    今天有一个电话面试,面试官问我:CentOS怎么查看CPU负载?我说:看top的第一行有load average.面试官又问:为什么从这就判定是负载高呢?依据是什么呢?然后... 然后我就尴尬了,挂了 ...

  7. Golang gRPC调试工具

    目录 Golang gRPC调试工具 1. 命令行工具 grpcurl 1.1 安装 1.2 验证 1.3 注册反射 1.4 使用示例 2. web调试工具grpcui 2.1 安装 2.2 验证 2 ...

  8. 26-Palindrome Number

    回文数的判定,不使用额外的空间 Determine whether an integer is a palindrome. Do this without extra space. 思路:将一个整数逆 ...

  9. java中接口可以继承接口

    今天阅读别人的代码才发现,接口是可以继承接口的 一个类只能extends一个父类,但可以implements多个接口. 一个接口则可以同时extends多个接口,却不能implements任何接口. ...

  10. 使用flock命令查看nas存储是否支持文件锁

    上锁 文件锁有两种 shared lock 共享锁 exclusive lock 排他锁 当文件被上了共享锁之后,其他进程可以继续为此文件加共享锁,但此文件不能被加排他锁,此文件会有一个共享锁计数,加 ...