下载完关闭流

master
linjj 2 years ago
parent 89c152fbc8
commit 972d86d783

@ -54,6 +54,12 @@
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>

@ -38,16 +38,9 @@ public class ScheduledJob implements Job {
@Override
public void execute(JobExecutionContext context) {
try {
//System.out.println("动态的定时任务1执行时间" + LocalTime.now());
QuartzUtils.pauseScheduleJob(scheduler, "group1", "job5");
logger.error("下载任务开始");
long start = System.currentTimeMillis();
List<MessageSubordinate> all = messageSubordinateService.findAllByStatus(0);
logger.error("本次调度任务查询待下载时间:" + (System.currentTimeMillis() - start));
logger.error("本次调度任务需要下载数量:" + all.size());
long start1 = System.currentTimeMillis();
queueService.doSomething(all);
logger.error("处理下载任务耗时:" + (System.currentTimeMillis() - start1));
} catch (Exception e) {
ExceptionPrintUtil.printException(e);
// 如果报错-- 捕捉异常 继续执行

@ -21,6 +21,10 @@ public interface MessageSubordinateRepository extends JpaRepository<MessageSubor
public List<MessageSubordinate> findAllMessageSubordinateByError();
@Query(nativeQuery = true , value = "select * from message_subordinate where message_id in (" +
"select id from message_log where create_time > '2020-01-01 00:00:00.000') and status = 2 and remark = 'java.lang.StringIndexOutOfBoundsException: String index out of range: -1'")
List<MessageSubordinate> findHaha();

@ -13,6 +13,7 @@ import com.ann.entity.interfaceEntity.dto.InterfaceHisCacheDto;
import com.ann.repository.InterfaceHisCacheDtoRepository;
import com.ann.service.*;
import com.ann.utils.*;
import com.ann.utils.downLoader.HttpDownloader;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -288,9 +289,18 @@ public class QueueService {
}
}
} else if (address.contains("http")){//新心电以http协议传
String path = HttpUtils.getPdfByHttpUrl(address, pdfFile);
if (path != null) {
pdfPath = path;
logger.error("类型为:" + messageDto.getType());
if (Objects.equals(messageDto.getType(), AliasName.PETCT_REPORT)) {
HttpDownloader httpDownloader = new HttpDownloader(null);
logger.error("核医学下载地址为URL" + address);
logger.error("核医学下载文件名:" + address);
httpDownloader.downLoadFromUrl(address, pdfFile.getName(), pdfFile.getParent());
pdfPath = pdfFile.getAbsolutePath();
} else {
String path = HttpUtils.getPdfByHttpUrl(address, pdfFile);
if (path != null) {
pdfPath = path;
}
}
} else {
// 一张图片转成pdf

@ -0,0 +1,141 @@
package com.ann.utils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.SingletonBeanRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringUtils.applicationContext = applicationContext;
}
/**
* bean
*
* @param name bean
* @return true
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
/**
* spring bean
*
* @param name bean
* @return
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
/**
* bean()
*
* @param requiredType bean
* @return bean
*/
public static <T> T getBean(Class<T> requiredType) throws BeansException {
return applicationContext.getBean(requiredType);
}
/**
* bean
*
* @param type
* @return Map<bean, bean>
*/
public static <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException {
return applicationContext.getBeansOfType(type);
}
public static <T> T getBean(String beanName, Class<T> beanClass) {
return applicationContext.getBean(beanName, beanClass);
}
public static <T> T createBean(Class<T> beanClass) {
return applicationContext.getAutowireCapableBeanFactory().createBean(beanClass);
}
public static <T> List<T> getBeans(Class<T> beanClass) {
return new ArrayList<>(applicationContext.getBeansOfType(beanClass).values());
}
@SuppressWarnings("unchecked")
public static <T> T initializeBean(T bean) {
AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
beanFactory.autowireBean(bean);
return (T) beanFactory.initializeBean(bean, bean.getClass().getName());
}
public static void registerSingletonBean(String beanName, Class beanClass) {
GenericBeanDefinition definition = new GenericBeanDefinition();
definition.setBeanClass(beanClass);
definition.setScope(BeanDefinition.SCOPE_SINGLETON);
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) applicationContext.getAutowireCapableBeanFactory();
registry.registerBeanDefinition(beanName, definition);
}
public static void registerSingletonBean(String beanName, Object bean) {
SingletonBeanRegistry registry = (SingletonBeanRegistry) applicationContext.getAutowireCapableBeanFactory();
registry.registerSingleton(beanName, bean);
}
public static Object invokeMethod(Class<?> requiredType, String methodName, Object[] params) {
Object service = getBean(requiredType);
Class<? extends Object>[] paramClass = null;
if (params != null) {
int paramsLength = params.length;
paramClass = new Class[paramsLength];
for (int i = 0; i < paramsLength; i++) {
paramClass[i] = params[i].getClass();
}
}
// 找到方法
Method method = ReflectionUtils.findMethod(service.getClass(), methodName, paramClass);
// 执行方法
return ReflectionUtils.invokeMethod(method, service, params);
}
/**
* beanspring bean
*
* @param serviceName
* @param methodName
* @param params
* @return
*/
public static Object invokeMethod(String serviceName, String methodName, Object[] params) {
Object service = getBean(serviceName);
Class<? extends Object>[] paramClass = null;
if (params != null) {
int paramsLength = params.length;
paramClass = new Class[paramsLength];
for (int i = 0; i < paramsLength; i++) {
paramClass[i] = params[i].getClass();
}
}
// 找到方法
Method method = ReflectionUtils.findMethod(service.getClass(), methodName, paramClass);
// 执行方法
return ReflectionUtils.invokeMethod(method, service, params);
}
}

