¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: Chill åºéª (smallchill@163.com) |
| | | */ |
| | | package org.springblade.core.boot.file; |
| | | |
| | | import org.springblade.core.tool.utils.StringPool; |
| | | import org.springblade.core.tool.utils.StringUtil; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * æä»¶å·¥å
·ç±» |
| | | * |
| | | * @author Chill |
| | | */ |
| | | public class BladeFileUtil { |
| | | |
| | | /** |
| | | * å®ä¹å
许ä¸ä¼ çæä»¶æ©å±å |
| | | */ |
| | | private static final HashMap<String, String> EXT_MAP = new HashMap<>(); |
| | | private static final String IS_DIR = "is_dir"; |
| | | private static final String FILE_NAME = "filename"; |
| | | private static final String FILE_SIZE = "filesize"; |
| | | |
| | | /** |
| | | * å¾çæ©å±å |
| | | */ |
| | | private static final String[] FILE_TYPES = new String[]{"gif", "jpg", "jpeg", "png", "bmp"}; |
| | | |
| | | static { |
| | | EXT_MAP.put("image", ".gif,.jpg,.jpeg,.png,.bmp,.JPG,.JPEG,.PNG"); |
| | | EXT_MAP.put("flash", ".swf,.flv"); |
| | | EXT_MAP.put("media", ".swf,.flv,.mp3,.mp4,.wav,.wma,.wmv,.mid,.avi,.mpg,.asf,.rm,.rmvb"); |
| | | EXT_MAP.put("file", ".doc,.docx,.xls,.xlsx,.ppt,.htm,.html,.txt,.zip,.rar,.gz,.bz2"); |
| | | EXT_MAP.put("allfile", ".gif,.jpg,.jpeg,.png,.bmp,.swf,.flv,.mp3,.mp4,.wav,.wma,.wmv,.mid,.avi,.mpg,.asf,.rm,.rmvb,.doc,.docx,.xls,.xlsx,.ppt,.htm,.html,.txt,.zip,.rar,.gz,.bz2"); |
| | | } |
| | | |
| | | /** |
| | | * è·åæä»¶åç¼ |
| | | * |
| | | * @param fileName æä»¶å |
| | | * @return String è¿ååç¼ |
| | | */ |
| | | public static String getFileExt(String fileName) { |
| | | return fileName.substring(fileName.lastIndexOf(StringPool.DOT)); |
| | | } |
| | | |
| | | /** |
| | | * æµè¯æä»¶åç¼ åªè®©æå®åç¼çæä»¶ä¸ä¼ ï¼åjsp,war,shçå±é©çåç¼ç¦æ¢ |
| | | * |
| | | * @param dir ç®å½ |
| | | * @param fileName æä»¶å |
| | | * @return è¿åæåä¸å¦ |
| | | */ |
| | | public static boolean testExt(String dir, String fileName) { |
| | | String fileExt = getFileExt(fileName); |
| | | String ext = EXT_MAP.get(dir); |
| | | return StringUtil.isNotBlank(ext) && (ext.contains(fileExt.toLowerCase()) || ext.contains(fileExt.toUpperCase())); |
| | | } |
| | | |
| | | /** |
| | | * æä»¶ç®¡çæåº |
| | | */ |
| | | public enum FileSort { |
| | | |
| | | /** |
| | | * å¤§å° |
| | | */ |
| | | size, |
| | | |
| | | /** |
| | | * ç±»å |
| | | */ |
| | | type, |
| | | |
| | | /** |
| | | * åç§° |
| | | */ |
| | | name; |
| | | |
| | | /** |
| | | * ææ¬æåºè½¬æ¢ææä¸¾ |
| | | * |
| | | * @param sort |
| | | * @return |
| | | */ |
| | | public static FileSort of(String sort) { |
| | | try { |
| | | return FileSort.valueOf(sort); |
| | | } catch (Exception e) { |
| | | return FileSort.name; |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static class NameComparator implements Comparator { |
| | | @Override |
| | | public int compare(Object a, Object b) { |
| | | Hashtable hashA = (Hashtable) a; |
| | | Hashtable hashB = (Hashtable) b; |
| | | if (((Boolean) hashA.get(IS_DIR)) && !((Boolean) hashB.get(IS_DIR))) { |
| | | return -1; |
| | | } else if (!((Boolean) hashA.get(IS_DIR)) && ((Boolean) hashB.get(IS_DIR))) { |
| | | return 1; |
| | | } else { |
| | | return ((String) hashA.get(FILE_NAME)).compareTo((String) hashB.get(FILE_NAME)); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static class SizeComparator implements Comparator { |
| | | @Override |
| | | public int compare(Object a, Object b) { |
| | | Hashtable hashA = (Hashtable) a; |
| | | Hashtable hashB = (Hashtable) b; |
| | | if (((Boolean) hashA.get(IS_DIR)) && !((Boolean) hashB.get(IS_DIR))) { |
| | | return -1; |
| | | } else if (!((Boolean) hashA.get(IS_DIR)) && ((Boolean) hashB.get(IS_DIR))) { |
| | | return 1; |
| | | } else { |
| | | if (((Long) hashA.get(FILE_SIZE)) > ((Long) hashB.get(FILE_SIZE))) { |
| | | return 1; |
| | | } else if (((Long) hashA.get(FILE_SIZE)) < ((Long) hashB.get(FILE_SIZE))) { |
| | | return -1; |
| | | } else { |
| | | return 0; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static class TypeComparator implements Comparator { |
| | | @Override |
| | | public int compare(Object a, Object b) { |
| | | Hashtable hashA = (Hashtable) a; |
| | | Hashtable hashB = (Hashtable) b; |
| | | if (((Boolean) hashA.get(IS_DIR)) && !((Boolean) hashB.get(IS_DIR))) { |
| | | return -1; |
| | | } else if (!((Boolean) hashA.get(IS_DIR)) && ((Boolean) hashB.get(IS_DIR))) { |
| | | return 1; |
| | | } else { |
| | | return ((String) hashA.get("filetype")).compareTo((String) hashB.get("filetype")); |
| | | } |
| | | } |
| | | } |
| | | |
| | | public static String formatUrl(String url) { |
| | | return url.replaceAll("\\\\", "/"); |
| | | } |
| | | |
| | | |
| | | /********************************BladeFileå°è£
********************************************************/ |
| | | |
| | | /** |
| | | * è·åBladeFileå°è£
ç±» |
| | | * |
| | | * @param file æä»¶ |
| | | * @return BladeFile |
| | | */ |
| | | public static LocalFile getFile(MultipartFile file) { |
| | | return getFile(file, "image", null, null); |
| | | } |
| | | |
| | | /** |
| | | * è·åBladeFileå°è£
ç±» |
| | | * |
| | | * @param file æä»¶ |
| | | * @param dir ç®å½ |
| | | * @return BladeFile |
| | | */ |
| | | public static LocalFile getFile(MultipartFile file, String dir) { |
| | | return getFile(file, dir, null, null); |
| | | } |
| | | |
| | | /** |
| | | * è·åBladeFileå°è£
ç±» |
| | | * |
| | | * @param file æä»¶ |
| | | * @param dir ç®å½ |
| | | * @param path è·¯å¾ |
| | | * @param virtualPath èæè·¯å¾ |
| | | * @return BladeFile |
| | | */ |
| | | public static LocalFile getFile(MultipartFile file, String dir, String path, String virtualPath) { |
| | | return new LocalFile(file, dir, path, virtualPath); |
| | | } |
| | | |
| | | /** |
| | | * è·åBladeFileå°è£
ç±» |
| | | * |
| | | * @param files æä»¶éå |
| | | * @return BladeFile |
| | | */ |
| | | public static List<LocalFile> getFiles(List<MultipartFile> files) { |
| | | return getFiles(files, "image", null, null); |
| | | } |
| | | |
| | | /** |
| | | * è·åBladeFileå°è£
ç±» |
| | | * |
| | | * @param files æä»¶éå |
| | | * @param dir ç®å½ |
| | | * @return BladeFile |
| | | */ |
| | | public static List<LocalFile> getFiles(List<MultipartFile> files, String dir) { |
| | | return getFiles(files, dir, null, null); |
| | | } |
| | | |
| | | /** |
| | | * è·åBladeFileå°è£
ç±» |
| | | * |
| | | * @param files æä»¶éå |
| | | * @param path è·¯å¾ |
| | | * @param virtualPath èæè·¯å¾ |
| | | * @return BladeFile |
| | | */ |
| | | public static List<LocalFile> getFiles(List<MultipartFile> files, String dir, String path, String virtualPath) { |
| | | List<LocalFile> list = new ArrayList<>(); |
| | | for (MultipartFile file : files) { |
| | | list.add(new LocalFile(file, dir, path, virtualPath)); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | } |