You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
316 lines
12 KiB
Java
316 lines
12 KiB
Java
package com.ann.utils;
|
|
|
|
|
|
import com.ann.entity.constant.AliasName;
|
|
import jcifs.smb.SmbFile;
|
|
import jcifs.smb.SmbFileInputStream;
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.io.*;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.MalformedURLException;
|
|
import java.net.URL;
|
|
import java.net.URLEncoder;
|
|
import java.util.Objects;
|
|
|
|
/**
|
|
* @Description 下载工具
|
|
* @Date 2019/7/2 16:56
|
|
* @Created by ljx
|
|
*/
|
|
public class DownloadUtils {
|
|
|
|
static final Logger logger = LoggerFactory.getLogger(DownloadUtils.class);
|
|
|
|
|
|
/**
|
|
* 从网络Url中下载文件
|
|
*
|
|
* @param urlStr
|
|
* @param fileName
|
|
* @param savePath
|
|
* @throws IOException
|
|
*/
|
|
public static boolean downLoadByUrl(String urlStr, String fileName, String savePath) throws IOException {
|
|
URL url = null;
|
|
InputStream inputStream = null;
|
|
FileOutputStream fos = null;
|
|
boolean result = false;
|
|
try {
|
|
String s1 = urlStr.substring(0, urlStr.lastIndexOf("/")+1);
|
|
String s2 = urlStr.substring(urlStr.lastIndexOf("/")+1, urlStr.length());
|
|
|
|
url = new URL(s1 +URLEncoder.encode(s2, "utf-8"));
|
|
// url = new URL(urlStr);
|
|
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
|
|
//设置超时间为5秒
|
|
conn.setConnectTimeout(5 * 1000);
|
|
//防止屏蔽程序抓取而返回403错误
|
|
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
|
|
|
|
|
|
//得到输入流
|
|
inputStream = conn.getInputStream();
|
|
//获取自己数组
|
|
byte[] getData = readInputStream(inputStream);
|
|
//文件保存位置
|
|
File saveDir = new File(savePath);
|
|
if (!saveDir.exists()) {
|
|
saveDir.mkdir();
|
|
}
|
|
File file = new File(saveDir + File.separator + fileName);
|
|
fos = new FileOutputStream(file);
|
|
fos.write(getData);
|
|
result = true;
|
|
} catch (MalformedURLException e) {
|
|
throw e;
|
|
} catch (FileNotFoundException e) {
|
|
throw e;
|
|
} catch (IOException e) {
|
|
throw e;
|
|
} finally {
|
|
try {
|
|
if (fos != null) {
|
|
fos.close();
|
|
}
|
|
if (inputStream != null) {
|
|
inputStream.close();
|
|
}
|
|
} catch (IOException e) {
|
|
throw e;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
/**
|
|
* 从输入流中获取字节数组
|
|
*
|
|
* @param inputStream
|
|
* @return
|
|
* @throws IOException
|
|
*/
|
|
public static 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 boolean cmdFile(String path, File imageFile) throws IOException {
|
|
boolean result = false;
|
|
BufferedInputStream in = null;
|
|
BufferedOutputStream out = null;
|
|
try {
|
|
String tempIp = path.substring(2, path.length());
|
|
String ip = tempIp.substring(0, tempIp.indexOf("/"));
|
|
Process exec = Runtime.getRuntime().exec("net use \\\\" + ip + " his /user:his");
|
|
in = new BufferedInputStream(new FileInputStream(path));
|
|
out = new BufferedOutputStream(new FileOutputStream(imageFile));
|
|
|
|
byte[] buffer = new byte[1024];
|
|
while (in.read(buffer) != -1) {
|
|
out.write(buffer);
|
|
buffer = new byte[1024];
|
|
}
|
|
out.flush();
|
|
exec.destroy();
|
|
result = true;
|
|
} catch (IOException e) {
|
|
FileUtils.deleteImageFile(imageFile);
|
|
throw e;
|
|
} finally {
|
|
try {
|
|
if (out != null) {
|
|
out.close();
|
|
}
|
|
if (in != null) {
|
|
in.close();
|
|
}
|
|
} catch (IOException e) {
|
|
FileUtils.deleteImageFile(imageFile);
|
|
throw e;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static boolean downloadImageFile(String address, String type, File imageFile) throws Exception {
|
|
//--------------------------------------------此处判断下 是否http 也有可能传图片 共享也有可能传pdf 还有考虑ftp 这两种情况
|
|
if (address.indexOf("http") != -1) {
|
|
// http协议
|
|
return true;
|
|
} else if (address.indexOf("ftp") != -1) {
|
|
if (Objects.equals(type, AliasName.EKG_REPORT)) {
|
|
// ftp://PTVIEW:MedExTech@10.6.0.65:10021/ecgdata/ECG/2019-07-24/MECGCAS1907240069_1.jpg
|
|
address = address.substring(address.indexOf("/") + 2, address.length());
|
|
|
|
String ftpAccount = address.substring(0, address.indexOf("@"));
|
|
String[] split = ftpAccount.split(":");
|
|
address = address.substring(address.indexOf("@") + 1, address.length());
|
|
String url = address.substring(0, address.indexOf(":"));
|
|
Integer port = 0;
|
|
if (address.indexOf(":") != -1 && address.indexOf("/") != -1) {
|
|
port = Integer.parseInt(address.substring(address.indexOf(":") + 1, address.indexOf("/")));
|
|
}
|
|
String path = address.substring(address.indexOf("/"), address.length());
|
|
FTPClient ftpClient = FTPUtil.connectFtpServer(url, port, split[0], split[1]);
|
|
return FTPUtil.downloadFtp(path, ftpClient, imageFile);
|
|
}
|
|
} else {
|
|
// 共享文件
|
|
// //10.6.0.74/Images/201907_U/Report609639_1.jpg
|
|
address = address.replaceAll("\\\\", "/");
|
|
return DownloadUtils.cmdFile(address, imageFile);
|
|
//return true
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
public static String downLoadPdfFile(File pdfFile, String address, String type) throws Exception {
|
|
if (address.indexOf("http") != -1) {
|
|
// 放射 http://10.6.0.144/PDF/b74abb84-3524-442e-b0af-0d3966c9b979.pdf
|
|
if (DownloadUtils.downLoadByUrl(address, pdfFile.getName(), pdfFile.getParent())) {
|
|
return pdfFile.getAbsolutePath();
|
|
}
|
|
} else if (address.indexOf("ftp") != -1) {
|
|
String url = "";
|
|
String path = "";
|
|
Integer port = 21;
|
|
// 处理 “/”
|
|
if (address.indexOf("/") == -1) {
|
|
address = address.replace("\\", "/");
|
|
}
|
|
address = address.substring(address.indexOf("/") + 2, address.length());
|
|
path = address.substring(address.indexOf("/"), address.length());
|
|
//处理url 以及端口 如果有端口 截取 否则默认端口
|
|
if (address.indexOf(":") != -1 && address.indexOf("/") != -1) {
|
|
port = Integer.parseInt(address.substring(address.indexOf(":") + 1, address.indexOf("/")));
|
|
url = address.substring(0, address.indexOf(":"));
|
|
} else {
|
|
url = address.substring(0, address.indexOf("/"));
|
|
}
|
|
String userName = "";
|
|
String password = "";
|
|
if (Objects.equals(type, AliasName.HANDNUMBNESS_REPORT)) {
|
|
// 手麻 ftp://10.6.0.75/600/000360639/1/000360639600_1_麻醉_麻醉单_1_1.pdf
|
|
userName = "mdsd";
|
|
password = "mdsd";
|
|
} else if (Objects.equals(type, AliasName.INSPECTION_REPORT)) {
|
|
// 检验报告 ftp://10.6.1.128:23/LIS/20200228/1003620200228121.pdf
|
|
userName = "common";
|
|
password = "common";
|
|
} else if (Objects.equals(type, AliasName.PETCT_REPORT)) {
|
|
//petct报告 ftp:\\10.2.1.25:2121\201909\21\E19090279\E19090279_FT200_161712285.pdf
|
|
userName = "view";
|
|
password = "view";
|
|
} else if (Objects.equals(type, AliasName.BLOODAPPLY_REPORT)) {
|
|
// 输血 ftp://10.6.1.128/tmis/10_000526962900_9_LCFXD191021010_临床发血单.PDF
|
|
userName = "common";
|
|
password = "common";
|
|
} else if (Objects.equals(type, AliasName.ICU_REPORT)) {
|
|
// icu ftp://10.6.0.241/fallSheets/徐晓峰-988874-2020-02-25 - 2020-03-21.pdf;
|
|
userName = "zingicu";
|
|
password = "zingicu123";
|
|
} else if(Objects.equals(type, AliasName.PRN) || Objects.equals(type, AliasName.SOS)) {
|
|
// 长期医嘱/临时医嘱 ftp://10.6.1.16:21/1147_1_long.pdf
|
|
userName = "his";
|
|
password = "his_docus";
|
|
}else if(Objects.equals(type, AliasName.OTHER_REPORT)) {
|
|
//其他报告 根据IP区分报告类型 考虑IP单独做配置
|
|
userName = "";
|
|
password = "";
|
|
}else {
|
|
// 电子病历
|
|
// ftp://10.6.0.155/2020-01-25/000593857900_1_000593857900_0_4_EMR06.02.04_3_5_0.pdf
|
|
// ftp://10.6.0.155/000538888400_2_吴惠/病历文书/12440000455858169P-000538888400-2-2-0-29.pdf
|
|
userName = "pdf";
|
|
password = "pdf";
|
|
}
|
|
FTPClient ftpClient = FTPUtil.connectFtpServer(url, port, userName, password);
|
|
if (FTPUtil.downloadFtp(path, ftpClient, pdfFile)) {
|
|
return pdfFile.getAbsolutePath();
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 下载多个文件
|
|
*/
|
|
public static String downloadImageFiles(String address, String imageDir) throws Exception {
|
|
// ftp://10.6.0.241/02019-08-29.png;ftp://10.6.0.241/12019-08-29.png
|
|
String[] split = address.split(";");
|
|
String imagesPath = "";
|
|
int i = 1;
|
|
for (String addr : split) {
|
|
addr = addr.substring(addr.indexOf("/") + 2, addr.length());
|
|
String url = addr.substring(0, addr.indexOf("/"));
|
|
String path = addr.substring(addr.indexOf("/") + 1, addr.length());
|
|
// 1.连接到指定的服务器
|
|
JSchUtils.connect("zingicu", "zingicu123", url, 22);
|
|
|
|
// 2.下载文件
|
|
JSchUtils.download(path, imageDir);
|
|
// 4.关闭连接
|
|
JSchUtils.close();
|
|
|
|
//截取文件夹
|
|
path = path.substring(path.indexOf("/") + 1, path.length());
|
|
imagesPath += imageDir + "\\" + path + ";";
|
|
i++;
|
|
}
|
|
return imagesPath;
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
//10.6.0.217/images/201909_Z/ReportBB818E8E-E93A-46E8-A8B1-F789677BC0D8.1.jpg
|
|
//10.6.0.74/Images/201909_U/Report617970_1.jpg
|
|
//File imageFile = FileUtils.createFile("images","2019", "9","haha");
|
|
//smbGet("smb://his:his@10.6.0.217/images/201909_Z/ReportBB818E8E-E93A-46E8-A8B1-F789677BC0D8.1.jpg", imageFile.getParent());
|
|
}
|
|
|
|
public static void smbGet(String remoteUrl, String localDir) {
|
|
InputStream in = null;
|
|
OutputStream out = null;
|
|
try {
|
|
SmbFile remoteFile = new SmbFile(remoteUrl);
|
|
if (remoteFile == null) {
|
|
System.out.println("共享文件不存在");
|
|
return;
|
|
}
|
|
String fileName = remoteFile.getName();
|
|
File localFile = new File(localDir + File.separator + fileName);
|
|
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
|
|
out = new BufferedOutputStream(new FileOutputStream(localFile));
|
|
byte[] buffer = new byte[1024];
|
|
while (in.read(buffer) != -1) {
|
|
out.write(buffer);
|
|
buffer = new byte[1024];
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
try {
|
|
out.close();
|
|
in.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|