@ -0,0 +1,559 @@
package com.ann.utils.downLoader;
import com.alibaba.fastjson.JSON;
import com.ann.utils.downLoader.download.CallBackPara;
import com.ann.utils.downLoader.download.FileCheckPoints;
import com.ann.utils.downLoader.download.IDownCallBack;
import com.ann.utils.downLoader.download.IDownloadInfo;
import com.ann.utils.downLoader.download.MultiDownFile;
import com.ann.utils.downLoader.download.SaveFileItem;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.util.URIUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.security.cert.CertificateException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
*/
@Slf4j
public class HttpDownloader extends Thread {
private Logger logger = LoggerFactory.getLogger(HttpDownloader.class);
private IDownloadInfo info;
private int maxRetry = 5;
private IDownCallBack downCallBack;
private String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36";
public HttpDownloader(IDownloadInfo info, int maxRetry) {
this.info = info;
this.maxRetry = maxRetry;
}
/**
*
*
* @param downCallBack
*/
public void intiCallBack(IDownCallBack downCallBack) {
this.downCallBack = downCallBack;
}
public HttpDownloader(IDownloadInfo info) {
this.info = info;
}
public String[] removeArraysEmpty(String[] arr) {
return Arrays.stream(arr).filter(s -> !"".equals(s)).toArray(String[]::new);
}
@Override
public void run() {
CallBackPara para = new CallBackPara();
para.setId(info.getPair().id);
para.setFilename(info.getPair().localName);
para.setLocalpath(info.getPair().localPath);
para.setStarttime(getNewtime());
para.setFileStorageFormat(info.getPair().fileStorageFormat);
para.setDatatype(info.getPair().datatype);
String url = info.getPair().remoteUrl;
List<MultiDownFile> files = new ArrayList<>();
String[] urls = removeArraysEmpty(url.split("http://|https://"));
if (urls.length > 1) {
for (int i = 0; i < urls.length; i++) {
String urltemp = "";
if (url.indexOf("http://" + urls[i]) >= 0) {
urltemp = "http://" + urls[i];
}
if (url.indexOf("https://" + urls[i]) >= 0) {
urltemp = "https://" + urls[i];
}
for (String o : this.info.getPair().separators) {
urltemp = urltemp.replaceAll(o + "$", "");
}
files.add(new MultiDownFile(urltemp, info.getPair().localPath, "docustemp_" + i + "_" + info.getPair().localName));
}
} else {
files.add(new MultiDownFile(url, info.getPair().localPath, info.getPair().localName));
}
para.setFiles(files);
if (info.getPair().proxyurls != null) {
Pattern p = Pattern.compile(String.join("|", info.getPair().proxyurls));
for (MultiDownFile o : files) {
Matcher matcher = p.matcher(o.getRemoteUrl());
if (matcher.find()) {
o.setRemoteUrl(String.format(info.getPair().failurl, o.getRemoteUrl()));
}
}
}
// URLHttpDownBootstrapBuilder builder=null;
// HttpDownBootstrap bootstrap;
try {
for (MultiDownFile file : files) {
// try {
// url = EncoderUrl(file.getRemoteUrl());
// } catch (Exception e) {
//
// }
downLoadFromUrl(url, file.getLocalName(), file.getLocalPath());
if (downCallBack != null) {
downCallBack.success(para);
}
//防止过快,第三链接无法支持
try {
Thread.sleep(100);
} catch (Exception e) {
}
// builder = HttpDownBootstrap.builder(url);
// builder.downConfig(new HttpDownConfigInfo()
// .setFilePath(file.getLocalPath())
// ).callBackPara(para);
// builder.response(new HttpResponseInfo(file.getLocalName()));
// bootstrap = builder.callback(new ConsoleHttpDownCallback()).build();
// bootstrap.start();
// bootstrap = null;
// builder = null;
}
} catch (Exception e) {
// e.printStackTrace();
log.error("nio下载失败" + JSON.toJSONString(para) + ";失败信息:" + e.getMessage());
if (downCallBack != null) {
downCallBack.fail(para);
}
}
// finally {
// bootstrap = null;
// builder = null;
// }
}
public String EncoderUrl(String url) throws UnsupportedEncodingException {
String resultURL = "";
for (int i = 0; i < url.length(); i++) {
char charAt = url.charAt(i);
//只对汉字处理
if (isChineseChar(charAt)) {
String encode = URLEncoder.encode(charAt + "", "UTF-8");
resultURL += encode;
} else {
resultURL += charAt;
}
}
return resultURL;
}
public boolean isChineseChar(char c) {
return String.valueOf(c).matches("[\u4e00-\u9fa5]");
}
/**
* 302
*
* @param uc
* @return
* @throws Exception
*/
private HttpURLConnection reload(HttpURLConnection uc) throws Exception {
HttpURLConnection huc = uc;
if (huc.getResponseCode() == HttpURLConnection.HTTP_MOVED_TEMP
|| huc.getResponseCode() == HttpURLConnection.HTTP_MOVED_PERM) {// 302, 301
String url = huc.getHeaderField("Location");
url = url.replace("\\", "/");
return reload((HttpURLConnection) new URL(url).openConnection());
}
return uc;
}
public void downLoadFromUrl(String urlStr, String fileName, String savePath) throws Exception {
long start = System.currentTimeMillis();
urlStr = urlStr.replace("\\", "/");
urlStr = URIUtil.encodePathQuery(urlStr);
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
boolean useHttps = urlStr.toLowerCase().startsWith("https");
if (useHttps) {
HttpsURLConnection https = (HttpsURLConnection) conn;
trustAllHosts(https);
https.setHostnameVerifier(DO_NOT_VERIFY);
}
//设置超时间为3秒
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", userAgent);
conn.setRequestProperty("Accept-Encoding", "identity");
conn.setConnectTimeout(8 * 1000);
conn.setReadTimeout(8 * 1000);
conn = reload(conn);
long length = conn.getContentLength();
if (length < 0) {
String values = conn.getHeaderField("Content-Length");
if (values != null && !values.isEmpty()) {
length = Long.parseLong(values);
}
}
// log.info(urlStr+" 文件大小:"+length);
InputStream inputStream = null;
if (conn.getResponseCode() >= 400) {
throw new Exception("文件不存在");
// inputStream = conn.getErrorStream();
} else {
inputStream = conn.getInputStream();
}
//得到输入流
//InputStream inputStream = conn.getInputStream();
//获取自己数组
byte[] getData = readInputStream(inputStream);
//文件保存位置
File saveDir = new File(savePath);
if (!saveDir.exists()) {
saveDir.mkdirs();
}
File file = new File(saveDir + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(getData);
if (fos != null) {
fos.close();
}
if (inputStream != null) {
inputStream.close();
}
long end = System.currentTimeMillis();
logger.info("info:" + url + " download success;用时:" + (end - start) + "ms");
} catch (Exception ex) {
throw ex;
} finally {
// 断开连接,释放资源
conn.disconnect();
}
}
/**
*
*
* @param inputStream
* @return
* @throws IOException
*/
public byte[] readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return bos.toByteArray();
}
public static FileCheckPoints initCheckPoint(int splitNum, long totalSize, long timeStamp) {
long[] startPos = new long[splitNum];
long[] endPos = new long[splitNum];
for (int i = 0, len = startPos.length; i < len; i++) {
long size = i * (totalSize / len);
startPos[i] = size;
// 设置最后一个结束点的位置
if (i == len - 1) {
endPos[i] = totalSize;
} else {
size = (i + 1) * (totalSize / len);
endPos[i] = size;
}
}
FileCheckPoints chp = new FileCheckPoints();
chp.setEndPos(endPos);
chp.setStartPos(startPos);
chp.totalSize = totalSize;
chp.timestamp = timeStamp;
return chp;
}
private FileCheckPoints getInitedCheckPoint() {
long fileLength = -1;
long timeStamp = -1;
HttpURLConnection conn = null;
int stateCode = 0;
try {
URL url = new URL(this.info.getPair().remoteUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept-Encoding", "identity");
HttpDownloader.RetriveSingleStream.setHeader(conn);
stateCode = conn.getResponseCode();
// 判断http status是否为HTTP/1.1 206 Partial Content或者200 OK
if (stateCode != HttpURLConnection.HTTP_OK
&& stateCode != HttpURLConnection.HTTP_PARTIAL) {
logger.warn(info.getPair().remoteUrl + " #Error Code:# "
+ stateCode);
fileLength = -2;
} else if (stateCode >= 400) {
logger.warn(info.getPair().remoteUrl + " #Error Code:# "
+ stateCode);
fileLength = -2;
} else {
// 获取长度
fileLength = conn.getContentLengthLong();
timeStamp = conn.getLastModified();
logger.info(info.getPair().remoteUrl + " #FileLength:# "
+ fileLength);
}
} catch (MalformedURLException e) {
// e.printStackTrace();
} catch (IOException e) {
// e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
FileCheckPoints chp;
if (fileLength > 0) {
chp = initCheckPoint(info.getSplitNum(), fileLength, timeStamp);
chp.timestamp = timeStamp;
} else {
chp = new FileCheckPoints();
chp.statecode = stateCode;
}
return chp;
}
/**
* bug fixed change the RandomAccessFile size
*
* @author burkun
*/
protected static class RetriveSingleStream implements Runnable {
private boolean isDone = false;
private FileCheckPoints chp;
private int curIndex;
private SaveFileItem file;
private long startPos;
private long endPos;
byte[] buffer = new byte[1024 * 12];
private IDownloadInfo __info;
private int maxRetry;
private Logger logger = LoggerFactory.getLogger(RetriveSingleStream.class);
public boolean isDone() {
return isDone;
}
public RetriveSingleStream(IDownloadInfo info, FileCheckPoints chp,
int curIndex, int maxRetry) {
this.__info = info;
this.chp = chp;
this.curIndex = curIndex;
this.startPos = chp.getStartPos()[curIndex];
this.endPos = chp.getEndPos()[curIndex];
this.maxRetry = maxRetry;
}
@Override
public void run() {
InputStream in = null;
HttpURLConnection conn = null;
int curRetry = 0;
while (curRetry < maxRetry && !isDone) {
try {
URL url = new URL(__info.getPair().remoteUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(30000);
setHeader(conn);
String property = "bytes=" + startPos + "-";
conn.setRequestProperty("RANGE", property);
logger.info(__info.getPair().localName + " #Block"
+ (curIndex + 1) + "# begin downloading...");
int length;
long counter = 0;
InputStream is = conn.getInputStream();
file = new SaveFileItem(__info.getPair().getLocalFullPath(), startPos);
//--bug fixed
file.setLength(__info.getCurCheckPoints().totalSize);
//--bug fixed
while (!isDone && startPos < endPos && (length = is.read(buffer)) > 0) {
startPos += file.write(buffer, 0, length);
counter += 1;
chp.getStartPos()[curIndex] = Math.min(startPos, endPos);
if (counter % 20 == 0) {
__info.writeInfo(chp);
logger.info(__info.getPair().remoteUrl + " #Block"
+ (curIndex + 1) + "# download "
+ getPercentage() + "%...");
Thread.yield();
}
}
__info.writeInfo(chp);
isDone = true;
} catch (IOException e) {
isDone = false;
logger.debug(__info.getPair().remoteUrl, e);
} finally {
if (!isDone) {
curRetry++;
logger.debug(__info.getPair().remoteUrl + " download failed, retry again!");
if (curRetry >= maxRetry) {
//保证循环跳出
isDone = true;
}
} else {
curRetry = maxRetry;
}
try {
if (in != null) {
in.close();
}
if (file != null) {
file.close();
}
if (conn != null) {
conn.disconnect();
}
} catch (IOException e) {
logger.debug(__info.getPair().remoteUrl, e);
}
}
}
}
public static void setHeader(URLConnection conn) {
conn.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 BIDUBrowser/7.0 Safari/537.36");
conn.setRequestProperty("Accept-Language",
"en-us,en;q=0.7,zh-cn;q=0.3");
conn.setRequestProperty("Accept-Encoding", "utf-8");
conn.setRequestProperty("Accept-Charset",
"ISO-8859-1,utf-8;q=0.7,*;q=0.7");
conn.setRequestProperty("Keep-Alive", "300");
conn.setRequestProperty("connnection", "keep-alive");
// conn.setRequestProperty("If-Modified-Since",
// "Fri, 02 Jan 2009 17:00:05 GMT");
// conn.setRequestProperty("If-None-Match",
// "\"1261d8-4290-df64d224\"");
conn.setRequestProperty("Cache-conntrol", "max-age=0");
conn.setRequestProperty("Referer", "http://www.baidu.com");
}
private int getPercentage() {
long total = 0;
for (int i = 0; i < chp.getSplit(); i++) {
total += chp.getEndPos()[i] - chp.getStartPos()[i];
}
return (int) ((chp.totalSize - total) * 100 / chp.totalSize);
}
}
private Timestamp getNewtime() {
Date now = new Date();
Timestamp timestamp = new Timestamp(now.getTime());
return timestamp;
}
// public static void main(String[] args) {
// String url="http://ss,ss,https://bbbbb;http://ccc";
// List<String> separators = new ArrayList<>();
// separators.add(",");
// separators.add(";");
// String[] urls = url.split("http://|https://");
// for (int i = 0; i < urls.length; i++) {
// String urltemp = "";
// if (url.indexOf("http://" + urls[i]) >= 0) {
// urltemp="http://" + urls[i];
// }
// if (url.indexOf("https://" + urls[i]) >= 0) {
// urltemp="https://" + urls[i];
// }
// for(String o:separators){
// urltemp=urltemp.replaceAll(o+"$", "");
// }
// System.out.println(urltemp);
// }
// }
/**
* java
*/
private final TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws CertificateException {
}
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}};
/**
*
*/
private final HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
/**
*
*
* @param connection
* @return
*/
private SSLSocketFactory trustAllHosts(HttpsURLConnection connection) {
SSLSocketFactory oldFactory = connection.getSSLSocketFactory();
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
SSLSocketFactory newFactory = sc.getSocketFactory();
connection.setSSLSocketFactory(newFactory);
} catch (Exception e) {
e.printStackTrace();
}
return oldFactory;
}
}

@ -0,0 +1,154 @@
package com.ann.utils.downLoader.download;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@Slf4j
public class Base64Utils {
private static Base64Utils utils =null;
private Base64Utils(){
}
public static Base64Utils getInstance(){
if(utils==null){
synchronized (Base64Utils.class){
if(utils == null){
utils = new Base64Utils();
}
}
}
return utils;
}
public String imageToBase64Str(String imgFile) {
InputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
// 加密
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
}
public byte[] base64StrToBytes(String imgStr) {
if (imgStr == null) {
return null;
}
String[] s = imgStr.split(",");
if(s.length>0){
imgStr=s[1];
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// 解密
byte[] b = decoder.decodeBuffer(imgStr);
// 处理数据
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
return b;
} catch (Exception e) {
log.error("Base64 转 byte[] 出错啦!",e);
return null;
}
}
public boolean base64StrToImage(byte[] bytes, String path) {
if (bytes == null) {
return false;
}
try {
//文件夹不存在则自动创建
File tempFile = new File(path);
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(tempFile);
out.write(bytes);
out.flush();
out.close();
return true;
} catch (Exception e) {
log.error("图片文件下载失败:"+e.getMessage());
return false;
}
}
public boolean base64StrToImage(String imgStr, String path) {
if (imgStr == null) {
return false;
}
String[] s = imgStr.split(",");
if(s.length>0){
imgStr=s[1];
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// 解密
byte[] b = decoder.decodeBuffer(imgStr);
// 处理数据
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
//文件夹不存在则自动创建
File tempFile = new File(path);
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs();
}
OutputStream out = new FileOutputStream(tempFile);
out.write(b);
out.flush();
out.close();
return true;
} catch (Exception e) {
log.error("图片文件下载失败:"+e.getMessage());
return false;
}
}
public int getFileSize(File inFile){
InputStream in =null;
try{
in= new FileInputStream(inFile);
int len =in.available();
return len;
}
catch (Exception e){
}
finally {
try{
in.close();
}
catch (IOException e){
e.printStackTrace();
}
}
return -1;
}
public static void main(String[] args) {
System.out.println(Base64Utils.getInstance().imageToBase64Str("e:\\1.jpg"));
}
}

@ -0,0 +1,50 @@
package com.ann.utils.downLoader.download;
import lombok.Data;
import java.sql.Timestamp;
import java.util.List;
@Data
public class CallBackPara {
/**
*
*/
private String id;
/**
*
*/
private String mess;
/**
*
*/
private String localpath;
/**
*
*/
private String filename;
/**
*
*/
private Timestamp starttime;
/**
*
*/
private Timestamp endtime;
private List<MultiDownFile> files;
/**
* 0 1 pdf 2 jpg
*/
private int fileStorageFormat=1;
/**
* 0:1 2
*/
private int datatype=0;
}

@ -0,0 +1,68 @@
package com.ann.utils.downLoader.download;
import com.ann.utils.SpringUtils;
import com.ann.utils.downLoader.HttpDownloader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
public class DownloadManager {
private static Logger logger = LoggerFactory.getLogger(DownloadManager.class);
enum ProtocolType {HTTP, HTTPS, FTP, BASE64, SMB, OTHER}
;
public static void feedLinks(Collection<RemoteLocalPair> list, int maxHttpRetry) {
for (RemoteLocalPair pair : list) {
ProtocolType deal = getLinkProtocol(pair.remoteUrl);
if (deal == ProtocolType.HTTP || deal == ProtocolType.HTTPS) {
HttpDownload(pair, maxHttpRetry);
} else if (deal == ProtocolType.FTP) {
} else if (deal == ProtocolType.BASE64) {
} else if (deal == ProtocolType.SMB) {
} else {
logger.error(pair.remoteUrl + " Url is not http or ftp!");
}
}
}
public static void HttpDownload(RemoteLocalPair pair, int maxHttpRetry) {
//GeneralDownloadInfo info = new GeneralDownloadInfo(pair);
NoCheckPointInfo info = new NoCheckPointInfo(pair);
HttpDownloader downloader = new HttpDownloader(info, maxHttpRetry);
downloader.intiCallBack(SpringUtils.getBean(IDownCallBack.class));
//可以用thread.start()不过不建议
downloader.run();
}
protected static ProtocolType getLinkProtocol(String link) {
int idx = link.indexOf("://");
if (idx == -1) {
if (link.contains("base64")) {
return ProtocolType.BASE64;
}
return ProtocolType.OTHER;
}
String name = link.substring(0, idx);
if (name.equals("http")) {
return ProtocolType.HTTP;
}
if (name.equals("https")) {
return ProtocolType.HTTPS;
}
if (name.equals("ftp")) {
return ProtocolType.FTP;
}
if (name.equals("smb")) {
return ProtocolType.SMB;
}
return ProtocolType.OTHER;
}
}

