package com.ld.igds.util;
|
|
import com.ld.igds.data.ConfigData;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.io.FileOutputStream;
|
import java.io.*;
|
import java.net.URL;
|
|
/**
|
* @author chen
|
*/
|
@Slf4j
|
@Component
|
public class FilesUtil {
|
|
@Resource
|
private ConfigData configData;
|
|
/**
|
* 根据url地址下载保存图片
|
*
|
* @param imgUrl 图片地址,如:https://ip:port/imgUrl
|
* @param path 图片保存路径,如:F:/IGDS/COMMON/
|
* @param imgName 图片名称,如:img1.png
|
*/
|
public static void downLoadImg(String imgUrl, String path, String imgName) {
|
URL url = null;
|
//创建一个输入流
|
InputStream in = null;
|
//创建输出流
|
OutputStream os = null;
|
try {
|
url = new URL(imgUrl);
|
in = new DataInputStream(url.openStream());
|
os = new FileOutputStream(new File(path + imgName));
|
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
byte[] buffer = new byte[1024];
|
int length;
|
while ((length = in.read(buffer)) > 0) {
|
output.write(buffer, 0, length);
|
}
|
os.write(output.toByteArray());
|
in.close();
|
os.close();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
|
/**
|
* 验证static/img/下有没有当前文件 有返回true,没有返回false
|
*
|
* @param fileName
|
* @return
|
*/
|
public static boolean checkImgFile(String fileName) {
|
if (null == fileName) return false;
|
|
try {
|
ClassPathResource classPathResource = new ClassPathResource("static/img/" + fileName);
|
log.debug("---获取IMG下面文件路径-{}", classPathResource.getURL().getPath());
|
return classPathResource.exists();
|
} catch (Exception e) {
|
log.warn("---获取IMG下面文件路径失败:{}", e.getMessage());
|
return false;
|
}
|
}
|
|
|
/**
|
* 根据文件路径+文件名创建
|
*
|
* @param path
|
* @return
|
*/
|
public static boolean createFile(String path) {
|
if (path.contains(".")) {
|
int partition1 = path.lastIndexOf("/");
|
if (partition1 == -1) {
|
partition1 = path.lastIndexOf("\\");
|
}
|
// 纯文件名
|
String fileName = path.substring(partition1 + 1);
|
String filePath = path.substring(0, partition1);
|
return createNewFile(fileName, filePath);
|
} else {
|
return createCatalog(path);
|
}
|
}
|
|
/**
|
* 根据路径和文件名称创建
|
*
|
* @param fileName
|
* @param filePath
|
* @return
|
*/
|
public static boolean createNewFile(String fileName, String filePath) {
|
if (createCatalog(filePath)) {
|
File f = new File(filePath, fileName);
|
try {
|
return f.createNewFile();
|
} catch (IOException e) {
|
new RuntimeException("创建文件失败");
|
e.printStackTrace();
|
}
|
}
|
return false;
|
|
}
|
|
/**
|
* 创建文件夹
|
*
|
* @param path
|
* @return
|
*/
|
private static boolean createCatalog(String path) {
|
File f = new File(path);
|
if (!f.exists()) {
|
return f.mkdirs();
|
}
|
return true;
|
}
|
|
|
/**
|
* 根据组织编码获取当前用户的企业LOGO
|
*
|
* @param companyId
|
* @return
|
*/
|
public static String getLogoByCompanyId(String companyId) {
|
String logoName = "logo-" + companyId + ".png";
|
boolean isExists = FilesUtil.checkImgFile(logoName);
|
if (isExists) return logoName;
|
return "logo-default.png";
|
}
|
|
/**
|
* 根据用户所在组织获取系统首页系统名称
|
*
|
* @param companyId
|
* @return
|
*/
|
public static String getLogoTitleByCompanyId(String companyId) {
|
String logoName = "logo-title-" + companyId + ".png";
|
boolean isExists = FilesUtil.checkImgFile(logoName);
|
if (isExists) return logoName;
|
return "logo-title-default.png";
|
}
|
|
|
/**
|
* 根据组织编码获取当前用户的企业库区鸟瞰图
|
*
|
* @param companyId
|
* @return
|
*/
|
public static String getAerialByCompanyId(String companyId) {
|
String logoName = "aerial-" + companyId + ".png";
|
boolean isExists = FilesUtil.checkImgFile(logoName);
|
if (isExists) return logoName;
|
return "aerial-default.png";
|
}
|
|
/**
|
* 根据当前人所在分库ID获取鸟瞰图
|
*
|
* @param deptId
|
* @return
|
*/
|
public static String getAerialByDeptId(String deptId) {
|
String logoName = "aerial-" + deptId + ".png";
|
boolean isExists = FilesUtil.checkImgFile(logoName);
|
if (isExists) return logoName;
|
return "aerial-default.png";
|
}
|
|
|
/**
|
* 返回项目临时目录
|
*
|
* @param companyId
|
* @return
|
*/
|
public String getTempPath(String companyId) {
|
String path = configData.getFilePath() + "TEMP/" + companyId;
|
|
createCatalog(path);
|
|
return path;
|
}
|
|
/**
|
* 获取系统配置文件夹
|
*
|
* @return
|
*/
|
public String getConfPath() {
|
return configData.getFilePath() + "CONF";
|
}
|
}
|