文件存储 FileUtil FileBaseDto

package com.guige.base.fileutils;


import com.alibaba.fastjson.JSONArray;
import com.aliyun.oss.ServiceException;
import com.guige.base.dto.FileBaseDto;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author songaw
 * @date 2018/4/9 17:07
 */
public abstract class FileUtil {
    public static final String FILE_LOCAL="0";
    public static final String FILE_OSS="1";
    public static final String FILE_FTP="2";
    private String storageType;
    private Map<String,String> linkConf =new HashMap<>();


    /**
     * 根据一个配置得到对应的FileUtil
     * @param storageType FileUtil类型 0本地 1 OSS 2FTP
     * @return
     * @throws Exception
     */
    public static FileUtil sysConfigOfFileUtil(Map<String, String> linkConf, String storageType) throws Exception {
        FileUtil fileUtil = null;
        List<FileUtil> resultFileUtils = new ArrayList<>();
        if (linkConf==null||linkConf.isEmpty()) {
            return fileUtil;
        }

        if (linkConf==null||linkConf.isEmpty()) {
            return fileUtil;
        }
        List<Map<String, String>> linkConfList = new ArrayList<>();
        linkConfList.add(linkConf);
        resultFileUtils = sysConfigOfFileUtil(linkConfList, storageType);
        if(CollectionUtils.isNotEmpty(resultFileUtils)){
            fileUtil= resultFileUtils.get(0);
        }
        return fileUtil;
    }
    /**
     * 根据一组链接配置 得到一组FileUtil
     * @param confs 一组配置
     * @param storageType FileUtil类型0本地 1 OSS 2FTP
     * @return
     * @throws Exception
     */
    public static List<FileUtil> sysConfigOfFileUtil(List<Map<String, String>> confs, String storageType) throws Exception {
        List<FileUtil> fileUtils = new ArrayList<>();
        boolean isLinkSuccess = false;
        try {
            if (confs == null || confs.size() == 0) {
                return fileUtils;
            }
            for (int i = 0; i < confs.size(); i++) {
                Map<String, String> map = confs.get(i);
                FileUtil fileUtil = null;
                try {
                    switch (storageType) {
                        case "0":
                            fileUtil = new LocalFileUtil(map.get("ROOT_PATH"));
                            break;
                        case "1":
                            fileUtil = new OssFileUtil(map.get("END_POINT"), map.get("ACCESS_KEY_ID"), map.get("ACCESS_KEY_SECRET"), map.get("BUCKET_NAME"), map.get("PROXY_HOST"), map.get("PROXY_PORT"));
                            break;
                        case "2":
                            fileUtil = new FtpFileUtil(map.get("HOST"), Integer.parseInt(map.get("PORT")), map.get("USERNAME"), map.get("PASSWORD"));
                            break;
                    }
                    isLinkSuccess = true;
                    if (fileUtil != null) {
                        fileUtils.add(fileUtil);
                    }
                } catch (Exception e) {
                    continue;
                }
            }
            if (!isLinkSuccess) {
                throw new RuntimeException("文件管理器创建失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("文件管理器链接失败");
        }
        return fileUtils;
    }
    //上传文件

    /**
     * 保存文件
     *
     * @param path
     * @param filename
     * @param input
     * @return
     */
    public abstract boolean saveFile(String path, String filename, InputStream input, boolean replace_existing) throws IOException;

    /**
     * 保存文件
     *
     * @param path
     * @param filename
     * @param fileDate
     * @return
     * @throws IOException
     */
    public  abstract boolean saveFile(String path, String filename, byte[] fileDate, boolean replace_existing) throws IOException;

    /**
     * 保存文件base 64
     *
     * @param path
     * @param filename
     * @param base64Data
     * @return
     * @throws IOException
     */
    public abstract boolean saveFile(String path, String filename, String base64Data, boolean replace_existing) throws IOException;

    /**
     * 保存文件
     *
     * @param path     路径
     * @param filename 文件名称
     * @param file     文件
     * @return
     * @throws IOException
     */
    public abstract boolean saveFile(String path, String filename, File file, boolean replace_existing) throws IOException;

    /**
     * 保存文件 直接保存文件  文件名是File的name
     *
     * @param path 路径
     * @param file 文件
     * @return
     * @throws IOException
     */
    public abstract boolean saveFile(String path, File file, boolean replace_existing) throws IOException;

    /**
     * 上传多个文件
     *
     * @param path
     * @param files
     * @return
     * @throws IOException
     */
    public abstract boolean saveFile(String path, List<File> files, boolean replace_existing) throws IOException;

    /**
     * 上传已经存在的文件 到FTP或者 OSS
     *
     * @param path
     * @param urls
     * @return
     * @throws IOException
     */
    public abstract boolean saveLocalFile(String path, List<String> urls, boolean replace_existing) throws IOException;

    /**
     * 读取文件
     *
     * @param path
     * @param filename
     * @return
     */
    public abstract InputStream readInputStream(String path, String filename) throws IOException;

    /**
     * 查询一个目录下的所有文件
     *
     * @param pathStr
     * @return
     */
    public abstract List<FileBaseDto> readFileList(String pathStr, boolean isReadDir) throws IOException;

    /**
     * 查询文件
     *
     * @param path
     * @param filename
     * @return
     */
    public abstract FileBaseDto readFileInfo(String path, String filename) throws IOException;

    /**
     * 删除文件
     *
     * @param path 文件所在路径
     * @return
     */
    public abstract boolean delete(String path) throws IOException;

    /**
     * 删除多个文件
     *
     * @param paths
     * @return
     */
    public abstract boolean delete(List<String> paths) throws IOException;


    /**
     * 判断文件是否存在
     *
     * @param path
     * @return
     */
    public abstract boolean exists(String path) throws IOException;

    /**
     * 移动文件
     *
     * @param path             原路径 可以是文件夹也可以是文件
     * @param newPath          目标路径 跟原路径格式保持一致
     * @param replace_existing 是否替換 (如果为true并且目标文件夹存在则会删除目标文件夹 然后进行移动)
     * @return
     */
    public abstract boolean moveTo(String path, String newPath, boolean replace_existing) throws IOException;

    /**
     * 复制文件
     *
     * @param path             原路径 可以是文件夹也可以是文件
     * @param newPath          目标路径 跟原路径格式保持一致
     * @param replace_existing 是否替換 (如果为true并且目标文件夹存在则会删除目标文件夹 然后进行复制)
     * @return
     */
    public abstract boolean copy(String path, String newPath, boolean replace_existing) throws IOException;


    public String getContentType(String fileName) {
        if (fileName.contains(".")) {
            String fileExtension = fileName.substring(fileName.lastIndexOf("."));

            switch (fileExtension) {
                case "asf":
                    return "video/x-ms-asf";

                case "avi":
                    return "video/avi";

                case "doc":
                    return "application/msword";

                case "docx":
                    return "application/msword";

                case "zip":
                    return "application/zip";

                case "xls":
                    return "application/vnd.ms-excel";

                case "xlsx":
                    return "application/vnd.ms-excel";

                case "gif":
                    return "image/gif";

                case "jpg":
                    return "image/jpeg";

                case "png":
                    return "image/jpeg";

                case "jpeg":
                    return "image/jpeg";

                case "wav":
                    return "audio/wav";

                case "mp3":
                    return "audio/mpeg3";

                case "mpg":
                    return "video/mpeg ";

                case "mepg":
                    return "video/mpeg";

                case "rtf":
                    return "application/rtf";

                case "html":
                    return "text/html";

                case "mht":
                    return "message/rfc822";

                case "mhtl":
                    return "message/rfc822";

                case "txt":
                    return "text/plain";

                default:
                    // throw new RuntimeException("fileutils type:" + ext + "can't be downloaded.");
                    return "multipart/form-data";
            }
        }
        return "dir";
    }

    public byte[] inputStream2ByteArray(File file) throws IOException {

        InputStream in = new FileInputStream(file);
        byte[] data = toByteArray(in);
        in.close();

        return data;
    }

    public byte[] toByteArray(InputStream in) throws IOException {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024 * 4];
        int n = 0;
        while ((n = in.read(buffer)) != -1) {
            out.write(buffer, 0, n);
        }
        return out.toByteArray();
    }
    public String getStorageType() {
        return storageType;
    }

    public void setStorageType(String storageType) {
        this.storageType = storageType;
    }

    public Map<String, String> getLinkConf() {
        return linkConf;
    }

    public void setLinkConf(Map<String, String> linkConf) {
        this.linkConf = linkConf;
    }
}
package com.guige.base.dto;

/**
 * 文件管理帮助类
 *
 * @author songaw
 * @date 2018/4/13 10:55
 */

public class FileBaseDto {
    /**
     * 文件的路径
     */
    public String filePath;
    /**
     * 文件名
     */
    public String originName;
    /**
     * 文件大小
     */
    private Long fileSize;
    /**
     * 类型
     */
    public String contentType;

    /**
     * 存储类型,0 LOCAL, 1 OSS 2.FTP
     */
    private Integer storageType;


    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getOriginName() {
        return originName;
    }

    public void setOriginName(String originName) {
        this.originName = originName;
    }

    public Long getFileSize() {
        return fileSize;
    }

    public void setFileSize(Long fileSize) {
        this.fileSize = fileSize;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }



    public Integer getStorageType() {
        return storageType;
    }

    public void setStorageType(Integer storageType) {
        this.storageType = storageType;
    }
}

猜你喜欢

转载自www.cnblogs.com/songanwei/p/9098734.html
今日推荐