bug
jiangqs
2023-07-26 add86a49cc69b6882500c95dd67a2ac826c35526
ruoyi-modules/ruoyi-file/src/main/java/com/ruoyi/file/utils/OBSUploadUtils.java
@@ -9,6 +9,7 @@
import com.ruoyi.common.core.utils.uuid.IdUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.InputStream;
import java.util.Calendar;
@@ -86,4 +87,50 @@
        return result.getResponse().getErrorResponseAsString();
    }
    public static String uploadLocalFile (File file) throws Exception {
        // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。
        String bucketName = "hongruitang";
        Calendar calendar = Calendar.getInstance();
        // 获取当前年
        String year = String.valueOf(calendar.get(Calendar.YEAR));
        // 获取当前月
        String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
        // 获取当前日
        String day = String.valueOf(calendar.get(Calendar.DATE));
        String filePath = year+"/"+month+"/"+day+"/";
        String uuid = IdUtils.fastSimpleUUID();
        // 创建OSSClient实例。
        OSS ossClient = createOss();
        PutObjectResult result = null;
        try {
            String fileName =file.getName();
            System.out.println(fileName + "开始上传");
            String prefix = fileName.substring(fileName.lastIndexOf("."));
            String objectName = filePath + uuid + prefix;
            // 创建PutObject请求。
            result = ossClient.putObject(bucketName,objectName,file);
            // 如果上传成功,则返回200。
            System.out.println(fileName + "上传返回" + result.getResponse().getStatusCode());
            return result.getResponse().getUri();
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return result.getResponse().getErrorResponseAsString();
    }
}