@ -0,0 +1,40 @@
package com.ann.utils.downLoader.download;
public class FileCheckPoints {
public int statecode;
public long timestamp = -99;
public long totalSize = -99;
private int split = -1;
private long[] startPos;
private long[] endPos;
public long[] getStartPos() {
return startPos;
}
public void setStartPos(long[] startPos) {
split = startPos.length;
this.startPos = startPos;
}
public long[] getEndPos() {
return endPos;
}
public void setEndPos(long[] endPos) {
split = endPos.length;
this.endPos = endPos;
}
public int getSplit() {
return split;
}
public void copy(FileCheckPoints _chp) {
this.setEndPos(_chp.endPos);
this.setStartPos(_chp.startPos);
this.totalSize = _chp.totalSize;
this.timestamp = _chp.timestamp;
}
}

@ -0,0 +1,11 @@
package com.ann.utils.downLoader.download;
import lombok.Data;
@Data
public class FtpInfo {
private String ip;
private Integer port;
private String user;
private String pwd;
}

@ -0,0 +1,200 @@
package com.ann.utils.downLoader.download;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class GeneralDownloadInfo implements IDownloadInfo {
/**
* 0:ftp1:234
* @param fullUrl
* @return
*/
public static String[] getHostNameAndFilePath(String fullUrl) {
fullUrl = fullUrl.trim();
String[] res = new String[5];
int beginIndex = "ftp://".length();
int endIndex = fullUrl.indexOf('/', beginIndex);
if (endIndex != -1) {
res[0] = fullUrl.substring(beginIndex, endIndex);
res[1] = fullUrl.substring(endIndex + 1);
} else {
res[0] = fullUrl.substring(beginIndex);
res[1] = Tools.getRandomUUID().toString() + ".none";
}
String[] temp = res[0].split("@");
if(temp.length>1){
res[0] = temp[1];
res[2] =temp[0];
}
else
{
res[2]="";
res[3]="";
}
temp = res[0].split(":");
if(temp.length>1){
res[0] = temp[0];
res[4] =temp[1];
}
else
{
res[4]="";
}
if(!StringUtils.isEmpty(res[2])){
temp = res[2].split(":");
if(temp.length>1){
res[2] = temp[0];
res[3] =temp[1];
}
}
return res;
}
public GeneralDownloadInfo(RemoteLocalPair pair) {
this.pair = pair;
curName = pair.localName;
curFlagFile = new File(pair.getLocalFullPath()+".flags");
if (!curFlagFile.getParentFile().exists()) {
curFlagFile.getParentFile().mkdirs();
}
}
@Override
public FileCheckPoints getCurCheckPoints() {
return chp;
}
@Override
public RemoteLocalPair getPair() {
return this.pair;
}
@Override
public void initDownload() {
}
@Override
public int getSplitNum(){
return this.pair.splitNum;
}
@Override
public boolean isNeedDownload(FileCheckPoints serverInitChp) {
chp = readInfo();
if (chp.timestamp == serverInitChp.timestamp && chp.totalSize == serverInitChp.totalSize && chp.getSplit() == serverInitChp.getSplit()) {
// 不需要下载
boolean isneed = false;
for (int i = 0; i < chp.getStartPos().length; i++) {
if (chp.getStartPos()[i] < chp.getEndPos()[i]) {
isneed = true;
logger.info("restore from checkpoint");
break;
}
}
return isneed;
} else {
chp.copy(serverInitChp);
return true;
}
}
@Override
public synchronized boolean writeInfo(FileCheckPoints chkp) {
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream(curFlagFile));
dos.writeLong(chkp.timestamp);
dos.writeLong(chkp.totalSize);
int split = chp.getSplit();
dos.writeInt(split);
for(int i=0; i < chp.getSplit(); i++){
dos.writeLong(chp.getStartPos()[i]);
dos.writeLong(chp.getEndPos()[i]);
}
} catch (FileNotFoundException e) {
logger.debug(curName, e);
} catch (IOException e) {
logger.debug(curName, e);
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
logger.debug(curName, e);
}
}
}
return true;
}
/**
* <b>function:</b>
*/
@Override
public FileCheckPoints readInfo() {
if (curFlagFile.exists()) {
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream(curFlagFile));
long curTimeStamp = dis.readLong();
long curTotalSize = dis.readLong();
int curSplit = dis.readInt();
FileCheckPoints chkP = new FileCheckPoints();
chkP.timestamp = curTimeStamp;
chkP.totalSize = curTotalSize;
long[] sp = new long[curSplit];
long[] ep = new long[curSplit];
for (int i=0; i< curSplit; i++){
sp[i] = dis.readLong();
ep[i] = dis.readLong();
}
chkP.setStartPos(sp);
chkP.setEndPos(ep);
return chkP;
} catch (FileNotFoundException e) {
logger.debug(curName, e);
} catch (IOException e) {
logger.debug(curName, e);
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
logger.debug(curName, e);
}
}
}
} else {
return new FileCheckPoints();
}
return new FileCheckPoints();
}
@Override
public void downloadDone(FileCheckPoints chkp) {
writeInfo(chkp);
}
private Logger logger = LoggerFactory.getLogger(GeneralDownloadInfo.class);
private RemoteLocalPair pair;
private String curName;
private File curFlagFile;
private FileCheckPoints chp = null;
}

