8222acc37ea5de320cf6de01173821b7db0c35bb.svn-base 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package org.jeecg.common.util;
  2. import java.io.*;
  3. import java.sql.Connection;
  4. import java.sql.DatabaseMetaData;
  5. import java.sql.SQLException;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. import javax.sql.DataSource;
  9. import org.jeecg.common.constant.CommonConstant;
  10. import org.jeecg.common.constant.DataBaseConstant;
  11. import org.jeecg.common.util.filter.FileTypeFilter;
  12. import org.jeecg.common.util.oss.OssBootUtil;
  13. import org.jeecgframework.poi.util.PoiPublicUtil;
  14. import org.springframework.util.FileCopyUtils;
  15. import org.springframework.web.multipart.MultipartFile;
  16. import com.baomidou.mybatisplus.annotation.DbType;
  17. import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
  18. import lombok.extern.slf4j.Slf4j;
  19. @Slf4j
  20. public class CommonUtils {
  21. //中文正则
  22. private static Pattern ZHONGWEN_PATTERN = Pattern.compile("[\u4e00-\u9fa5]");
  23. public static String uploadOnlineImage(byte[] data,String basePath,String bizPath,String uploadType){
  24. String dbPath = null;
  25. String fileName = "image" + Math.round(Math.random() * 100000000000L);
  26. fileName += "." + PoiPublicUtil.getFileExtendName(data);
  27. try {
  28. if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
  29. File file = new File(basePath + File.separator + bizPath + File.separator );
  30. if (!file.exists()) {
  31. file.mkdirs();// 创建文件根目录
  32. }
  33. String savePath = file.getPath() + File.separator + fileName;
  34. File savefile = new File(savePath);
  35. FileCopyUtils.copy(data, savefile);
  36. dbPath = bizPath + File.separator + fileName;
  37. }else {
  38. InputStream in = new ByteArrayInputStream(data);
  39. String relativePath = bizPath+"/"+fileName;
  40. if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
  41. dbPath = MinioUtil.upload(in,relativePath);
  42. }else if(CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)){
  43. dbPath = OssBootUtil.upload(in,relativePath);
  44. }
  45. }
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. return dbPath;
  50. }
  51. /**
  52. * 判断文件名是否带盘符,重新处理
  53. * @param fileName
  54. * @return
  55. */
  56. public static String getFileName(String fileName){
  57. //判断是否带有盘符信息
  58. // Check for Unix-style path
  59. int unixSep = fileName.lastIndexOf('/');
  60. // Check for Windows-style path
  61. int winSep = fileName.lastIndexOf('\\');
  62. // Cut off at latest possible point
  63. int pos = (winSep > unixSep ? winSep : unixSep);
  64. if (pos != -1) {
  65. // Any sort of path separator found...
  66. fileName = fileName.substring(pos + 1);
  67. }
  68. //替换上传文件名字的特殊字符
  69. fileName = fileName.replace("=","").replace(",","").replace("&","")
  70. .replace("#", "").replace("“", "").replace("”", "");
  71. //替换上传文件名字中的空格
  72. fileName=fileName.replaceAll("\\s","");
  73. return fileName;
  74. }
  75. // java 判断字符串里是否包含中文字符
  76. public static boolean ifContainChinese(String str) {
  77. if(str.getBytes().length == str.length()){
  78. return false;
  79. }else{
  80. Matcher m = ZHONGWEN_PATTERN.matcher(str);
  81. if (m.find()) {
  82. return true;
  83. }
  84. return false;
  85. }
  86. }
  87. /**
  88. * 统一全局上传
  89. * @Return: java.lang.String
  90. */
  91. public static String upload(MultipartFile file, String bizPath, String uploadType) {
  92. String url = "";
  93. if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
  94. url = MinioUtil.upload(file,bizPath);
  95. }else{
  96. url = OssBootUtil.upload(file,bizPath);
  97. }
  98. return url;
  99. }
  100. /**
  101. * 本地文件上传
  102. * @param mf 文件
  103. * @param bizPath 自定义路径
  104. * @return
  105. */
  106. public static String uploadLocal(MultipartFile mf,String bizPath,String uploadpath){
  107. try {
  108. //update-begin-author:liusq date:20210809 for: 过滤上传文件类型
  109. FileTypeFilter.fileTypeFilter(mf);
  110. //update-end-author:liusq date:20210809 for: 过滤上传文件类型
  111. String fileName = null;
  112. File file = new File(uploadpath + File.separator + bizPath + File.separator );
  113. if (!file.exists()) {
  114. file.mkdirs();// 创建文件根目录
  115. }
  116. String orgName = mf.getOriginalFilename();// 获取文件名
  117. orgName = CommonUtils.getFileName(orgName);
  118. if(orgName.indexOf(".")!=-1){
  119. fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
  120. }else{
  121. fileName = orgName+ "_" + System.currentTimeMillis();
  122. }
  123. String savePath = file.getPath() + File.separator + fileName;
  124. File savefile = new File(savePath);
  125. FileCopyUtils.copy(mf.getBytes(), savefile);
  126. String dbpath = null;
  127. if(oConvertUtils.isNotEmpty(bizPath)){
  128. dbpath = bizPath + File.separator + fileName;
  129. }else{
  130. dbpath = fileName;
  131. }
  132. if (dbpath.contains("\\")) {
  133. dbpath = dbpath.replace("\\", "/");
  134. }
  135. return dbpath;
  136. } catch (IOException e) {
  137. log.error(e.getMessage(), e);
  138. }catch (Exception e) {
  139. log.error(e.getMessage(), e);
  140. }
  141. return "";
  142. }
  143. /**
  144. * 统一全局上传 带桶
  145. * @Return: java.lang.String
  146. */
  147. public static String upload(MultipartFile file, String bizPath, String uploadType, String customBucket) {
  148. String url = "";
  149. if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
  150. url = MinioUtil.upload(file,bizPath,customBucket);
  151. }else{
  152. url = OssBootUtil.upload(file,bizPath,customBucket);
  153. }
  154. return url;
  155. }
  156. /** 当前系统数据库类型 */
  157. private static String DB_TYPE = "";
  158. private static DbType dbTypeEnum = null;
  159. /**
  160. * 全局获取平台数据库类型(作废了)
  161. * @return
  162. */
  163. @Deprecated
  164. public static String getDatabaseType() {
  165. if(oConvertUtils.isNotEmpty(DB_TYPE)){
  166. return DB_TYPE;
  167. }
  168. DataSource dataSource = SpringContextUtils.getApplicationContext().getBean(DataSource.class);
  169. try {
  170. return getDatabaseTypeByDataSource(dataSource);
  171. } catch (SQLException e) {
  172. //e.printStackTrace();
  173. log.warn(e.getMessage(),e);
  174. return "";
  175. }
  176. }
  177. /**
  178. * 全局获取平台数据库类型(对应mybaisPlus枚举)
  179. * @return
  180. */
  181. public static DbType getDatabaseTypeEnum() {
  182. if (oConvertUtils.isNotEmpty(dbTypeEnum)) {
  183. return dbTypeEnum;
  184. }
  185. try {
  186. DataSource dataSource = SpringContextUtils.getApplicationContext().getBean(DataSource.class);
  187. dbTypeEnum = JdbcUtils.getDbType(dataSource.getConnection().getMetaData().getURL());
  188. return dbTypeEnum;
  189. } catch (SQLException e) {
  190. log.warn(e.getMessage(), e);
  191. return null;
  192. }
  193. }
  194. /**
  195. * 获取数据库类型
  196. * @param dataSource
  197. * @return
  198. * @throws SQLException
  199. */
  200. private static String getDatabaseTypeByDataSource(DataSource dataSource) throws SQLException{
  201. if("".equals(DB_TYPE)) {
  202. Connection connection = dataSource.getConnection();
  203. try {
  204. DatabaseMetaData md = connection.getMetaData();
  205. String dbType = md.getDatabaseProductName().toLowerCase();
  206. if(dbType.indexOf("mysql")>=0) {
  207. DB_TYPE = DataBaseConstant.DB_TYPE_MYSQL;
  208. }else if(dbType.indexOf("oracle")>=0 ||dbType.indexOf("dm")>=0) {
  209. DB_TYPE = DataBaseConstant.DB_TYPE_ORACLE;
  210. }else if(dbType.indexOf("sqlserver")>=0||dbType.indexOf("sql server")>=0) {
  211. DB_TYPE = DataBaseConstant.DB_TYPE_SQLSERVER;
  212. }else if(dbType.indexOf("postgresql")>=0) {
  213. DB_TYPE = DataBaseConstant.DB_TYPE_POSTGRESQL;
  214. }else if(dbType.indexOf("mariadb")>=0) {
  215. DB_TYPE = DataBaseConstant.DB_TYPE_MARIADB;
  216. }else {
  217. log.error("数据库类型:[" + dbType + "]不识别!");
  218. //throw new JeecgBootException("数据库类型:["+dbType+"]不识别!");
  219. }
  220. } catch (Exception e) {
  221. log.error(e.getMessage(), e);
  222. }finally {
  223. connection.close();
  224. }
  225. }
  226. return DB_TYPE;
  227. }
  228. }