一:创建一个springboot的maven项目包
新建:File–new–project
下载1.8jdk 以及 Maven 我这里下载的是 Maven 3.6.1
GroupID是项目组织唯一的标识符:一般书写格式:com.公司.项目名
ArtifactID就是项目的唯一的标识符:一般书写格式:项目名
需要提前配置maven 还要修改settings.xml文件 这里就不多说了
二:src里面写我们的去水印源码
这是我new好的文件 在这里我就不多做解释了
在JsonUtil这个java文件里代码内容如下
package com.rjt.common.util;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON;
public class JsonUtil {
/**
* 将json格式化为字符串,然后根据key取值
* @param jsonStr
* @param key
* @return
*/
public static String getJsonValue(String jsonStr,String key) {
if(StringUtils.isEmpty(jsonStr) || StringUtils.isEmpty(key)) {
return "";
}
if(key.indexOf(".")==-1) {
if(key.indexOf("[")!=-1) {
int num =Integer.parseInt(TextUtil.getSubString(key, "[", "]"));
key=key.substring(0, key.indexOf("["));
return JSON.parseObject(jsonStr).getJSONArray(key).getString(num);
}else {
return JSON.parseObject(jsonStr).getString(key);
}
}else {
String[] keys = key.split("\\.");
for(int i=0;i<keys.length;i++) {
String tempKey=keys[i];
if(tempKey.indexOf("[")!=-1) {
int num =Integer.parseInt(TextUtil.getSubString(tempKey, "[", "]"));
tempKey=tempKey.substring(0, keys[i].indexOf("["));
jsonStr=JSON.parseObject(jsonStr).getJSONArray(tempKey).getString(num);
}else {
jsonStr=JSON.parseObject(jsonStr).getString(tempKey);
}
}
return jsonStr;
}
}
}
TextUtilde
package com.rjt.common.util;
import blade.kit.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Random;
public class TextUtil {
private static Logger logger = LoggerFactory.getLogger(TextUtil.class);
/**
* 取两个文本之间的文本值
*
* @param text
* @param left
* @param right
* @return
*/
public static String getSubString(String text, String left, String right) {
String result = "";
int zLen;
if (left == null || left.isEmpty()) {
zLen = 0;
} else {
zLen = text.indexOf(left);
if (zLen > -1) {
zLen += left.length();
} else {
zLen = 0;
}
}
int yLen = text.indexOf(right, zLen);
if (yLen < 0 || right == null || right.isEmpty()) {
yLen = text.length();
}
result = text.substring(zLen, yLen);
return result;
}
/**
* 获取ip
*
* @param request
* @return
*/
public static String getRemortIP(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
// 多次反向代理后会有多个ip值,第一个ip才是真实ip
if( ip.indexOf(",")!=-1 ){
ip = ip.split(",")[0];
}
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
/**
* 根据ip获取地址
*
* @param
* @return
* @throws UnsupportedEncodingException
*/
public static String getPlace(String ip) throws UnsupportedEncodingException {
if (ip.indexOf(", ") != -1) {
ip = ip.split(", ")[1];
}
String url = "https://ip.cn/index.php?ip=" + ip;
HttpRequest request = HttpRequest.get(url, true).header("User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0");
String res = request.body();
String place = TextUtil.getSubString(res, "置:<code>", "</code></p><p>");
request.disconnect();
return place;
}
/**
* 根据地址获取纬度
*
* @param
* @return
* @throws UnsupportedEncodingException
*/
public static String getWd(String place) throws UnsupportedEnco