File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed
utilcode/src/main/java/com/blankj/utilcode/util Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 4
4
5
5
<uses-permission android : name =" android.permission.SYSTEM_ALERT_WINDOW" />
6
6
7
+ <uses-permission android : name =" android.permission.REQUEST_INSTALL_PACKAGES" />
8
+
7
9
<!-- bar-->
8
10
<uses-permission android : name =" android.permission.EXPAND_STATUS_BAR" />
9
11
Original file line number Diff line number Diff line change 9
9
import java .io .FileNotFoundException ;
10
10
import java .io .IOException ;
11
11
import java .io .InputStream ;
12
+ import java .net .HttpURLConnection ;
13
+ import java .net .URL ;
12
14
import java .security .DigestInputStream ;
13
15
import java .security .MessageDigest ;
14
16
import java .security .NoSuchAlgorithmException ;
@@ -850,7 +852,8 @@ public static String getDirSize(final File dir) {
850
852
* @return 文件大小
851
853
*/
852
854
public static String getFileSize (final String filePath ) {
853
- return getFileSize (getFileByPath (filePath ));
855
+ long len = getFileLength (filePath );
856
+ return len == -1 ? "" : byte2FitMemorySize (len );
854
857
}
855
858
856
859
/**
@@ -903,6 +906,20 @@ public static long getDirLength(final File dir) {
903
906
* @return 文件长度
904
907
*/
905
908
public static long getFileLength (final String filePath ) {
909
+ boolean isURL = filePath .matches ("[a-zA-z]+://[^\\ s]*" );
910
+ if (isURL ) {
911
+ try {
912
+ HttpURLConnection conn = (HttpURLConnection ) new URL (filePath ).openConnection ();
913
+ conn .setRequestProperty ("Accept-Encoding" , "identity" );
914
+ conn .connect ();
915
+ if (conn .getResponseCode () == 200 ) {
916
+ return conn .getContentLength ();
917
+ }
918
+ return -1 ;
919
+ } catch (IOException e ) {
920
+ e .printStackTrace ();
921
+ }
922
+ }
906
923
return getFileLength (getFileByPath (filePath ));
907
924
}
908
925
You can’t perform that action at this time.
0 commit comments