@ -0,0 +1,143 @@
package com.ann.utils.downLoader.download;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class GlobalData {
/**
* @param args
*/
public static void main(String[] args) {
}
private Properties properties;
private String filePath;
private GlobalData(String filePath) {
this.filePath = filePath;
properties = loadProperties(filePath);
}
//===================================
private Properties loadProperties(String filePath) {
InputStream is;
try {
is = new BufferedInputStream(new FileInputStream(new File(filePath)));
Properties properties = new Properties();
properties.load(is);
return properties;
} catch (FileNotFoundException e) {
// e.printStackTrace();
} catch (IOException e) {
// e.printStackTrace();
}
return null;
}
public Properties getProperties() {
return properties;
}
public void setPropertity(String key, String value) {
FileOutputStream oFile;
try {
properties.setProperty(key, value);
oFile = new FileOutputStream(filePath);
properties.store(oFile, "add counter!");
oFile.close();
} catch (IOException e) {
// e.printStackTrace();
}
}
public String getTempDir() {
String path = properties.getProperty("TEMP_PATH");
File dir = new File(path);
if (!dir.exists()) {
try {
dir.mkdir();
} catch (SecurityException e) {
// e.printStackTrace();
}
}
return path;
}
public String getParserDir() {
String path = properties.getProperty("PARSER_DIR");
File dir = new File(path);
if (!dir.exists()) {
try {
dir.mkdir();
} catch (SecurityException e) {
// e.printStackTrace();
}
}
return path;
}
public String getHomoGoaUrl() {
return properties.getProperty("GO_HOMO_GOA_GZ_LINK");
}
public String getGoBasicOboUrl() {
return properties.getProperty("GO_OBO_BASIC_LINK");
}
public String getGoOboUrl() {
return properties.getProperty("GO_OBO_LINK");
}
public String getGoOwlUrl() {
return properties.getProperty("GO_OWL_LINK");
}
public String getGoOwlPlusUrl() {
return properties.getProperty("GO_OWL_PLUS_LINK");
}
public String getDoOboUrl() {
return properties.getProperty("DO_OBO_LINK");
}
public String getProOboUrl() {
return properties.getProperty("PRO_OBO_LINK");
}
public String getChebiOboUrl() {
return properties.getProperty("CHEBI_OBO_LINK");
}
public String getLocalGoBasicPath() {
String goUrl = getGoBasicOboUrl();
return getTempDir() + File.separator + GlobalData.getFileName(goUrl);
}
//====================================
private static GlobalData single;
private static Object lock = new Object();
//====================================
public static String getFileName(String url) {
return url.substring(url.lastIndexOf("/") + 1, url.length());
}
public static GlobalData getInstance() {
synchronized (lock) {
if (single == null) {
single = new GlobalData("configure/settings.properties");
}
return single;
}
}
}

