Browse Source

file-utils

lyhzzz 3 years ago
parent
commit
b4d41fed35

+ 0 - 133
4dkankan-utils-file/src/main/java/com/fdkankan/file/utils/FileMd5Util.java

@@ -1,133 +0,0 @@
-package com.fdkankan.file.utils;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.security.MessageDigest;
-
-public class FileMd5Util {
-
-    public static final String KEY_MD5 = "MD5";
-    public static final String CHARSET_ISO88591 = "ISO-8859-1";
-
-    /**
-     * Get MD5 of one file:hex string,test OK!
-     *
-     * @param file
-     * @return
-     */
-    public static String getFileMD5(File file) {
-        if (!file.exists() || !file.isFile()) {
-            return null;
-        }
-        MessageDigest digest = null;
-        FileInputStream in = null;
-        byte buffer[] = new byte[1024];
-        int len;
-        try {
-            digest = MessageDigest.getInstance("MD5");
-            in = new FileInputStream(file);
-            while ((len = in.read(buffer, 0, 1024)) != -1) {
-                digest.update(buffer, 0, len);
-            }
-            in.close();
-        } catch (Exception e) {
-            e.printStackTrace();
-            return null;
-        }
-        byte by[] = digest.digest();
-        int i;
-        StringBuffer sbf = new StringBuffer();
-        for (int j = 0; j < by.length; j++) {
-            i = by[j];
-            if (i < 0) {
-                i += 256;
-            } else if (i < 16) {
-                sbf.append("0");    //因为大于16的有两位,因此小于16需要补位,
-            }
-            sbf.append(Integer.toHexString(i));
-
-        }
-
-        return sbf.toString();
-//        BigInteger bigInt = new BigInteger(1, digest.digest());
-//        return bigInt.toString(16);
-    }
-
-    /***
-     * Get MD5 of one file!test ok!
-     *
-     * @param filepath
-     * @return
-     */
-    public static String getFileMD5(String filepath) {
-        File file = new File(filepath);
-        return getFileMD5(file);
-    }
-
-    /**
-     * MD5 encrypt,test ok
-     *
-     * @param data
-     * @return byte[]
-     * @throws Exception
-     */
-    public static byte[] encryptMD5(byte[] data) throws Exception {
-
-        MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
-        md5.update(data);
-        return md5.digest();
-    }
-
-    public static byte[] encryptMD5(String data) throws Exception {
-        return encryptMD5(data.getBytes(CHARSET_ISO88591));
-    }
-
-    /***
-     * compare two file by Md5
-     *
-     * @param file1
-     * @param file2
-     * @return
-     */
-    public static boolean isSameMd5(File file1, File file2) {
-        String md5_1 = FileMd5Util.getFileMD5(file1);
-        String md5_2 = FileMd5Util.getFileMD5(file2);
-        return md5_1.equals(md5_2);
-    }
-
-    /***
-     * compare two file by Md5
-     *
-     * @param filepath1
-     * @param filepath2
-     * @return
-     */
-    public static boolean isSameMd5(String filepath1, String filepath2) {
-        File file1 = new File(filepath1);
-        File file2 = new File(filepath2);
-        return isSameMd5(file1, file2);
-    }
-
-    public static void main(String[] args){
-//        String path = "F:\\桌面\\";
-//
-//        StringBuffer sb = new StringBuffer(path + "20190925151119.h264");
-//        File dbFile = new File(sb.toString());
-//
-//        String fileMD5 = FileMd5Util.getFileMD5(dbFile);
-//        System.out.println(fileMD5);
-
-//        String path1 = "F:\\文档\\WeChat Files\\Iove-bing\\FileStorage\\File\\2020-05\\1.3.4-update.zip";
-//        File dbFile1 = new File(path1);
-//
-//        String fileMD51 = FileMd5Util.getFileMD5(dbFile1);
-//        System.out.println(fileMD51);
-
-//        BigInteger usedSpace = new BigInteger("0");
-//        BigInteger space = new BigInteger("560800515");
-//        usedSpace.add(space);
-//        System.out.println(usedSpace.intValue());
-        System.out.println("obj1: " + getFileMD5(new File("F:\\桌面\\c11m-T11-EA\\log\\i6VhiQ2Q-copy.obj")));
-        System.out.println("obj: " + getFileMD5(new File("F:\\桌面\\c11m-T11-EA\\log\\i6VhiQ2Q.obj")));
-    }
-}

+ 0 - 141
4dkankan-utils-file/src/main/java/com/fdkankan/file/utils/FileSizeUtil.java

