通用后台管理系统(7)-编写文件接口、实现

文件接口

package com.sundablog.service.backend.system.upms.filesystem;

import java.util.Map;

/**
 * 文件处理接口类(文件上传 、文件下载等)
 * @ClassName:  FilesyStemService   
 * @Description:文件处理接口类(文件上传 、文件下载等) 
 * @author: 哒哒 
 * @date:   2018年3月30日 上午12:11:17   
 *     
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
public interface FilesyStemService {

    
    /**
     * layui文件上传组件上传
     * @Title: fileUploadLayui   
     * @Description: layui文件上传组件上传
     * @param: @param filContent
     * @param: @return      
     * @return: Map      
     * @throws
     */
    String fileUploadLayui(byte[] filContent); 
    
    
    
    /**
     * 获取文件下载URL
     * @Title: getDownloadUrl   
     * @Description: 获取文件下载URL  
     * @param: @param fileKey file 文件(全路径或者fileKey)
     * @param: @return      
     * @return: String      
     * @throws
     */
    String getDownloadUrl(String fileKey);
}

文件接口实现

package com.sundablog.service.impl.backend.system.upms.filesystem;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.jeesuite.filesystem.FSProvider;
import com.jeesuite.filesystem.UploadObject;
import com.jeesuite.filesystem.provider.aliyun.AliyunossProvider;
import com.jeesuite.filesystem.provider.fdfs.FdfsProvider;
import com.jeesuite.filesystem.provider.qiniu.QiniuProvider;
import com.sundablog.service.backend.system.upms.filesystem.FilesyStemService;

import cn.hutool.core.util.RandomUtil;

/**
 * 文件处理接口实现类(文件上传 、文件下载等)
 * 
 * @ClassName: FilesyStemServiceImpl
 * @Description:文件处理接口实现类(文件上传 、文件下载等)
 * @author: 哒哒
 * @date: 2018年3月30日 上午12:12:05
 * 
 * @Copyright: 2018 www.sundablog.com Inc. All rights reserved.
 */
@Service
public class FilesyStemServiceImpl implements FilesyStemService {

    @Value("${UPLOADING_QINIU_TYPE}")
    private int UPLOADING_QINIU_TYPE;

    /**
     *  qiniu配置
     */
    @Value("${QINIU_FILESYSTEM_PROVIDER}")
    private String QINIU_FILESYSTEM_PROVIDER;
    @Value("${QINIU_FILESYSTEM_BUCKETNAME}")
    private String QINIU_FILESYSTEM_BUCKETNAME;
    @Value("${QINIU_FILESYSTEM_URLPREFIX}")
    private String QINIU_FILESYSTEM_URLPREFIX;
    @Value("${QINIU_FILESYSTEM_ACCESSKEY}")
    private String QINIU_FILESYSTEM_ACCESSKEY;
    @Value("${QINIU_FILESYSTEM_SECRETKEY}")
    private String QINIU_FILESYSTEM_SECRETKEY;
    @Value("${QINIU_FILESYSTEM_PRIVATE}")
    private String QINIU_FILESYSTEM_PRIVATE;

    /**
     * FASTDFS
     */
    @Value("${FASTDFS_FILESYSTEM_PROVIDER}")
    private String FASTDFS_FILESYSTEM_PROVIDER;
    @Value("${FASTDFS_FILESYSTEM_SERVERS}")
    private String FASTDFS_FILESYSTEM_SERVERS;
    @Value("${FASTDFS_FILESYSTEM_URLPREFIX}")
    private String FASTDFS_FILESYSTEM_URLPREFIX;
    @Value("${FASTDFS_FILESYSTEM_CONNECTTIMEOUT}")
    private int FASTDFS_FILESYSTEM_CONNECTTIMEOUT;
    @Value("${FASTDFS_FILESYSTEM_MAXTHREADS}")
    private int FASTDFS_FILESYSTEM_MAXTHREADS;

    /**
     * aliyun
     */
    @Value("${ALIYUN_FILESYSTEM_PROVIDER}")
    private String ALIYUN_FILESYSTEM_PROVIDER;
    @Value("${ALIYUN_FILESYSTEM_BUCKETNAME}")
    private String ALIYUN_FILESYSTEM_BUCKETNAME;
    @Value("${ALIYUN_FILESYSTEM_ENDPOINT}")
    private String ALIYUN_FILESYSTEM_ENDPOINT;
    @Value("${ALIYUN_FILESYSTEM_ACCESSKEY}")
    private String ALIYUN_FILESYSTEM_ACCESSKEY;
    @Value("${ALIYUN_FILESYSTEM_SECRETKEY}")
    private String ALIYUN_FILESYSTEM_SECRETKEY;
    @Value("${ALIYUN_FILESYSTEM_URLPREFIX}")
    private String ALIYUN_FILESYSTEM_URLPREFIX;
    @Value("${ALIYUN_FILESYSTEM_PRIVATE}")
    private String ALIYUN_FILESYSTEM_PRIVATE;

    /**
     * layui文件上传组件上传 Title: fileUploadLayui Description:
     * 
     * @param filContent
     * @return
     * @see com.sundablog.service.backend.system.upms.filesystem.FilesyStemService#fileUploadLayui(byte[])
     */
    @Override
    public String fileUploadLayui(byte[] filContent) {
        String url; 
        if (UPLOADING_QINIU_TYPE == 0) {
            url = fdfsProvider(filContent);
        } else if (UPLOADING_QINIU_TYPE == 1) {
            url = aliyunossProvider(filContent);
        } else {
            url = qiniuProvider(filContent);
        }
        
        return url;
    }

    /**
     * 获取文件下载URL Title: getDownloadUrl Description:
     * 
     * @param fileKey
     * @return
     * @see com.sundablog.service.backend.system.upms.filesystem.FilesyStemService#getDownloadUrl(java.lang.String)
     */
    @Override
    public String getDownloadUrl(String fileKey) {
        String url; 
        if (UPLOADING_QINIU_TYPE == 0) {
            url = getFdfsDownloadUrl(fileKey);
        } else if (UPLOADING_QINIU_TYPE == 1) {
            url = getAliyunossDownloadUrl(fileKey);
        } else {
            url = getQiniuDownloadUrl(fileKey);
        }
        return url;
    }

    /**
     * 7牛云 oss 文件上传
     * @Title: qiniuProvider   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @param filContent
     * @param: @return      
     * @return: String      
     * @throws
     */
    private String qiniuProvider(byte[] filContent) {
        FSProvider provider = new QiniuProvider(QINIU_FILESYSTEM_URLPREFIX, QINIU_FILESYSTEM_BUCKETNAME,
                QINIU_FILESYSTEM_ACCESSKEY, QINIU_FILESYSTEM_SECRETKEY, true);
        String url = provider.upload(new UploadObject(RandomUtil.simpleUUID(), filContent));
        return url;
    }
    /**
     * 7牛云 文件下载url
     * @Title: getQiniuDownloadUrl   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @param fileKey
     * @param: @return      
     * @return: String      
     * @throws
     */
    public String getQiniuDownloadUrl(String fileKey) {
        FSProvider provider = new QiniuProvider(QINIU_FILESYSTEM_URLPREFIX, QINIU_FILESYSTEM_BUCKETNAME,
                QINIU_FILESYSTEM_ACCESSKEY, QINIU_FILESYSTEM_SECRETKEY, true);
        String url = provider.getDownloadUrl(fileKey);
        return url;
    }

    /**
     * 阿里云oss文件上传
     * @Title: aliyunossProvider   
     * @Description: 阿里云oss文件上传
     * @param: @param filContent
     * @param: @return      
     * @return: String      
     * @throws
     */
    private String aliyunossProvider(byte[] filContent) {
        FSProvider provider = new AliyunossProvider(ALIYUN_FILESYSTEM_URLPREFIX, ALIYUN_FILESYSTEM_ENDPOINT,
                ALIYUN_FILESYSTEM_BUCKETNAME, ALIYUN_FILESYSTEM_ACCESSKEY, ALIYUN_FILESYSTEM_SECRETKEY, true);
        String url = provider.upload(new UploadObject(RandomUtil.simpleUUID(), filContent));
        return url;
    }
    /**
     * 阿里云oss文件下载url
     * @Title: getAliyunossDownloadUrl   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @param fileKey
     * @param: @return      
     * @return: String      
     * @throws
     */
    public String getAliyunossDownloadUrl(String fileKey) {
        FSProvider provider = new AliyunossProvider(ALIYUN_FILESYSTEM_URLPREFIX, ALIYUN_FILESYSTEM_ENDPOINT,
                ALIYUN_FILESYSTEM_BUCKETNAME, ALIYUN_FILESYSTEM_ACCESSKEY, ALIYUN_FILESYSTEM_SECRETKEY, true);
        String url = provider.getDownloadUrl(fileKey);
        return url;
    }

    /**
     * fdfs文件上传
     * @Title: fdfsProvider   
     * @Description: fdfs文件上传
     * @param: @param filContent
     * @param: @return      
     * @return: String      
     * @throws
     */
    private String fdfsProvider(byte[] filContent) {
        FSProvider provider = new FdfsProvider(FASTDFS_FILESYSTEM_URLPREFIX, "group1",
                new String[] { FASTDFS_FILESYSTEM_SERVERS }, FASTDFS_FILESYSTEM_CONNECTTIMEOUT,
                FASTDFS_FILESYSTEM_MAXTHREADS);
        String url = provider.upload(new UploadObject(RandomUtil.simpleUUID(), filContent));
        return url;
    }
    /**
     * fdfs文件下载地址
     * @Title: getFdfsDownloadUrl   
     * @Description: TODO(这里用一句话描述这个方法的作用)   
     * @param: @param fileKey
     * @param: @return      
     * @return: String      
     * @throws
     */
    public String getFdfsDownloadUrl(String fileKey) {
        FSProvider provider = new FdfsProvider(FASTDFS_FILESYSTEM_URLPREFIX, "group1",
                new String[] { FASTDFS_FILESYSTEM_SERVERS }, FASTDFS_FILESYSTEM_CONNECTTIMEOUT,
                FASTDFS_FILESYSTEM_MAXTHREADS);
        String url = provider.getDownloadUrl(fileKey);
        return url;
    }

}

文件控制器

你可能感兴趣的:(通用后台管理系统(7)-编写文件接口、实现)