java110
2023-08-15 20001a775126c1be95a2b84cebb8813a54608712
java110-core/src/main/java/com/java110/core/client/OssUploadTemplate.java
@@ -1,12 +1,15 @@
package com.java110.core.client;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.java110.utils.cache.MappingCache;
import com.java110.core.factory.LogFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.utils.util.Base64Convert;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.OSSUtil;
import org.slf4j.Logger;
import com.java110.core.log.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@@ -43,6 +46,7 @@
        OSSClient ossClient = null;
        ByteArrayInputStream is = null;
        String urlPath = "";
        String resMsg = "上传成功";
        try {
            ossClient = OSSUtil.getOSSClient();
            fileName = UUID.randomUUID().toString();
@@ -65,10 +69,11 @@
            byte[] context = Base64Convert.base64ToByte(imageBase64);
            is = new ByteArrayInputStream(context);
            OSSUtil.uploadByInputStream(ossClient, is,  ftpPath + urlPath);
            OSSUtil.uploadByInputStream(ossClient, is, ftpPath + urlPath);
        } catch (Exception e) {
            logger.error("上传文件失败", e);
            throw new IllegalArgumentException("上传文件失败");
            resMsg = e.getMessage()+e.getLocalizedMessage();
            throw new IllegalArgumentException("上传文件失败" + e.getMessage());
        } finally {
            if (is != null) {
                try {
@@ -77,6 +82,9 @@
                    e.printStackTrace();
                }
            }
            LogFactory.saveOutLog("OSS", "文件", new ResponseEntity(resMsg, HttpStatus.OK));
        }
        return urlPath;
    }
@@ -109,6 +117,19 @@
            }
        }
        return fileName;
    }
    public String upload(InputStream inputStream, String ftpPath) {
        OSSClient ossClient = null;
        try {
            ossClient = OSSUtil.getOSSClient();
            OSSUtil.uploadByInputStream(ossClient, inputStream, ftpPath);
        } catch (Exception e) {
            // logger.error("上传文件失败", e);
            throw new IllegalArgumentException("上传文件失败");
        } finally {
        }
        return ftpPath;
    }
    /*
@@ -241,4 +262,20 @@
        return null;
    }
    public InputStream download(String fileName) {
        OSSClient ossClient = null;
        InputStream is = null;
        try {
            ossClient = OSSUtil.getOSSClient();
            is = OSSUtil.getInputStreamByOSS(ossClient, fileName);
            if (null == is) {
                throw new FileNotFoundException(fileName);
            }
        } catch (Exception e) {
            logger.error("ftp通过文件名称获取远程文件流", e);
        }
        return is;
    }
}