@@ -1,141 +0,0 @@
-package com.fdkankan.file.utils;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.text.DecimalFormat;
-
-public class FileSizeUtil {
-
-    private static final String TAG=FileSizeUtil.class.getSimpleName();
-
-    public static final int SIZETYPE_B = 1;//获取文件大小单位为B的double值
-    public static final int SIZETYPE_KB = 2;//获取文件大小单位为KB的double值
-    public static final int SIZETYPE_MB = 3;//获取文件大小单位为MB的double值
-    public static final int SIZETYPE_GB = 4;//获取文件大小单位为GB的double值
-
-    /**
-     * 获取文件指定文件的指定单位的大小
-     *
-     * @param filePath 文件路径
-     * @param sizeType 获取大小的类型1为B、2为KB、3为MB、4为GB
-     * @return double值的大小
-     */
-    public static double getFileOrFilesSize(String filePath, int sizeType) {
-        File file = new File(filePath);
-        long blockSize = 0;
-        try {
-            if (file.isDirectory()) {
-                blockSize = getFileSizes(file);
-            } else {
-                blockSize = getFileSize(file);
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return formetFileSize(blockSize, sizeType);
-    }
-
-    /**
-     * 调用此方法自动计算指定文件或指定文件夹的大小
-     *
-     * @param filePath 文件路径
-     * @return 计算好的带B、KB、MB、GB的字符串
-     */
-    public static String getAutoFileOrFilesSize(String filePath) {
-        File file = new File(filePath);
-        long blockSize = 0;
-        try {
-            if (file.isDirectory()) {
-                blockSize = getFileSizes(file);
-            } else {
-                blockSize = getFileSize(file);
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return formatFileSize(blockSize);
-    }
-
-    /**
-     * 获取指定文件大小
-     */
-    private static long getFileSize(File file) throws Exception {
-        long size = 0;
-        if (file.exists()) {
-            FileInputStream fis = null;
-            fis = new FileInputStream(file);
-            size = fis.available();
-        } else {
-            file.createNewFile();
-        }
-        return size;
-    }
-
-    /**
-     * 获取指定文件夹
-     */
-    private static long getFileSizes(File f) throws Exception {
-        long size = 0;
-        File flist[] = f.listFiles();
-        assert flist != null;
-        for (File file : flist) {
-            if (file.isDirectory()) {
-                size = size + getFileSizes(file);
-            } else {
-                size = size + getFileSize(file);
-            }
-        }
-        return size;
-    }
-
-    /**
-     * 转换文件大小
-     */
-    public static String formatFileSize(long fileS) {
-        DecimalFormat df = new DecimalFormat("#.00");
-        String fileSizeString = "";
-        String wrongSize = "0B";
-        if (fileS == 0) {
-            return wrongSize;
-        }
-        if (fileS < 1024) {
-            fileSizeString = df.format((double) fileS) + "B";
-        } else if (fileS < 1048576) {
-            fileSizeString = df.format((double) fileS / 1024) + "KB";
-        } else if (fileS < 1073741824) {
-            fileSizeString = df.format((double) fileS / 1048576) + "MB";
-        } else if (fileS < 1099511627776L){
-            fileSizeString = df.format((double) fileS / 1073741824) + "GB";
-        } else if (fileS < 1125899906842624L){
-            fileSizeString = df.format((double) fileS / 1099511627776L) + "TB";
-        } else {
-            fileSizeString = df.format((double) fileS / 1125899906842624L) + "PB";
-        }
-        return fileSizeString;
-    }
-
-    /**
-     * 转换文件大小,指定转换的类型
-     */
-    public static double formetFileSize(long fileS, int sizeType) {
-        DecimalFormat df = new DecimalFormat("#.00");
-        double fileSizeLong = 0;
-        switch (sizeType) {
-            case SIZETYPE_B:
-                fileSizeLong = Double.valueOf(df.format((double) fileS));
-                break;
-            case SIZETYPE_KB:
-                fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
-                break;
-            case SIZETYPE_MB:
-                fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
-                break;
-            case SIZETYPE_GB:
-                fileSizeLong = Double.valueOf(df.format((double) fileS / 1073741824));
-                break;
-            default:
-                break;
-        }
-        return fileSizeLong;
-    }
-}

+ 0 - 134
4dkankan-utils-file/src/main/java/com/fdkankan/file/utils/FileUpload.java

@@ -1,134 +0,0 @@
-package com.fdkankan.file.utils;
-
-import lombok.extern.slf4j.Slf4j;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.io.*;
-
-/**
- * @author MeepoGuan
- *
- * <p>Description: 上传问题件</p>
- *
- * 2017年4月30日
- *
- */
-@Slf4j
-public class FileUpload {
-
-	/**
-	 * @param file 			//文件对象
-	 * @param filePath		//上传路径
-	 * @param fileName		//文件名
-	 * @return  文件名
-	 */
-	public static String fileUp(MultipartFile file, String filePath, String fileName) throws IOException {
-		String extName = ""; // 扩展名格式:
-
-		if (file.getOriginalFilename().lastIndexOf(".") >= 0){
-			extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
-		}
-		copyFile(file.getInputStream(), filePath, fileName+extName).replaceAll("-", "");
-
-		return fileName+extName;
-	}
-	
-	/**
-	 * 写文件到当前目录的upload目录中
-	 * 
-	 * @param in
-	 * @param dir
-	 * @param realName
-	 * @throws IOException
-	 */
-	private static String copyFile(InputStream in, String dir, String realName)
-			throws IOException {
-		File file = new File(dir, realName);
-		if (!file.exists()) {
-			if (!file.getParentFile().exists()) {
-				file.getParentFile().mkdirs();
-			}
-			file.createNewFile();
-		}
-        org.apache.commons.io.FileUtils.copyInputStreamToFile(in, file);
-		return realName;
-	}
-
-	/**
-	 * 断点续传
-	 * @param
-     */
-	public static String fileUpAgain(InputStream in, String filePath, String fileName, String realSavePath) throws IOException {
-		try{
-			File realFile = new File(realSavePath +  fileName);
-			File tempFile = new File(filePath + fileName);
-			if(!realFile.getParentFile().exists()){
-				realFile.getParentFile().mkdirs();
-			}
-			if(!tempFile.getParentFile().exists()){
-				tempFile.getParentFile().mkdirs();
-			}
-
-//			InputStream in = file.getInputStream();
-			long needSkipBytes = 0;
-			if (tempFile.exists()) {//续传
-				needSkipBytes = tempFile.length();
-			} else {//第一次传
-				tempFile.createNewFile();
-			}
-			log.info("跳过的字节数为:" + needSkipBytes);
-			in.skip(needSkipBytes);
-			RandomAccessFile tempRandAccessFile = new RandomAccessFile(tempFile, "rw");
-			tempRandAccessFile.seek(needSkipBytes);
-			byte[] buffer = new byte[1024];
-			int len = 0;
-			int count = 0;
-			while ((len = in.read(buffer)) > 0) {
-				tempRandAccessFile.write(buffer);
-				count++;
-			}
-			in.close();
-			tempRandAccessFile.close();
-			realFile.createNewFile();
-			if (fileCopy(tempFile, realFile)) {
-				tempFile.delete();
-			}
-		}catch (Exception e){
-			e.printStackTrace();
-		}
-		return realSavePath + fileName;
-	}
-
-	private static boolean fileCopy(File sourceFile, File targetFile) {
-		boolean success = true;
-		try {
-			FileInputStream in = new FileInputStream(sourceFile);
-			FileOutputStream out = new FileOutputStream(targetFile);
-			byte[] buffer = new byte[1024];
-			int len = 0;
-			while ((len = in.read(buffer)) > 0) {
-				out.write(buffer);
-			}
-			in.close();
-			out.close();
-		} catch (FileNotFoundException e) {
-			success = false;
-		} catch (IOException e) {
-			success = false;
-		}
-		return success;
-	}
-
-
-	public static void main(String[] args) {
-		try{
-			String path = "F:\\桌面\\20190925151119-T.h264";
-			FileInputStream f = new FileInputStream(path);
-			fileUpAgain(f, "G:\\javaProject\\9b918c802c3e40282267a89b5231f9a8_201905101446434643\\videos\\", "test123-T.h264", "G:\\javaProject\\9b918c802c3e40282267a89b5231f9a8_201905101446434643\\capture\\");
-//			copyFile(f, "G:\\javaProject\\zhoushan-system\\zhoushan-system-api\\src\\main\\resources\\static\\head", "test.h264");
-		}catch (Exception e){
-			e.printStackTrace();
-		}
-
-	}
-}

+ 0 - 240
4dkankan-utils-file/src/main/java/com/fdkankan/file/utils/FileUtil.java

@@ -1,240 +0,0 @@
-package com.fdkankan.file.utils;
-
-import java.io.*;
-import java.nio.ByteBuffer;
-import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
-import java.nio.channels.FileChannel.MapMode;
-
-/**
- * @author MeepoGuan
- *
- * <p>Description: file_util</p>
- *
- * 2017年4月30日
- *
- */
-public class FileUtil {
-
-/*	public static void main(String[] args) {
-		String dirName = "d:/FH/topic/";// 创建目录
-		FileUtil.createDir(dirName);
-	}*/
-
-	/**
-	 * 创建目录
-	 * 
-	 * @param destDirName
-	 *            目标目录名
-	 * @return 目录创建成功返回true,否则返回false
-	 */
-	public static boolean createDir(String destDirName) {
-		File dir = new File(destDirName);
-		if (dir.exists()) {
-			return false;
-		}
-		if (!destDirName.endsWith(File.separator)) {
-			destDirName = destDirName + File.separator;
-		}
-		// 创建单个目录
-		if (dir.mkdirs()) {
-			return true;
-		} else {
-			return false;
-		}
-	}
-
-	/**
-	 * 删除文件
-	 * 
-	 * @param filePathAndName
-	 *            String 文件路径及名称 如c:/fqf.txt
-	 * @return boolean
-	 */
-	public static void delFile(String filePathAndName) {
-		try {
-			String filePath = filePathAndName;
-			filePath = filePath.toString();
-			File myDelFile = new File(filePath);
-			myDelFile.delete();
-
-		} catch (Exception e) {
-			System.out.println("删除文件操作出错");
-			e.printStackTrace();
-
-		}
-
-	}
-
-	/**
-	 * 读取到字节数组0
-	 * 
-	 * @param filePath //路径
-	 * @throws IOException
-	 */
-	public static byte[] getContent(String filePath) throws IOException {
-		File file = new File(filePath);
-		long fileSize = file.length();
-		if (fileSize > Integer.MAX_VALUE) {
-			System.out.println("file too big...");
-			return null;
-		}
-		FileInputStream fi = new FileInputStream(file);
-		byte[] buffer = new byte[(int) fileSize];
-		int offset = 0;
-		int numRead = 0;
-		while (offset < buffer.length
-				&& (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
-			offset += numRead;
-		}
-		// 确保所有数据均被读取
-		if (offset != buffer.length) {
-			throw new IOException("Could not completely read file "
-					+ file.getName());
-		}
-		fi.close();
-		return buffer;
-	}
-
-	/**
-	 * 读取到字节数组1
-	 * 
-	 * @param filePath
-	 * @return
-	 * @throws IOException
-	 */
-	public static byte[] toByteArray(String filePath) throws IOException {
-
-		File f = new File(filePath);
-		if (!f.exists()) {
-			throw new FileNotFoundException(filePath);
-		}
-		ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
-		BufferedInputStream in = null;
-		try {
-			in = new BufferedInputStream(new FileInputStream(f));
-			int buf_size = 1024;
-			byte[] buffer = new byte[buf_size];
-			int len = 0;
-			while (-1 != (len = in.read(buffer, 0, buf_size))) {
-				bos.write(buffer, 0, len);
-			}
-			return bos.toByteArray();
-		} catch (IOException e) {
-			e.printStackTrace();
-			throw e;
-		} finally {
-			try {
-				in.close();
-			} catch (IOException e) {
-				e.printStackTrace();
-			}
-			bos.close();
-		}
-	}
-
-	/**
-	 * 读取到字节数组2
-	 * 
-	 * @param filePath
-	 * @return
-	 * @throws IOException
-	 */
-	public static byte[] toByteArray2(String filePath) throws IOException {
-
-		File f = new File(filePath);
-		if (!f.exists()) {
-			throw new FileNotFoundException(filePath);
-		}
-
-		FileChannel channel = null;
-		FileInputStream fs = null;
-		try {
-			fs = new FileInputStream(f);
-			channel = fs.getChannel();
-			ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
-			while ((channel.read(byteBuffer)) > 0) {
-				// do nothing
-				// System.out.println("reading");
-			}
-			return byteBuffer.array();
-		} catch (IOException e) {
-			e.printStackTrace();
-			throw e;
-		} finally {
-			try {
-				channel.close();
-			} catch (IOException e) {
-				e.printStackTrace();
-			}
-			try {
-				fs.close();
-			} catch (IOException e) {
-				e.printStackTrace();
-			}
-		}
-	}
-
-	/**
-	 * Mapped File way MappedByteBuffer 可以在处理大文件时,提升性能
-	 * 
-	 * @param filePath
-	 * @return
-	 * @throws IOException
-	 */
-	public static byte[] toByteArray3(String filePath) throws IOException {
-
-		FileChannel fc = null;
-		RandomAccessFile rf = null;
-		try {
-			rf = new RandomAccessFile(filePath, "r");
-			fc = rf.getChannel();
-			MappedByteBuffer byteBuffer = fc.map(MapMode.READ_ONLY, 0,
-					fc.size()).load();
-			//System.out.println(byteBuffer.isLoaded());
-			byte[] result = new byte[(int) fc.size()];
-			if (byteBuffer.remaining() > 0) {
-				// System.out.println("remain");
-				byteBuffer.get(result, 0, byteBuffer.remaining());
-			}
-			return result;
-		} catch (IOException e) {
-			e.printStackTrace();
-			throw e;
-		} finally {
-			try {
-				rf.close();
-				fc.close();
-			} catch (IOException e) {
-				e.printStackTrace();
-			}
-		}
-	}
-
-    public static File[] sort(File[] s) {
-        //中间值
-        File temp = null;
-        //外循环:我认为最小的数,从0~长度-1
-        for (int j = 0; j < s.length - 1; j++) {
-            //最小值:假设第一个数就是最小的
-            String min = s[j].getName();
-            //记录最小数的下标的
-            int minIndex = j;
-            //内循环:拿我认为的最小的数和后面的数一个个进行比较
-            for (int k = j + 1; k < s.length; k++) {
-                //找到最小值
-                if (Integer.parseInt(min.substring(0, min.indexOf("."))) > Integer.parseInt(s[k].getName().substring(0, s[k].getName().indexOf(".")))) {
-                    //修改最小
-                    min = s[k].getName();
-                    minIndex = k;
-                }
-            }
-            //当退出内层循环就找到这次的最小值
-            //交换位置
-            temp = s[j];
-            s[j] = s[minIndex];
-            s[minIndex] = temp;
-        }
-        return s;
-    }
-}

File diff suppressed because it is too large
+ 0 - 1082
4dkankan-utils-file/src/main/java/com/fdkankan/file/utils/FileUtils.java


+ 0 - 29
4dkankan-utils-file/pom.xml

@@ -86,37 +86,8 @@
             <artifactId>spring-webmvc</artifactId>
             <version>5.3.13</version>
         </dependency>
-        <dependency>
-            <groupId>com.google.code.gson</groupId>
-            <artifactId>gson</artifactId>
-            <version>2.8.5</version>
-        </dependency>
 
-        <dependency>
-            <groupId>com.alibaba</groupId>
-            <artifactId>fastjson</artifactId>
-        </dependency>
 
-        <dependency>
-            <groupId>org.bytedeco</groupId>
-            <artifactId>javacpp</artifactId>
-            <version>1.4.3</version>
-        </dependency>
-        <dependency>
-            <groupId>org.bytedeco</groupId>
-            <artifactId>javacv-platform</artifactId>
-            <version>1.4.3</version>
-        </dependency>
-        <dependency>
-            <groupId>joinery</groupId>
-            <artifactId>jave</artifactId>
-            <version>1.0.2.2</version>
-        </dependency>
-        <dependency>
-            <groupId>org.apache.ant</groupId>
-            <artifactId>ant</artifactId>
-            <version>1.8.2</version>
-        </dependency>
     </dependencies>
 
 

+ 1 - 1
4dkankan-utils-file/src/main/java/com/fdkankan/file/oss/UploadToOssUtil.java

@@ -1,4 +1,4 @@
-package com.fdkankan.file.oss;
+package com.fdkankan.fyun.oss;
 
 import com.aliyun.oss.OSSClient;
 import com.aliyun.oss.model.OSSObjectSummary;

+ 1 - 1
4dkankan-utils-file/src/main/java/com/fdkankan/file/qiniu/QiniuUpload.java

@@ -1,4 +1,4 @@
-package com.fdkankan.file.qiniu;
+package com.fdkankan.fyun.qiniu;
 
 import com.google.gson.Gson;
 import com.qiniu.cdn.CdnManager;

+ 1 - 1
4dkankan-utils-file/src/main/java/com/fdkankan/file/qiniu/QiniuUtil.java

@@ -1,4 +1,4 @@
-package com.fdkankan.file.qiniu;
+package com.fdkankan.fyun.qiniu;
 
 import com.qiniu.common.Zone;
 import com.qiniu.util.Auth;