導航:首頁 > 編程語言 > java獲取file的大小

java獲取file的大小

發布時間:2022-12-25 21:09:27

❶ 用java代碼如何查看本地一個文件的大小

publicstaticvoidgetFileSize(Stringpath){
//傳入文件路徑
Filefile=newFile(path);
//測試此文件是否存在
if(file.exists()){
//如果是文件夾
//這里只檢測了文件夾中第一層如果有需要可以繼續遞歸檢測
if(file.isDirectory()){
intsize=0;
for(Filezf:file.listFiles()){
if(zf.isDirectory())continue;
size+=zf.length();
}
System.out.println("文件夾"+file.getName()+"Size:"+(size/1024f)+"kb");
}else{
System.out.println(file.getName()+"Size:"+(file.length()/1024f)+"kb");
}
//如果文件不存在
}else{
System.out.println("此文件不存在");
}
}

❷ java web 如何獲得文件上傳大小

有一種叫jspsmartupload的包用來簡化文件上傳下載的編寫
裡面可以獲取文件大小

//取得文件
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
//取得文件名
String fileName = myFile.getFileName();
//取得文件大小
int fileSize = myFile.getSize();
這是基於spring架構的上傳文件支持多個文件上傳,拿到file對象後,直接file.size()就可以獲取文件的大小,
if (request instanceof MultipartHttpServletRequest) {
MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
for (Iterator it = multipartHttpServletRequest.getFileNames(); it.hasNext();) {
String key = (String) it.next();
MultipartFile file = multipartHttpServletRequest.getFile(key);
String originalFilename = file.getOriginalFilename();
long size = file.getSize();//文件大小需要轉換成KB或M
if (StringUtils.isNotBlank(originalFilename)) {
String suffixName = originalFilename.indexOf(".") == -1 ? "" : originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();
try {
InputStream inputStream = file.getInputStream();
byte[] ToByteArray = FileCopyUtils.ToByteArray(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

❸ 利用Java文件類File的方法,獲取磁碟文件的文件名、長度、大小等特性。

package com.texst;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class GetFileMessageOfDisk {

/**
* @param args
*/
public static void main(String[] args) {
File file = new File("D:/");
getMessage(file);
}
private static void getMessage(File file){
File[] files = file.listFiles();
if(files==null){
files = new File[0];
}
for(int i=0;i<files.length;i++){
if(files[i].isDirectory()){
System.out.println(files[i].getPath());
getMessage(files[i]);
}else{
try {
int length = new FileInputStream(files[i]).available();
System.out.println(files[i].getName()+" 長度:"+length+" 大小:"+length/1024+"KB");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

}

android的java怎麼獲取文件大小

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();
}
}
}

❺ 如何用java獲取網路文件的大小

try{
System.out.println("正在鏈接URL");
url=new URL("http://211.64.201.201/uploadfile/nyz.mp3");
HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
//根據響應獲取文件大小
fileLength=urlcon.getContentLength();
if(urlcon.getResponseCode()>=400){
System.out.println("伺服器響應錯誤");
System.exit(-1);
}
if(fileLength<=0)
System.out.println("無法獲知文件大小");
//列印信息
printMIME(urlcon);
System.out.println("文件大小為"+fileLength/1024+"K");
//獲取文件名
String trueurl=urlcon.getURL().toString();
String filename=trueurl.substring(trueurl.lastIndexOf('/')+1);
fileOut=new File("D://",filename);
}
catch(MalformedURLException e){
System.err.println(e);
}
catch(IOException e){
System.err.println(e);
}

❻ java怎麼查看file的大小

FileInputStream fis=new FileInputStream(new File(filepath));
System.out.println(fis.available());

當然裡面需要處理下異常

❼ Java獲取下載文件的大小

可以直接通過HttpURLConnection 的getContentLength()方法來獲取下載文件的大小。
//找到要下載的文件
url=new URL( "http://www..com/uploadfile/oracle.txt");
//根據響應獲取文件大小
HttpURLConnection urlcon=(HttpURLConnection)url.openConnection();
//獲取相應的文件長度
fileLength=urlcon.getContentLength();
備註:以上方法中url變換即可,下面的方法不用變更,即可獲取到對應下載文件的大小。

閱讀全文

與java獲取file的大小相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:962
phpffmpeg轉碼 瀏覽:671
長沙好玩的解壓項目 瀏覽:144
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:737
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:484
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:349
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:892
app轉賬是什麼 瀏覽:163