| | |
| | | package com.lf.server.helper; |
| | | |
| | | import java.io.File; |
| | | import java.util.Date; |
| | | import java.text.DecimalFormat; |
| | | |
| | | /** |
| | | * 文件帮助类 |
| | |
| | | */ |
| | | public class FileHelper { |
| | | private final static String POINT = "."; |
| | | |
| | | public static final double D1024 = 1024.0; |
| | | |
| | | public static final double D1050 = 1050.0; |
| | | |
| | | /** |
| | | * 获取文件扩展名 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取临时路径 |
| | | * |
| | | * @return |
| | | * 字节单位换算 |
| | | */ |
| | | public static String getTempPath() { |
| | | return StringHelper.YMD_HM_FORMAT.format(new Date()); |
| | | public static String formatByte(long byteNumber) { |
| | | double kbNumber = byteNumber / D1024; |
| | | if (kbNumber < D1024) { |
| | | return new DecimalFormat("#.##KB").format(kbNumber); |
| | | } |
| | | double mbNumber = kbNumber / D1024; |
| | | if (mbNumber < D1024) { |
| | | return new DecimalFormat("#.##MB").format(mbNumber); |
| | | } |
| | | double gbNumber = mbNumber / D1024; |
| | | if (gbNumber < D1024) { |
| | | return new DecimalFormat("#.##GB").format(gbNumber); |
| | | } |
| | | double tbNumber = gbNumber / D1024; |
| | | |
| | | return new DecimalFormat("#.##TB").format(tbNumber); |
| | | } |
| | | |
| | | /** |
| | | * byte转MB |
| | | */ |
| | | public static double sizeToMb(long size) { |
| | | if (size < D1050) { |
| | | return 0.001; |
| | | } |
| | | |
| | | String str = String.format("%.3f", size / D1024 / D1024); |
| | | |
| | | return Double.parseDouble(str); |
| | | } |
| | | } |