依赖

  <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. Codeforces 1392H - ZS Shuffles Cards(DP+打表找规律)

    Codeforces 题面传送门 & 洛谷题面传送门 真·两天前刚做过这场的 I 题,今天模拟赛就考了这场的 H 题,我怕不是预言带师 提供一种奇怪的做法,来自于同机房神仙们,该做法不需要 M ...

  2. Codeforces 1491G - Switch and Flip(构造题)

    Codeforces 题目传送门 & 洛谷题目传送门 obviously,难度高一点的构造题对我来说都是不可做题 首先考虑将排列拆成一个个置换环,也就是 \(\forall i\) 连边 \( ...

  3. fluidity详解

    fluidity详解 1.fluidity编译过程 1.1.femtools库调用方法 编译fluidity/femtools目录下所有文件,打包为libfemtools.a静态库文件: 通过-lfe ...

  4. dlang ref的作用

    ref 作用 1 import std.stdio, std.string; 2 3 void main() 4 { 5 string[] color=["red","b ...

  5. ubuntu终端ls颜色配置

    buntu中没有LS_COLORS,/etc/目录中也没有DIR_COLORS,所以这里使用dircolor命令加以解决 1. 利用dircolors命令,查看我们的系统当前的文件名称显示颜色的值,然 ...

  6. 19.Happy Number-Leetcode

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  7. Flume消费内外网分流配置的Kafka时遇到的坑

    网上有铺天盖地的文章,介绍如何将Kafka同时配置成公网地址.内网地址,以实现内外网分流,看着都很成功. 但我们通过Flume消费一个配置了内外网分流的Kafka(版本0.10.1)集群时遇到了坑,却 ...

  8. 作业帮上万个 CronJob 和在线业务混部,如何解决弱隔离问题并进一步提升资源利用率?

    作者 吕亚霖,作业帮基础架构 - 架构研发团队负责人.负责技术中台和基础架构工作.在作业帮期间主导了云原生架构演进.推动实施容器化改造.服务治理.GO 微服务框架.DevOps 的落地实践. 别路,作 ...

  9. LeetCode382-链表随机节点

    原题链接:[382. 链表随机节点]:https://leetcode-cn.com/problems/linked-list-random-node/ 题目描述: 给定一个单链表,随机选择链表的一个 ...

  10. Properties类继承HashTable类,一般用来给程序配置属性文件。

    package com.itcast.demo04.Prop;import jdk.internal.util.xml.impl.ReaderUTF8;import sun.nio.cs.UTF_32 ...