文件处理 FastDFS和OSS封装

本文详细探讨了如何在Java后端环境中对FastDFS和OSS两种文件存储系统进行封装,包括上传、下载、删除等操作的实现,旨在提升文件处理效率和系统集成能力。
摘要由CSDN通过智能技术生成
package com.XX.config.fastdfs;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.xx.config.util.CommonCheckUtil;
import com.xx.config.util.CommonUtil;
import com.xx.config.util.DbUtilUuid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Date;

/**
 * @author xx
 * @description
 * @date 2018/9/28 15:51
 **/
@Component
@Slf4j
public class FastDFSClientWrapper {
   
    @Autowired
    private FastFileStorageClient storageClient;

    @Value("${fdfs.downloadUrl}")
    private String fdfsUrl;
    /**#1fdfs 2:oss*/
    @Value("${file.type}")
    private String fileType;

    @Autowired
    private DbUtilUuid dbUtilUuid;

    @Value("${aliyun.oss.accesskeysecret}")
    private  String accessKeySecret;

    @Value("${aliyun.oss.accesskeyid}")
    private  String accessKeyId;

    @Value("${aliyun.oss.endpoint}")
    private  String endpoint;

    @Value("${aliyun.oss.bucketName}")
    private  String bucketName;
    @Value("${aliyun.oss.bucketImgUrl}")
    private String bucketImgUrl;

    /**oss设置URL过期时间为10年  3600l* 1000*24*365*10**/
    private Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 20);
    /**oss文件存储目录*/
    private String filedir = "data/file/";
    /**oss图片存储目录*/
    private String imagesdir = "data/images/";
    /**oss视频存储目录*/
    private String videodir = "data/video/";

    public OSSClient getOssClient() {
   
        return new OSSClient(endpoint, accessKeyId, accessKeySecret);
    }

    /**
     * 上传文件
     * @param file 文件对象
     * @return 文件访问地址
     * @throws IOException
     */
    public String uploadFile(MultipartFile file)  {
   
        String path = this.uploadFileNoIp(file);
        return fdfsUrl+"/"+path;
    }

    /**
     * 上传文件
     * @param inputStream 文件对象
     * @param inputStream 文件类型
     * @return 文件访问地址
     * @throws Exception
     */
    public String uploadFileInputStream(InputStream inputStream,String fileType)   {
   
        String url = this.uploadFileInputStreamNoIp(inputStream, fileType);
        if
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值