| | |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.io.*; |
| | | import java.nio.ByteBuffer; |
| | | import java.nio.channels.FileChannel; |
| | | import java.nio.charset.StandardCharsets; |
| | |
| | | |
| | | /** |
| | | * 获取文件名 |
| | | * |
| | | * @param file |
| | | * @return |
| | | */ |
| | | public static String getFileName(String file) { |
| | | int idx = file.lastIndexOf(File.separator); |
| | |
| | | } |
| | | |
| | | return ""; |
| | | } |
| | | |
| | | /** |
| | | * 获取文件名称 |
| | | */ |
| | | public static String getName(String file) { |
| | | String fileName = getFileName(file); |
| | | int idx = fileName.lastIndexOf("."); |
| | | if (idx > -1) { |
| | | return fileName.substring(0, idx); |
| | | } |
| | | |
| | | return fileName; |
| | | } |
| | | |
| | | /** |
| | |
| | | |
| | | String fileName = file.getName().toLowerCase(); |
| | | |
| | | int idx = fileName.lastIndexOf(StaticData.POINT); |
| | | int idx = fileName.indexOf(StaticData.POINT); |
| | | if (idx == -1) { |
| | | return ""; |
| | | } |
| | |
| | | |
| | | /** |
| | | * 获取多用途互联网邮件扩展类型 |
| | | * |
| | | * @param ext 文件扩展名 |
| | | * @return |
| | | */ |
| | | public static String getMime(String ext) { |
| | | switch (ext) { |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取文件大小 |
| | | */ |
| | | public static String getSizes(double mbNumber) { |
| | | if (mbNumber < StaticData.D1024) { |
| | | return new DecimalFormat("#.##MB").format(mbNumber); |
| | | } |
| | | |
| | | double gbNumber = mbNumber / StaticData.D1024; |
| | | if (gbNumber < StaticData.D1024) { |
| | | return new DecimalFormat("#.##GB").format(gbNumber); |
| | | } |
| | | |
| | | double tbNumber = gbNumber / StaticData.D1024; |
| | | |
| | | return new DecimalFormat("#.##TB").format(tbNumber); |
| | | } |
| | | |
| | | /** |
| | | * byte转MB |
| | | */ |
| | | public static double sizeToMb(long size) { |
| | |
| | | * @param file 文件 |
| | | */ |
| | | public static void deleteFiles(File file) { |
| | | if (file == null || !file.exists()) { |
| | | if (null == file || !file.exists()) { |
| | | return; |
| | | } |
| | | |
| | | if (file.isDirectory()) { |
| | | File[] files = file.listFiles(); |
| | | for (File f : files) { |
| | | if (f.isDirectory()) { |
| | | deleteFiles(f); |
| | | } else { |
| | | f.delete(); |
| | | if (null != files && files.length > 0) { |
| | | for (File f : files) { |
| | | if (f.isDirectory()) { |
| | | deleteFiles(f); |
| | | } else { |
| | | f.delete(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | |
| | | return builder.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 根据路径获取文件 |
| | | */ |
| | | public static void getFilesByPath(List<String> list, String path) { |
| | | File file = new File(path); |
| | | if (file.isDirectory()) { |
| | | File[] files = file.listFiles(); |
| | | if (null == files) { |
| | | return; |
| | | } |
| | | |
| | | for (File f : files) { |
| | | if (f.isDirectory()) { |
| | | getFilesByPath(list, f.getPath()); |
| | | } else { |
| | | list.add(f.getPath()); |
| | | } |
| | | } |
| | | } else { |
| | | list.add(file.getPath()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 复制文件 |
| | | * |
| | | * @param src 源文件 |
| | | * @param dest 目录文件 |
| | | */ |
| | | public static void copyFile(File src, File dest) throws IOException { |
| | | InputStream is = null; |
| | | OutputStream os = null; |
| | | try { |
| | | is = new FileInputStream(src); |
| | | os = new FileOutputStream(dest); |
| | | |
| | | byte[] buffer = new byte[1024]; |
| | | |
| | | int length; |
| | | while ((length = is.read(buffer)) > 0) { |
| | | os.write(buffer, 0, length); |
| | | } |
| | | } finally { |
| | | os.close(); |
| | | is.close(); |
| | | } |
| | | } |
| | | } |