Android的java怎么获取文件大小?

Android的java怎么获取文件大小?,第1张

android中的java获取文件大小方法

import java.io.File

import java.io.FileInputStream

import java.io.FileReader

import java.io.IOException

public class FileContent {

private String path = "F:\\下载说明.txt"

public FileContent() throws IOException

{

File f = new File(path)

FileReader fileReader = new FileReader(f)

BufferedReader br = new BufferedReader(fileReader)

String str

while((str = br.readLine() ) != null)

{

System.out.println(str)

}

System.out.println(new FileInputStream(new File(path)).available() / 1024 / 1024 +"M")

}

public static void main(String[] args) {

try {

new FileContent()

} catch (IOException e) {

e.printStackTrace()

}

}

}

public long getFileSize(String urlString) throws IOException,Exception{

long lenght = 0

String url = UrlEncode(urlString, "UTF-8")

//URL mUrl = new URL(urlString)

URL mUrl = new URL(url)

HttpURLConnection conn = (HttpURLConnection) mUrl.openConnection()

conn.setConnectTimeout(5*1000)

conn.setRequestMethod("GET")

conn .setRequestProperty("Accept-Encoding", "identity")

conn.setRequestProperty("Referer", url)

//conn.setRequestProperty("Referer", urlString)

conn.setRequestProperty("Charset", "UTF-8")

conn.setRequestProperty("Connection", "Keep-Alive")

conn.connect()

int responseCode = conn.getResponseCode()

// 判断请求是否成功处理

if (responseCode == HttpStatus.SC_OK) {

lenght = conn.getContentLength()

}

return lenght

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/8066507.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-13
下一篇 2023-04-13

发表评论

登录后才能评论

评论列表(0条)

保存