@ -0,0 +1,7 @@
package com.ann.utils.downLoader.download;
public interface IDownCallBack {
void success(CallBackPara para);
void fail(CallBackPara para);
}

@ -0,0 +1,60 @@
package com.ann.utils.downLoader.download;
public interface IDownloadInfo {
/**
* download
*/
void initDownload();
/**
*
*
* @param serverInitChp checkpoint
* @return
*/
boolean isNeedDownload(FileCheckPoints serverInitChp);
/**
*
*
* @param chkp checkpoint
* @return
*/
boolean writeInfo(FileCheckPoints chkp);
/**
* checkPoint
*
* @return
*/
FileCheckPoints getCurCheckPoints();
/**
*
*
* @return
*/
FileCheckPoints readInfo();
/**
*
*
* @param chkp
*/
void downloadDone(FileCheckPoints chkp);
/**
*
*
* @return
*/
RemoteLocalPair getPair();
/**
* 1
*
* @return
*/
int getSplitNum();
}

@ -0,0 +1,38 @@
package com.ann.utils.downLoader.download;
public class MultiDownFile {
public MultiDownFile(String remoteUrl, String localPath, String localName) {
this.remoteUrl = remoteUrl;
this.localPath = localPath;
this.localName = localName;
}
private String remoteUrl;
private String localPath;
private String localName;
public String getRemoteUrl() {
return remoteUrl;
}
public void setRemoteUrl(String remoteUrl) {
this.remoteUrl = remoteUrl;
}
public String getLocalPath() {
return localPath;
}
public void setLocalPath(String localPath) {
this.localPath = localPath;
}
public String getLocalName() {
return localName;
}
public void setLocalName(String localName) {
this.localName = localName;
}
}

