Java Minute Get getMineType(String urlFile)

Here you can find the source of getMineType(String urlFile)

Description

get Mine Type

License

Apache License

Declaration

public static String getMineType(String urlFile) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.net.HttpURLConnection;
import java.net.URL;

import java.text.DecimalFormat;

public class Main {
    public static String getMineType(String urlFile) {
        URL url;/*from   ww w .ja  va2  s  .co m*/
        try {
            url = new URL(urlFile);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            return countFileSize(connection.getContentLength());
        } catch (Exception e) {
            return "0";
        }
    }

    public static String countFileSize(String pathname) {
        String fileSizeString = "";
        try {
            File file = new File(pathname);
            DecimalFormat df = new DecimalFormat("#.00");
            long fileS = file.length();
            if (fileS < 1024) {
                fileSizeString = "0byte";
            } else if (fileS < 1048576) {
                fileSizeString = df.format((double) fileS / 1024) + "KB";
            } else if (fileS < 1073741824) {
                fileSizeString = df.format(((double) fileS / 1024 / 1024) - 0.01) + "MB";
            } else {
                fileSizeString = df.format((double) fileS / 1024 / 1024 / 1024) + "G";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileSizeString;
    }

    public static String countFileSize(long fileSize) {
        String fileSizeString = "";
        try {
            DecimalFormat df = new DecimalFormat("#.00");
            long fileS = fileSize;
            if (fileS == 0) {
                fileSizeString = "0KB";
            } else if (fileS < 1024) {
                fileSizeString = df.format((double) fileS) + "B";
            } else if (fileS < 1048576) {
                fileSizeString = df.format((double) fileS / 1024) + "KB";
            } else if (fileS < 1073741824) {
                fileSizeString = df.format(((double) fileS / 1024 / 1024) - 0.01) + "MB";
            } else {
                fileSizeString = df.format((double) fileS / 1024 / 1024 / 1024) + "G";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileSizeString;
    }
}

Related

  1. getLessThanOneMinuteAgoRendering()
  2. getMilliSecondFromMinute(int minute)
  3. getMin()
  4. getMin(String dateTime)
  5. getMinDateString(Date date)
  6. getMininte(Date time)
  7. getMiniTime()
  8. getMinute()
  9. getMinute()