All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.feingto.cloud.kit.Filekit Maven / Gradle / Ivy

package com.feingto.cloud.kit;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;

/**
 * 文件操作工具类
 *
 * @author longfei
 */
@SuppressWarnings({"unused", "WeakerAccess"})
public class Filekit {
    public static String getFileExt(String fileName) {
        if (StringUtils.isBlank(fileName))
            return "";
        int lastIndex = fileName.lastIndexOf(".");
        return lastIndex >= 0 ? fileName.substring(lastIndex + 1).toLowerCase() : "";
    }

    /**
     * 得到不包含后缀的文件名字
     *
     * @param name 文件名
     * @return String
     */
    public static String getRealName(String name) {
        if (name == null)
            return "";
        int endIndex = name.lastIndexOf(".");
        return endIndex == -1 ? "" : name.substring(0, endIndex);
    }

    /**
     * 取得真实路径
     *
     * @param request HttpServletRequest
     * @return String
     */
    public static String getFileStorePath(HttpServletRequest request) {
        return request.getSession().getServletContext().getRealPath("/");
    }

    /**
     * 取得根路径webRoot
     *
     * @param request HttpServletRequest
     * @return String
     */
    public static String getContextPath(HttpServletRequest request) {
        return request.getSession().getServletContext().getContextPath();
    }

    /**
     * 文件流上传
     *
     * @param input InputStream
     * @param path  上传路径
     */
    public static void uploadFile(InputStream input, String path) {
        OutputStream os;
        try {
            os = new FileOutputStream(new File(path));
            int len;
            byte[] b = new byte[1024];
            while ((len = input.read(b)) != -1) {
                os.write(b, 0, len);
            }
            os.flush();
            os.close();
        } catch (IOException e) {
            throw new RuntimeException("InputStream write I/O exception.");
        }
    }

    /**
     * 剪切文件到目标路径
     *
     * @param request  HttpServletRequest
     * @param url      临时文件的相对路径,如:/temp/user-pic/20140322/201403221318490711.gif
     * @param destPath 相对目标路径,如:/attachment/user-pic/20140322
     */
    public static void cutFile(HttpServletRequest request, String url, String destPath) {
        if (StringUtils.isBlank(url) || StringUtils.isBlank(destPath))
            return;
        String fileName = url.substring(url.lastIndexOf("/") + 1, url.length());
        String srcPath = url.substring(0, url.lastIndexOf("/"));
        if (srcPath.equals(destPath))// 如果源路径和目标路径相等
            return;
        File srcFile = new File(getFileStorePath(request) + srcPath.replace("/", File.separator), fileName);
        File destFile = new File(getFileStorePath(request) + destPath.replace("/", File.separator), fileName);
        if (srcFile.exists()) {
            try {
                org.apache.commons.io.FileUtils.moveFile(srcFile, destFile);
            } catch (IOException e) {
                throw new RuntimeException("File move I/O exception.");
            }
        }
    }

    /**
     * 删除文件
     *
     * @param request HttpServletRequest
     * @param url     文件的相对路径,如:/attachment/user-pic/20140322/201403221318490711.gif
     */
    public static void deleteFile(HttpServletRequest request, String url) {
        if (StringUtils.isBlank(url))
            return;
        String uploadPath = getFileStorePath(request) + url.replace("/", File.separator);
        File file = new File(uploadPath);
        if (file.exists()) {
            try {
                org.apache.commons.io.FileUtils.forceDelete(file);
            } catch (IOException e) {
                throw new RuntimeException("File delete I/O exception.");
            }
        }
    }

    /**
     * 删除文件夹fileFolder中不存在于附件名称列表attachNames中的文件
     *
     * @param request     HttpServletRequest
     * @param fileFolder  文件夹url
     * @param attachNames 附件名称列表
     */
    public static void compareFile(HttpServletRequest request, String fileFolder, List attachNames) {
        String uploadPath = getFileStorePath(request) + fileFolder.replace("/", File.separator);
        File directory = new File(uploadPath);
        if (directory.exists() && directory.isDirectory()) {
            String names[] = directory.list();
            if (null != names) {
                File file;
                for (String name : names) {
                    if (!attachNames.contains(name)) {
                        file = new File(uploadPath + File.separator + name);
                        if (file.isFile()) {
                            try {
                                org.apache.commons.io.FileUtils.forceDelete(file);
                            } catch (IOException e) {
                                throw new RuntimeException("File delete I/O exception.");
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 统计文件夹下的文件数量(不递归)
     *
     * @param request    HttpServletRequest
     * @param fileFolder 文件夹url
     * @return Integer
     */
    public static Integer getFileNum(HttpServletRequest request, String fileFolder) {
        Integer num = 0;
        String uploadPath = getFileStorePath(request) + fileFolder.replace("/", File.separator);
        File directory = new File(uploadPath);
        if (directory.exists() && directory.isDirectory()) {
            String names[] = directory.list();
            num = names != null ? names.length : 0;
        }
        return num;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy