1.安装依赖
yum install -y openssl-devel libevent-devel libjpeg-devel giflib-devel libpng-devel libwebp-devel ImageMagick-devel libmemcached-devel
2.下载:zimg-3.1.0-Release.x86_64.rpm
wget https://github.com/buaazp/zimg/releases/download/v3.1.0/zimg-3.1.0-Release.x86_64.rpm
3.安装
sudo rpm -ivh zimg-3.1.0-Release.x86_64.rpm
4.运行:
/usr/local/zimg/zimg /usr/local/zimg/conf/zimg.lua
4.开机启动:
chmod +x /etc/rc.d/rc.local
vim /etc/rc.local
//加如下行
/usr/local/zimg/zimg /usr/local/zimg/conf/zimg.lua *
#### 5.关闭防火墙:
systemctl stop firewalld.service
systemctl disable firewalld.service
#### 6.浏览器打开:http://server-ip:4869
![无标题.png](https://upload-images.jianshu.io/upload_images/7848013-a06ea692ffaf1ee0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
####7.java上传工具类
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
/**
@类说明 Zimg图片服务器专用上传工具类
@author 高振中
@创建时间:2019年2月27日 下午4:07:03
*/
@Component
@Slf4j
public class ZimgUtils {
@Value("${xsrt.zimg.server}")
private String zimg_server;// Zimg图片服务器IP及端口
/**
@方法说明 上传文件
*/
public JSONObject upFile(MultipartFile file) {
try {
return this.upFile(file.getBytes());
} catch (IOException e) {
log.error("上传图片时出现异常:", e.getMessage());
return JSONObject.parseObject("{"ret":false,"info":"上传图片时出现异常"}");
}
}
public JSONObject upUrlImg(String url) {
return upFile(getFileBytes(url));
}
/**
@方法说明 上传文件
*/
public JSONObject upFile(byte[] bytes) {
try {
URL url = new URL(zimg_server + "upload");
URLConnection connection = url.openConnection();
HttpURLConnection conn = (HttpURLConnection) connection;
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", extName(bytes));
conn.setDoOutput(true);
conn.setDoInput(true);
conn.connect();
conn.getOutputStream().write(bytes);
String res = new BufferedReader(new InputStreamReader(conn.getInputStream())).readLine();
conn.disconnect();
return JSONObject.parseObject(res);
} catch (Exception e) {
log.error("上传图片时出现异常:", e.getMessage());
return JSONObject.parseObject("{"ret":false,"info":"上传图片时出现异常"}");
}
}
/**
@方法说明 取扩展名
*/
private String extName(byte[] data) {
if (data[1] == 'P' && data[2] == 'N' && data[3] == 'G') {
return "png";
}
if (data[0] == 'G' && data[1] == 'I' && data[2] == 'F') {
return "gif";
}
if (data[6] == 'J' && data[7] == 'F' && data[8] == 'I' && data[9] == 'F') {
return "jpg";
}
return "jpg";
}
/**
@方法说明 从url读取文件流
*/
public byte[] getFileBytes(String imgUrl) {
ByteArrayOutputStream swapStream = null;
try {
URL imgurl = new URL(imgUrl);
URLConnection connection = imgurl.openConnection();
InputStream inStream = connection.getInputStream();
swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = inStream.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return swapStream.toByteArray();
}
public static void main(String[] args) {
String res = "{"ret":true,"info":{"md5":"bee092fa4a73d82aa125c74f4a8ce96d","size":66468}}";
JSONObject json = JSONObject.parseObject(res);
System.out.println(json.getJSONObject("info").get("md5"));
}
}