@ -0,0 +1,73 @@
package com.ann.utils.downLoader.download;
import java.io.File;
/**
* 使info线
*
* @author burkun
*/
public class NoCheckPointInfo implements IDownloadInfo {
private boolean isDownloding;
public NoCheckPointInfo(RemoteLocalPair pair) {
this.pair = pair;
File file = new File(pair.localPath);
if (!file.exists()) {
file.mkdirs();
}
}
@Override
public void initDownload() {
isDownloding = true;
}
@Override
public boolean isNeedDownload(FileCheckPoints serverInitChp) {
chp = serverInitChp;
return true;
}
@Override
public boolean writeInfo(FileCheckPoints chkp) {
chp = chkp;
return true;
}
@Override
public FileCheckPoints getCurCheckPoints() {
return chp;
}
@Override
public FileCheckPoints readInfo() {
return null;
}
@Override
public void downloadDone(FileCheckPoints chkp) {
isDownloding = false;
}
@Override
public RemoteLocalPair getPair() {
return pair;
}
@Override
public int getSplitNum() {
return 1;
}
public boolean IsDownloading() {
return isDownloding;
}
private RemoteLocalPair pair;
private FileCheckPoints chp = null;
}

@ -0,0 +1,72 @@
package com.ann.utils.downLoader.download;
import java.io.File;
import java.util.List;
/**
*
*
* @author burkun
*/
public class RemoteLocalPair {
public String id;
public int splitNum = 1;
public String remoteUrl;
public String localPath;
public String localName;
public String getLocalFullPath() {
return localPath + File.separator + localName;
}
public String user;
public String pwd;
public Integer port;
public List<String> separators;
public String failurl;
public List<String> proxyurls;
public List<FtpInfo> smbips;
/**
* 0 1 pdf 2 jpg
*/
public int fileStorageFormat = 1;
/**
* 0:1 2:
*/
public int datatype = 0;
/**
* @param remoteUrl url
* @param localPath
* @param localName
*/
public RemoteLocalPair(String remoteUrl, String localPath, String localName) {
this.remoteUrl = remoteUrl;
if (localName.length() == 0) {
this.localName = getFileName(remoteUrl);
;
} else {
this.localName = localName;
}
if (localPath.length() == 0) {
this.localPath = GlobalData.getInstance().getTempDir();
} else {
this.localPath = localPath;
}
}
/**
* @param remoteUrl url
* @param localPath url
*/
public RemoteLocalPair(String remoteUrl, String localPath) {
this.remoteUrl = remoteUrl;
this.localName = getFileName(remoteUrl);
this.localPath = localPath;
}
private String getFileName(String url) {
return url.substring(url.lastIndexOf("/") + 1, url.length());
}
}

@ -0,0 +1,73 @@
package com.ann.utils.downLoader.download;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* <b>function:</b>
*/
public class SaveFileItem {
//存储文件
private RandomAccessFile itemFile;
private String name;
private Logger logger = LoggerFactory.getLogger(SaveFileItem.class);
/**
* @param name
* @param pos position
* @throws IOException
*/
public SaveFileItem(String name, long pos) throws IOException {
this.name = name;
itemFile = new RandomAccessFile(name, "rwd");
//在指定的pos位置写入数据
itemFile.seek(pos);
}
/**
* <b>function:</b>
* @author hoojo
* @createDate 2011-9-26 12:21:22
* @param buff
* @param start
* @param length
* @return
*/
public synchronized int write(byte[] buff, int start, int length) {
int i = -1;
try {
itemFile.write(buff, start, length);
i = length;
} catch (IOException e) {
logger.debug(name, e);
}
return i;
}
public void close() throws IOException {
if (itemFile != null) {
itemFile.close();
}
}
public String getFileName(){
return this.name;
}
/**
* old
* @param newLength
*/
public void setLength(long newLength){
try {
if(newLength != itemFile.length()){
itemFile.setLength(newLength);
}
} catch (IOException e) {
logger.debug(name, e);
}
}
}

@ -0,0 +1,30 @@
package com.ann.utils.downLoader.download;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.UUID;
public class Tools {
public static UUID getRandomUUID() {
return UUID.randomUUID();
}
/**
* @param filename propertiy file path
* @return
* @author burkun
*/
public static Properties readPropertiesFile(String filename) {
Properties properties = new Properties();
try {
InputStream inputs = new FileInputStream(filename);
properties.load(inputs);
inputs.close();
} catch (IOException e) {
// e.printStackTrace();
}
return properties;
}
}

@ -10,10 +10,10 @@ spring:
# show-sql: false
# hibernate:
# ddl-auto: update
url: jdbc:sqlserver://10.6.1.127:1433;DatabaseName=DB_PrivilegeManagement_GYFY
# url: jdbc:sqlserver://120.27.212.36:1433;DatabaseName=emr_record
# url: jdbc:sqlserver://10.6.1.127:1433;DatabaseName=DB_PrivilegeManagement_GYFY
url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=zj_record_new
username: sa
password: docus@702
password: admin123
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
jpa:
database: sql_server
@ -50,7 +50,7 @@ file:
dischargeRecordPath: D:\\1\dischargeRecord.properties
transferRecordPath: D:\\1\transferRecord.properties
admissionRecordPath: D:\\1\admissionRecord.properties
pdfPath: Z:/project_js
pdfPath: D:/project_js
imagePath: Z:/images
signContent: <REQUEST>

Loading…
Cancel
Save