添加520业务支持向redis中存放文件
This commit is contained in:
@@ -620,6 +620,15 @@ public enum RestBusinessCode {
|
||||
* Redis关联关系库中未找到配置对应的redisdb信息
|
||||
*/
|
||||
RedisDBRelationNotExistsInRedis(5003005,"Redis关联关系库中未找到配置对应的redisdb信息"),
|
||||
/**
|
||||
* 保存文件到redis发生了异常
|
||||
*/
|
||||
SaveFileToRedisError(5003006,"保存文件到redis发生了异常"),
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Map中缺少编译配置信息
|
||||
|
||||
50
src/main/java/com/nis/util/File2Redis.java
Normal file
50
src/main/java/com/nis/util/File2Redis.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package com.nis.util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
import com.nis.restful.RestBusinessCode;
|
||||
import com.nis.restful.ServiceRuntimeException;
|
||||
|
||||
import redis.clients.jedis.Transaction;
|
||||
|
||||
public class File2Redis {
|
||||
|
||||
public static String file2Redis(String url, Transaction transaction) {
|
||||
try {
|
||||
byte[] fileByte = downLoadFromUrl(url);
|
||||
String md5 = MD5Utils.getMd5ByIS(new ByteArrayInputStream(fileByte));
|
||||
String key = "redis://_FILE_" + md5;
|
||||
transaction.set(key.getBytes(), fileByte);
|
||||
return key;
|
||||
} catch (Exception e) {
|
||||
throw new ServiceRuntimeException("保存文件到redis发生了异常:" + e.getMessage(),
|
||||
RestBusinessCode.SaveFileToRedisError.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] downLoadFromUrl(String urlStr) throws IOException {
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
// 设置超时间为30秒
|
||||
conn.setConnectTimeout(3 * 10000);
|
||||
// 防止屏蔽程序抓取而返回403错误
|
||||
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
||||
// 得到输入流
|
||||
InputStream inputStream = conn.getInputStream();
|
||||
ByteArrayOutputStream baosOutputStream = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int len;
|
||||
while ((len = inputStream.read(buffer)) > -1) {
|
||||
baosOutputStream.write(buffer, 0, len);
|
||||
}
|
||||
baosOutputStream.flush();
|
||||
return baosOutputStream.toByteArray();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -3,41 +3,29 @@
|
||||
*/
|
||||
package com.nis.util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.lang3.StringEscapeUtils;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
|
||||
import com.ckfinder.connector.ServletContextFactory;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.nis.web.service.SpringContextHolder;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
/**
|
||||
* 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
|
||||
* @author ThinkGem
|
||||
* @version 2013-05-22
|
||||
*/
|
||||
public class MD5Utils{
|
||||
public class MD5Utils {
|
||||
|
||||
/**
|
||||
* 字符串转为MD532位小写
|
||||
* @param plain
|
||||
* @return
|
||||
*/
|
||||
public static String md5LowerCase(String plain) throws Exception{
|
||||
public static String md5LowerCase(String plain) throws Exception {
|
||||
String re_md5 = new String();
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
md.update(plain.getBytes());
|
||||
@@ -58,26 +46,52 @@ public class MD5Utils{
|
||||
re_md5 = buf.toString();
|
||||
return re_md5;
|
||||
}
|
||||
public static byte[] createChecksum(String filename) throws IOException, NoSuchAlgorithmException{
|
||||
InputStream fis=new FileInputStream(filename);
|
||||
byte[] buffer=new byte[1024];
|
||||
MessageDigest complete=MessageDigest.getInstance("MD5");
|
||||
|
||||
public static byte[] createChecksum(String filename) throws IOException, NoSuchAlgorithmException {
|
||||
InputStream fis = new FileInputStream(filename);
|
||||
byte[] buffer = new byte[1024];
|
||||
MessageDigest complete = MessageDigest.getInstance("MD5");
|
||||
int numRead;
|
||||
do{
|
||||
numRead=fis.read(buffer);
|
||||
if(numRead>0){
|
||||
complete.update(buffer,0,numRead);
|
||||
do {
|
||||
numRead = fis.read(buffer);
|
||||
if (numRead > 0) {
|
||||
complete.update(buffer, 0, numRead);
|
||||
}
|
||||
}while(numRead!=-1);
|
||||
} while (numRead != -1);
|
||||
fis.close();
|
||||
return complete.digest();
|
||||
}
|
||||
public static String getMD5Checksum(String filename) throws NoSuchAlgorithmException, IOException{
|
||||
byte[] b=createChecksum(filename);
|
||||
StringBuffer result=new StringBuffer();
|
||||
for(int i=0;i<b.length;i++){
|
||||
result.append(Integer.toString((b[i] & 0xff)+ 0x100,16).substring(1));
|
||||
|
||||
public static String getMD5Checksum(String filename) throws NoSuchAlgorithmException, IOException {
|
||||
byte[] b = createChecksum(filename);
|
||||
StringBuffer result = new StringBuffer();
|
||||
for (int i = 0; i < b.length; i++) {
|
||||
result.append(Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1));
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
|
||||
long start1 = System.currentTimeMillis();
|
||||
String md5Checksum = getMd5ByIS(new FileInputStream("f:\\1.iso"));
|
||||
System.out.println(md5Checksum);
|
||||
long start2 = System.currentTimeMillis();
|
||||
System.out.println("第一次用时" + (start2 - start1));
|
||||
String md5Checksum1 = getMD5Checksum("f:\\1.iso");
|
||||
long start3 = System.currentTimeMillis();
|
||||
System.out.println(md5Checksum1);
|
||||
System.out.println("第二次用时" + (start3 - start2));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 这种方式计算大文件时更快一些
|
||||
* @param fis
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @throws NoSuchAlgorithmException
|
||||
*/
|
||||
public static String getMd5ByIS(InputStream is) throws IOException, NoSuchAlgorithmException {
|
||||
return DigestUtils.md5Hex(is);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.nis.domain.restful.MaatConfig;
|
||||
import com.nis.restful.RestBusinessCode;
|
||||
import com.nis.restful.ServiceRuntimeException;
|
||||
import com.nis.util.Configurations;
|
||||
import com.nis.util.File2Redis;
|
||||
import com.nis.util.JedisUtils;
|
||||
import com.nis.util.ReadMaatXmlUtil;
|
||||
import com.nis.util.ServiceAndRDBIndexReal;
|
||||
@@ -89,10 +90,36 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
|
||||
}
|
||||
StringBuffer valBF = new StringBuffer();
|
||||
String[] valSplit = maatXmlExpr.getValueExpression().split(";");
|
||||
List<String> urlList = new ArrayList<String>();
|
||||
for (String valStr : valSplit) {
|
||||
if (!StringUtils.isEmpty(valStr) && valStr.trim().startsWith("[")) {
|
||||
valStr = valStr.trim().replace("[", "").replace("]", "");
|
||||
// if (service == 520) {
|
||||
// if (valStr.equals("private_key_file")
|
||||
// || valStr.equals("public_key_file")) {
|
||||
// String fileUrl = map.get(valStr);
|
||||
// urlList.add(fileUrl);
|
||||
// }
|
||||
// }
|
||||
// if(valStr.equals("op_time")) {
|
||||
// if (urlList.size() > 0) {
|
||||
// for (String fileUrl : urlList) {
|
||||
// String key = File2Redis.file2Redis(fileUrl, transaction);
|
||||
// logger.info("向{}号redis数据库添加了一条文件配置,key是{}",redisDBIndex,key);
|
||||
// valBF.append(key);
|
||||
// valBF.append("\t");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
if(valStr.contains("redisFile_")) {
|
||||
String key = File2Redis.file2Redis(map.get(valStr), transaction);
|
||||
logger.info("向{}号redis数据库添加了一条文件配置,key是{}",redisDBIndex,key);
|
||||
valBF.append(key);
|
||||
//valBF.append("\t");
|
||||
}else {
|
||||
valBF.append(map.get(valStr));
|
||||
}
|
||||
} else if (valStr.equals(" ")) {
|
||||
valBF.append(" ");
|
||||
} else if (valStr.equals("\\t")) {// xml中是字符串的\t这里判断的时候需要转义为\\t,但是添加的时候需要添加\t不是\\t
|
||||
@@ -270,7 +297,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
|
||||
&& redisStatisticsRealDBIndex < Configurations.getIntProperty("maxRedisDBIndex", 16)) {
|
||||
List<MaatConfig> list = configMap.get(redisDBIndex);
|
||||
if (list != null && list.size() > 0) {
|
||||
transaction.select(redisStatisticsRealDBIndex);//选择实时统计库
|
||||
transaction.select(redisStatisticsRealDBIndex);// 选择实时统计库
|
||||
String redisStatisticsReal = Configurations.getStringProperty("redis-statisticsReal",
|
||||
"[COMPILE_ID];\t;[SERVICE];\t;[ACTION];\t;[CONT_TYPE];\t;[ATTR_TYPE];\t;[CONT_LABEL];\t;[TASK_ID];\t;[AFFAIR_ID];\t;[DO_BLACKLIST];\t;[DO_LOG];\t;[EFFECTIVE_RANGE];\t;[START_TIME];\t;[END_TIME];\t;[USER_REGION];\t;[IS_VALID];\t;[GROUP_NUM];\t;[FATHER_CFG_ID];\t;[OP_TIME]");
|
||||
String[] redisStatisticsRealArr = redisStatisticsReal.split(";");
|
||||
@@ -461,8 +488,10 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
|
||||
int service = maatConfig.getService();
|
||||
if (ServiceAndRDBIndexReal.isAddASU(service)) {
|
||||
if (!keySet.contains(TAPREDISDB)) {
|
||||
throw new ServiceRuntimeException("业务类型:" + service + ",需要向阀门" + TAPREDISDB + "号库分发,但是当前只往"
|
||||
+ keySet + "库下发,请检查阀门编号或者当前业务对应的配置文件是否正确",RestBusinessCode.PropertiesIsError.getValue());
|
||||
throw new ServiceRuntimeException(
|
||||
"业务类型:" + service + ",需要向阀门" + TAPREDISDB + "号库分发,但是当前只往" + keySet
|
||||
+ "库下发,请检查阀门编号或者当前业务对应的配置文件是否正确",
|
||||
RestBusinessCode.PropertiesIsError.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -695,7 +724,7 @@ public class ConfigJedisServiceimpl implements ConfigRedisService {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (isReuse && (type == 12 || type == 13 || type == 14|| type == 15|| type == 18)) {// 将数据往临时库复制一份,无论临时库里有没有都可以直接写,有则覆盖,不影响
|
||||
} else if (isReuse && (type == 12 || type == 13 || type == 14 || type == 15 || type == 18)) {// 将数据往临时库复制一份,无论临时库里有没有都可以直接写,有则覆盖,不影响
|
||||
int tmpStorageReuseRegionDB = Configurations.getIntProperty("tmpStorageReuseRegionDB", 15);
|
||||
transaction.select(tmpStorageReuseRegionDB);
|
||||
transaction.set(maatKey.toUpperCase(), valBF.toString());
|
||||
|
||||
@@ -139,7 +139,7 @@
|
||||
<p:maatType service="520">
|
||||
<p:expressions>
|
||||
<p:keyExpression>EFFECTIVE_RULE;:;{un_maat_table_name};,;[cfg_id]</p:keyExpression>
|
||||
<p:valueExpression>[cfg_id];\t;[service];\t;[keyring_name];\t;[keyring_type];\t;[private_key_file];\t;[public_key_file];\t;[expire_after];\t;[public_key_algo];\t;[crl];\t;[is_valid];\t;[op_time];&nbsp;0;\n</p:valueExpression>
|
||||
<p:valueExpression>[cfg_id];\t;[service];\t;[keyring_name];\t;[keyring_type];\t;[private_key_file];\t;[public_key_file];\t;[expire_after];\t;[public_key_algo];\t;[crl];\t;[is_valid];\t;[redisFile_private_key_file];\t;[redisFile_public_key_file];\t;[op_time];&nbsp;0;\n</p:valueExpression>
|
||||
</p:expressions>
|
||||
<p:sequences>
|
||||
<p:operation>1</p:operation>
|
||||
|
||||
Reference in New Issue
Block a user