【蜕变之路】第39天 文件工具类(2019年5月4日)

    Hello,大家好!我是程序员阿飞!今天我给大家分享一个比较好的文件工具类,虽然不是很全面,但是用起来很好。好了,开始进入我们今天的主题:文件工具类。

    代码实现:

      

/**
* File工具类,扩展 hutool 工具包
* @author jie
* @date 2018-12-27
*/
public class FileUtil extends cn.hutool.core.io.FileUtil {

   /**
    * 定义GB的计算常量
    */
   private static final int GB = 1024 * 1024 * 1024;
   /**
    * 定义MB的计算常量
    */
   private static final int MB = 1024 * 1024;
   /**
    * 定义KB的计算常量
    */
   private static final int KB = 1024;

   /**
    * 格式化小数
    */
   private static final DecimalFormat DF = new DecimalFormat("0.00");

   /**
    * MultipartFile转File
    * @param multipartFile
    * @return
    */
   public static File toFile(MultipartFile multipartFile){
       // 获取文件名
       String fileName = multipartFile.getOriginalFilename();
       // 获取文件后缀
       String prefix="."+getExtensionName(fileName);
       File file = null;
       try {
           // 用uuid作为文件名,防止生成的临时文件重复
           file = File.createTempFile(IdUtil.simpleUUID(), prefix);
           // MultipartFile to File
           multipartFile.transferTo(file);
       } catch (IOException e) {
           e.printStackTrace();
       }
       return file;
   }

   /**
    * 删除
    * @param files
    */
   public static void deleteFile(File... files) {
       for (File file : files) {
           if (file.exists()) {
               file.delete();
           }
       }
   }

   /**
    * 获取文件扩展名
    * @param filename
    * @return
    */
   public static String getExtensionName(String filename) {
       if ((filename != null) && (filename.length() > 0)) {
           int dot = filename.lastIndexOf('.');
           if ((dot >-1) && (dot < (filename.length() - 1))) {
               return filename.substring(dot + 1);
           }
       }
       return filename;
   }

   /**
    * Java文件操作 获取不带扩展名的文件名
    * @param filename
    * @return
    */
   public static String getFileNameNoEx(String filename) {
       if ((filename != null) && (filename.length() > 0)) {
           int dot = filename.lastIndexOf('.');
           if ((dot >-1) && (dot < (filename.length()))) {
               return filename.substring(0, dot);
           }
       }
       return filename;
   }

   /**
    * 文件大小转换
    * @param size
    * @return
    */
   public static String getSize(int size){
       String resultSize = "";
       if (size / GB >= 1) {
           //如果当前Byte的值大于等于1GB
           resultSize = DF.format(size / (float) GB) + "GB   ";
       } else if (size / MB >= 1) {
           //如果当前Byte的值大于等于1MB
           resultSize = DF.format(size / (float) MB) + "MB   ";
       } else if (size / KB >= 1) {
           //如果当前Byte的值大于等于1KB
           resultSize = DF.format(size / (float) KB) + "KB   ";
       } else {
           resultSize = size + "B   ";
       }
       return resultSize;
   }

   public static void main(String[] args){
       String result = getExtensionName("test.txt");
       System.out.println("=========="+result);
   }
}


    【开心时刻】结婚相册

            P012_调整大小.jpg

P015_调整大小.jpg

P018_调整大小.jpg

猜你喜欢

转载自blog.51cto.com/12388374/2388738