123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- package com.fdkankan.fusion.common.util;
- import cn.hutool.core.io.FileUtil;
- import com.fdkankan.fusion.config.CacheUtil;
- import com.fdkankan.fusion.response.FileInfoVo;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.io.FileUtils;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Component;
- import java.io.File;
- import java.nio.charset.Charset;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- @Component
- @Slf4j
- public class LocalToOssUtil {
- @Value("${oss.bucket:4dkankan}")
- private String bucket;
- private String getOssPath(String bucket, String filePath) {
- return CacheUtil.basePath.concat(bucket).concat(File.separator).concat(filePath);
- }
- public String getOssPath( String filePath) {
- return CacheUtil.basePath.concat(bucket).concat(File.separator).concat(filePath);
- }
- public boolean existKey(String objectName) {
- return FileUtil.exist(getOssPath(bucket, objectName));
- }
- public boolean downFormRel(String objectName, String localPath) {
- try {
- FileUtil.copyContent(new File(getOssPath(bucket, objectName)), new File(localPath), true);
- }catch (Exception e){
- log.info("下载文件失败,remoteFilePath:{},localPath:{}", objectName, localPath);
- log.info("下载文件失败", e);
- return false;
- }
- return true;
- }
- public boolean downFormAbs(String objectName, String localPath) {
- try {
- FileUtil.copyContent(new File( objectName), new File(localPath), true);
- }catch (Exception e){
- log.info("下载文件失败,remoteFilePath:{},localPath:{}", objectName, localPath);
- log.info("下载文件失败", e);
- return false;
- }
- return true;
- }
- public String uploadFile(String bucket, String filePath, String remoteFilePath, Map<String, String> headers) {
- try {
- if (!new File(filePath).exists()) {
- log.warn("文件不存在:{}", filePath);
- return null;
- }
- FileUtil.copyContent(new File(filePath), new File(getOssPath(bucket, remoteFilePath)), true);
- //FileUtils.contentEqualsIgnoreEOL(new File(filePath),new File(getOssPath(bucket, remoteFilePath)), "UTF-8");
- }catch (Exception e){
- log.error("上传文件失败,filePath:{},remoteFilePath:{}", filePath, remoteFilePath);
- log.error("上传文件失败", e);
- }
- return null;
- }
- public void uploadOss(String filePath, String key1) {
- uploadFile(bucket, filePath, key1, null);
- }
- public void delete(String objectName) {
- FileUtil.del(getOssPath(bucket, objectName));
- }
- public void uploadFileOss(File file) {
- if(file.isFile()){
- String ossPath = file.getPath();
- ossPath = ossPath.replace("/mnt/","");
- ossPath = ossPath.replace("\\","/");
- this.uploadOss(file.getPath(),ossPath);
- }else {
- File[] files = file.listFiles();
- for (File file1 : files) {
- uploadFileOss(file1);
- }
- }
- }
- public List<String> listKeysFromAli(String sourcePath) {
- return FileUtil.loopFiles(getOssPath(bucket, sourcePath)).stream()
- .map(f -> f.getAbsolutePath().replace(CacheUtil.basePath.concat(bucket).concat(File.separator), ""))
- .collect(Collectors.toList());
- }
- public void copyFile(String sourcePath, String targetPath) {
- try {
- FileUtil.copyContent(new File(getOssPath(bucket, sourcePath)), new File(getOssPath(bucket, targetPath)), true);
- }catch (Exception e){
- log.error("复制文件失败,sourcePath:{},targetPath:{}",sourcePath,targetPath);
- log.error("复制文件失败", e);
- }
- }
- public FileInfoVo getFileInfo(String filePath) {
- return MD5Checksum.getFileInfo(getOssPath(bucket, filePath));
- }
- public Long getSizeCount(String filePath) {
- log.info("getSize:{}",filePath);
- File file = new File(filePath);
- if(file.isFile()){
- return file.length();
- }
- File[] files = file.listFiles();
- if(files == null){
- return 0L;
- }
- Long size = 0L;
- for (File file1 : files) {
- size+= getSizeCount(file1.getPath());
- }
- return size;
- }
- public Long getSize(String filePath) {
- return getSizeCount( getOssPath(bucket, filePath));
- }
- public String getObjectContent(String s) {
- return FileUtil.readUtf8String(new File(getOssPath(s)));
- }
- }
|