From 3a154bdb0a5aaa2c0ac3eac95a6ba747068bd454 Mon Sep 17 00:00:00 2001
From: MrShi <1878285526@qq.com>
Date: 星期二, 13 一月 2026 10:00:37 +0800
Subject: [PATCH] 优化
---
keyCabinet-android/app/src/main/java/com/doumee/keyCabinet/utils/update/FileUtil.java | 298 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 298 insertions(+), 0 deletions(-)
diff --git a/keyCabinet-android/app/src/main/java/com/doumee/keyCabinet/utils/update/FileUtil.java b/keyCabinet-android/app/src/main/java/com/doumee/keyCabinet/utils/update/FileUtil.java
new file mode 100644
index 0000000..42bd3b6
--- /dev/null
+++ b/keyCabinet-android/app/src/main/java/com/doumee/keyCabinet/utils/update/FileUtil.java
@@ -0,0 +1,298 @@
+package com.doumee.keyCabinet.utils.update;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.os.Build;
+import android.os.Environment;
+import android.util.Log;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.sql.Timestamp;
+import java.text.DecimalFormat;
+
+/**
+ * 鏂囦欢宸ュ叿绫�
+ *
+ * @author spring sky
+ *
+ */
+public class FileUtil {
+ /**
+ * 鑾峰彇鐩綍鍚嶇О
+ *
+ * @param url
+ * @return FileName
+ */
+ public static String getFileName(String url) {
+ int lastIndexStart = url.lastIndexOf("/");
+ if (lastIndexStart != -1) {
+ return url.substring(lastIndexStart + 1, url.length());
+ } else {
+ return new Timestamp(System.currentTimeMillis()).toString();
+ }
+ }
+
+ /**
+ * 鏂囦欢鏄惁瀛樺湪
+ *
+ * @param path
+ * @return
+ */
+ public static boolean FileIsExist(String path) {
+ File file = new File(path);
+
+ return file.exists();
+ }
+
+ /**
+ * 鍒ゆ柇SD鍗℃槸鍚﹀瓨鍦�
+ *
+ * @return boolean
+ */
+ public static boolean checkSDCard() {
+ if (Environment.getExternalStorageState().equals(
+ Environment.MEDIA_MOUNTED)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * 淇濆瓨鏂囦欢鍒版寚瀹氱洰褰�
+ *
+ * @param context
+ * @return 鏂囦欢淇濆瓨鐨勭洰褰�
+ */
+ public static String setMkdir(Context context) {
+ String filePath = null;
+ if (checkSDCard()) {
+ filePath = Environment.getExternalStorageDirectory()
+ + File.separator + "com.ahrykj.Weddingg" + File.separator
+ + "downloads";
+ } else {
+ filePath = context.getCacheDir().getAbsolutePath() + File.separator
+ + "com.ahrykj.Weddingg" + File.separator + "downloads";
+ }
+ File file = new File(filePath);
+ if (!file.exists()) {
+ file.mkdirs();
+ Log.e("file", "鐩綍涓嶅瓨鍦� 鍒涘缓鐩綍 ");
+ } else {
+ Log.e("file", "鐩綍瀛樺湪");
+ }
+ return filePath;
+ }
+
+ public static String getFile(Context context){
+ File sdDir = null;
+ boolean sdCardExist = Environment.getExternalStorageState().equals(
+ Environment.MEDIA_MOUNTED);// 鍒ゆ柇sd鍗℃槸鍚﹀瓨鍦�
+ if (sdCardExist) {
+ if (Build.VERSION.SDK_INT>=29){
+ //Android10涔嬪悗
+ sdDir = context.getExternalFilesDir(null);
+ }else {
+ sdDir = Environment.getExternalStorageDirectory();// 鑾峰彇SD鍗℃牴鐩綍
+ }
+ } else {
+ sdDir = Environment.getRootDirectory();// 鑾峰彇璺熺洰褰�
+ }
+ return sdDir.toString();
+ }
+
+ /**
+ * 鑾峰彇璺緞
+ *
+ * @return
+ * @throws IOException
+ */
+ public static String getPath(Context context, String url) {
+ String path = null;
+ try {
+ path = FileUtil.getFile(context) + File.separator
+ + "ticketmachine.apk";
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return path;
+ }
+
+ public static long getFileSize(File f) throws Exception// 鍙栧緱鏂囦欢澶瑰ぇ灏�
+ {
+ long size = 0;
+ File flist[] = f.listFiles();
+ if (flist != null) {
+ for (int i = 0; i < flist.length; i++) {
+ if (flist[i].isDirectory()) {
+ size = size + getFileSize(flist[i]);
+ } else {
+ size = size + flist[i].length();
+ }
+ }
+ }
+ return size;
+ }
+
+ /**
+ * 鑾峰彇鏂囦欢鐨勫ぇ灏�
+ *
+ * @param fileS
+ * 鏂囦欢鐨勫ぇ灏�
+ * @return
+ */
+ public static String FormetFileSize(long fileS) {// 杞崲鏂囦欢澶у皬
+ DecimalFormat df = new DecimalFormat("0.0");// #.00
+ String fileSizeString = "";
+ if (fileS < 1024) {
+ if (fileS < 70) {
+ fileSizeString = "0B";
+ } else {
+ fileSizeString = df.format((double) fileS) + "B";
+ }
+ } else if (fileS < 1048576) {
+ fileSizeString = df.format((double) fileS / 1024) + "K";
+ } else if (fileS < 1073741824) {
+ fileSizeString = df.format((double) fileS / 1048576) + "M";
+ } else {
+ fileSizeString = df.format((double) fileS / 1073741824) + "G";
+ }
+ return fileSizeString;
+ }
+
+ /**
+ * 鍒犻櫎璇ョ洰褰曚笅鐨勬寚瀹氭枃浠�
+ *
+ * @param filePath
+ * 鏂囦欢鐩綍
+ * @param fileName
+ * 鏂囦欢鍚�
+ * @return
+ */
+ public static boolean deleteFile(String filePath, String fileName) {
+ if (FileIsExist(filePath + fileName)) {
+ File file = new File(filePath + fileName);
+
+ return file.delete();
+ }
+
+ return true;
+ }
+
+ /**
+ * 鍒犻櫎璇ヨ矾寰勬寚瀹氱殑鏂囦欢
+ *
+ * @param path
+ * 鏂囦欢璺緞
+ * @return
+ */
+ public static boolean deleteFile(String path) {
+ if (FileIsExist(path)) {
+ File file = new File(path);
+ return file.delete();
+ }
+ return true;
+ }
+
+ /**
+ * 鍒犻櫎鏂规硶 杩欓噷鍙細鍒犻櫎鏌愪釜鏂囦欢澶逛笅鐨勬枃浠讹紝濡傛灉浼犲叆鐨刣irectory鏄釜鏂囦欢锛屽皢涓嶅仛澶勭悊
+ *
+ * @param file
+ */
+
+ public static void deleteFilesByDirectory(File file) {
+ if (file.isFile()) {
+ file.delete();
+ return;
+ }
+
+ if (file.isDirectory()) {
+ File[] childFiles = file.listFiles();
+ if (childFiles == null || childFiles.length == 0) {
+ file.delete();
+ return;
+ }
+
+ for (int i = 0; i < childFiles.length; i++) {
+ deleteFilesByDirectory(childFiles[i]);
+ }
+ file.delete();
+ }
+ }
+
+ //灏咮itmap杞崲鎴怓ile
+ public static File saveBitmapFile(Bitmap bitmap){
+ File file=new File("/sdcard/face.jpg");//灏嗚淇濆瓨鍥剧墖鐨勮矾寰�
+ try {
+ BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
+ bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
+ bos.flush();
+ bos.close();
+ } catch (IOException e) {
+ return null;
+ }
+ return file;
+ }
+
+ public static interface DownLoadCallBack{
+ void sucess();
+ void err(String e);
+ }
+
+ public static void downLoadFile(String filePath,String url,DownLoadCallBack callBack){
+ try {
+ URL u = new URL(url);
+ HttpURLConnection conn = (HttpURLConnection) u.openConnection();
+ conn.setConnectTimeout(10000);
+ int responeCode = conn.getResponseCode();
+ InputStream is;
+ if (responeCode == 200) {
+ is = conn.getInputStream();
+ } else {
+ //杩炴帴澶辫触
+ if(callBack!=null){
+ callBack.err("涓嬭浇鍦板潃閿欒");
+ }
+ return;
+ }
+
+ int fileSize = conn.getContentLength();
+ if (fileSize < 1 || is == null) {
+ //鏂囦欢澶у皬涓嶅
+ if(callBack!=null){
+ callBack.err("鏂囦欢澶у皬涓嶅");
+ }
+ return;
+ } else {
+ //涓嬭浇
+ if (FileUtil.deleteFile(filePath)) {// 鍒犻櫎鍘熸潵鐨勫畨瑁呮枃浠跺啀涓嬭浇
+ FileOutputStream fos = new FileOutputStream(filePath);
+ byte[] bytes = new byte[1024];
+ int len = -1;
+ while ((len = is.read(bytes)) != -1) {
+ fos.write(bytes, 0, len);
+ fos.flush();
+ }
+ //涓嬭浇瀹屾垚
+ if(callBack!=null){
+ callBack.sucess();
+ }
+ is.close();
+ fos.close();
+ }
+ }
+ } catch (Exception e) {
+ //涓嬭浇鎶ラ敊
+ if(callBack!=null){
+ callBack.err(e.getMessage());
+ }
+ e.printStackTrace();
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.3