添加520业务支持向redis中存放文件

This commit is contained in:
RenKaiGe-Office
2018-09-27 18:00:27 +08:00
parent df9ab11a9c
commit 861b962199
5 changed files with 155 additions and 53 deletions

View 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();
}
}