1. 程式人生 > >Java獲取檔案大小

Java獲取檔案大小

一、通過length方法:

1、建立一個檔案:

1 File file = new File("E:\\全部軟體\\軟體壓縮包\\Windows7_W64_SP1_ent.iso");

2、獲取檔案大小:

複製程式碼

    /**
     * 獲取檔案長度
     * @param file
     */
    public static void getFileSize1(File file) {
        if (file.exists() && file.isFile()) {
            String fileName = file.getName();
            System.out.println("檔案"+fileName+"的大小是:"+file.length());
        }
    }

 

 

like12---

//like12 add,20170314,bug,個別學員照片 檔案存在 但大小為0

if(!StringUtils.isEmpty(employee.getPhoto())){

//判斷檔案大小是否為0

String filePath = com.bstek.dorado.core.Configure

.getString(Constants.file_upload_config_path) +

"/" + employee.getPhoto();

File f = new File(filePath);

if (f.exists() && f.isFile()) {

if(f.length() <= 0){

return "照片檔案大小為:" + f.length() +

",是無效圖片,請重新上傳!";

}

} else {

return employee.getPhoto() + "照片檔案不存在!";

}

}