CommonUtils.java 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package org.jeecg.common.util;
  2. import org.jeecg.common.constant.CommonConstant;
  3. import org.jeecg.common.util.oss.OssBootUtil;
  4. import org.jeecgframework.poi.util.PoiPublicUtil;
  5. import org.springframework.util.FileCopyUtils;
  6. import java.io.ByteArrayInputStream;
  7. import java.io.File;
  8. import java.io.InputStream;
  9. public class CommonUtils {
  10. public static String uploadOnlineImage(byte[] data,String basePath,String bizPath,String uploadType){
  11. String dbPath = null;
  12. String fileName = "image" + Math.round(Math.random() * 100000000000L);
  13. fileName += "." + PoiPublicUtil.getFileExtendName(data);
  14. try {
  15. if(CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)){
  16. File file = new File(basePath + File.separator + bizPath + File.separator );
  17. if (!file.exists()) {
  18. file.mkdirs();// 创建文件根目录
  19. }
  20. String savePath = file.getPath() + File.separator + fileName;
  21. File savefile = new File(savePath);
  22. FileCopyUtils.copy(data, savefile);
  23. dbPath = bizPath + File.separator + fileName;
  24. }else {
  25. InputStream in = new ByteArrayInputStream(data);
  26. String relativePath = bizPath+"/"+fileName;
  27. if(CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)){
  28. dbPath = MinioUtil.upload(in,relativePath);
  29. }else if(CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)){
  30. dbPath = OssBootUtil.upload(in,relativePath);
  31. }
  32. }
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. return dbPath;
  37. }
  38. /**
  39. * 判断文件名是否带盘符,重新处理
  40. * @param fileName
  41. * @return
  42. */
  43. public static String getFileName(String fileName){
  44. //判断是否带有盘符信息
  45. // Check for Unix-style path
  46. int unixSep = fileName.lastIndexOf('/');
  47. // Check for Windows-style path
  48. int winSep = fileName.lastIndexOf('\\');
  49. // Cut off at latest possible point
  50. int pos = (winSep > unixSep ? winSep : unixSep);
  51. if (pos != -1) {
  52. // Any sort of path separator found...
  53. fileName = fileName.substring(pos + 1);
  54. }
  55. //替换上传文件名字的特殊字符
  56. fileName = fileName.replace("=","").replace(",","").replace("&","");
  57. return fileName;
  58. }
  59. }