123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- package org.jeecg.common.util;
- import java.io.*;
- import java.sql.Connection;
- import java.sql.DatabaseMetaData;
- import java.sql.SQLException;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import javax.sql.DataSource;
- import org.jeecg.common.constant.CommonConstant;
- import org.jeecg.common.constant.DataBaseConstant;
- import org.jeecg.common.util.filter.FileTypeFilter;
- import org.jeecg.common.util.oss.OssBootUtil;
- import org.jeecgframework.poi.util.PoiPublicUtil;
- import org.springframework.util.FileCopyUtils;
- import org.springframework.web.multipart.MultipartFile;
- import com.baomidou.mybatisplus.annotation.DbType;
- import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
- import lombok.extern.slf4j.Slf4j;
- @Slf4j
- public class CommonUtils {
- //中文正则
- private static Pattern ZHONGWEN_PATTERN = Pattern.compile("[\u4e00-\u9fa5]");
- public static String uploadOnlineImage(byte[] data,String basePath,String bizPath,String uploadType){
- String dbPath = null;
- String fileName = "image" + Math.round(Math.random() * 100000000000L);
- fileName += "." + PoiPublicUtil.getFileExtendName(data);
- try {
- if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
- File file = new File(basePath + File.separator + bizPath + File.separator );
- if (!file.exists()) {
- file.mkdirs();// 创建文件根目录
- }
- String savePath = file.getPath() + File.separator + fileName;
- File savefile = new File(savePath);
- FileCopyUtils.copy(data, savefile);
- dbPath = bizPath + File.separator + fileName;
- }else {
- InputStream in = new ByteArrayInputStream(data);
- String relativePath = bizPath+"/"+fileName;
- if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
- dbPath = MinioUtil.upload(in,relativePath);
- }else if(CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)){
- dbPath = OssBootUtil.upload(in,relativePath);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return dbPath;
- }
- /**
- * 判断文件名是否带盘符,重新处理
- * @param fileName
- * @return
- */
- public static String getFileName(String fileName){
- //判断是否带有盘符信息
- // Check for Unix-style path
- int unixSep = fileName.lastIndexOf('/');
- // Check for Windows-style path
- int winSep = fileName.lastIndexOf('\\');
- // Cut off at latest possible point
- int pos = (winSep > unixSep ? winSep : unixSep);
- if (pos != -1) {
- // Any sort of path separator found...
- fileName = fileName.substring(pos + 1);
- }
- //替换上传文件名字的特殊字符
- fileName = fileName.replace("=","").replace(",","").replace("&","")
- .replace("#", "").replace("“", "").replace("”", "");
- //替换上传文件名字中的空格
- fileName=fileName.replaceAll("\\s","");
- return fileName;
- }
- // java 判断字符串里是否包含中文字符
- public static boolean ifContainChinese(String str) {
- if(str.getBytes().length == str.length()){
- return false;
- }else{
- Matcher m = ZHONGWEN_PATTERN.matcher(str);
- if (m.find()) {
- return true;
- }
- return false;
- }
- }
- /**
- * 统一全局上传
- * @Return: java.lang.String
- */
- public static String upload(MultipartFile file, String bizPath, String uploadType) {
- String url = "";
- if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
- url = MinioUtil.upload(file,bizPath);
- }else{
- url = OssBootUtil.upload(file,bizPath);
- }
- return url;
- }
- /**
- * 本地文件上传
- * @param mf 文件
- * @param bizPath 自定义路径
- * @return
- */
- public static String uploadLocal(MultipartFile mf,String bizPath,String uploadpath){
- try {
- //update-begin-author:liusq date:20210809 for: 过滤上传文件类型
- FileTypeFilter.fileTypeFilter(mf);
- //update-end-author:liusq date:20210809 for: 过滤上传文件类型
- String fileName = null;
- File file = new File(uploadpath + File.separator + bizPath + File.separator );
- if (!file.exists()) {
- file.mkdirs();// 创建文件根目录
- }
- String orgName = mf.getOriginalFilename();// 获取文件名
- orgName = CommonUtils.getFileName(orgName);
- if(orgName.indexOf(".")!=-1){
- fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
- }else{
- fileName = orgName+ "_" + System.currentTimeMillis();
- }
- String savePath = file.getPath() + File.separator + fileName;
- File savefile = new File(savePath);
- FileCopyUtils.copy(mf.getBytes(), savefile);
- String dbpath = null;
- if(oConvertUtils.isNotEmpty(bizPath)){
- dbpath = bizPath + File.separator + fileName;
- }else{
- dbpath = fileName;
- }
- if (dbpath.contains("\\")) {
- dbpath = dbpath.replace("\\", "/");
- }
- return dbpath;
- } catch (IOException e) {
- log.error(e.getMessage(), e);
- }catch (Exception e) {
- log.error(e.getMessage(), e);
- }
- return "";
- }
- /**
- * 统一全局上传 带桶
- * @Return: java.lang.String
- */
- public static String upload(MultipartFile file, String bizPath, String uploadType, String customBucket) {
- String url = "";
- if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
- url = MinioUtil.upload(file,bizPath,customBucket);
- }else{
- url = OssBootUtil.upload(file,bizPath,customBucket);
- }
- return url;
- }
- /** 当前系统数据库类型 */
- private static String DB_TYPE = "";
- private static DbType dbTypeEnum = null;
- /**
- * 全局获取平台数据库类型(作废了)
- * @return
- */
- @Deprecated
- public static String getDatabaseType() {
- if(oConvertUtils.isNotEmpty(DB_TYPE)){
- return DB_TYPE;
- }
- DataSource dataSource = SpringContextUtils.getApplicationContext().getBean(DataSource.class);
- try {
- return getDatabaseTypeByDataSource(dataSource);
- } catch (SQLException e) {
- //e.printStackTrace();
- log.warn(e.getMessage(),e);
- return "";
- }
- }
- /**
- * 全局获取平台数据库类型(对应mybaisPlus枚举)
- * @return
- */
- public static DbType getDatabaseTypeEnum() {
- if (oConvertUtils.isNotEmpty(dbTypeEnum)) {
- return dbTypeEnum;
- }
- try {
- DataSource dataSource = SpringContextUtils.getApplicationContext().getBean(DataSource.class);
- dbTypeEnum = JdbcUtils.getDbType(dataSource.getConnection().getMetaData().getURL());
- return dbTypeEnum;
- } catch (SQLException e) {
- log.warn(e.getMessage(), e);
- return null;
- }
- }
- /**
- * 获取数据库类型
- * @param dataSource
- * @return
- * @throws SQLException
- */
- private static String getDatabaseTypeByDataSource(DataSource dataSource) throws SQLException{
- if("".equals(DB_TYPE)) {
- Connection connection = dataSource.getConnection();
- try {
- DatabaseMetaData md = connection.getMetaData();
- String dbType = md.getDatabaseProductName().toLowerCase();
- if(dbType.indexOf("mysql")>=0) {
- DB_TYPE = DataBaseConstant.DB_TYPE_MYSQL;
- }else if(dbType.indexOf("oracle")>=0 ||dbType.indexOf("dm")>=0) {
- DB_TYPE = DataBaseConstant.DB_TYPE_ORACLE;
- }else if(dbType.indexOf("sqlserver")>=0||dbType.indexOf("sql server")>=0) {
- DB_TYPE = DataBaseConstant.DB_TYPE_SQLSERVER;
- }else if(dbType.indexOf("postgresql")>=0) {
- DB_TYPE = DataBaseConstant.DB_TYPE_POSTGRESQL;
- }else if(dbType.indexOf("mariadb")>=0) {
- DB_TYPE = DataBaseConstant.DB_TYPE_MARIADB;
- }else {
- log.error("数据库类型:[" + dbType + "]不识别!");
- //throw new JeecgBootException("数据库类型:["+dbType+"]不识别!");
- }
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- }finally {
- connection.close();
- }
- }
- return DB_TYPE;
- }
- }
|