package com.utils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.HashMap;
public class UnsFileUtils {
//判断文件是否存在
public static boolean fileIsExists(String strFile)
{
try
{
File f=new File(strFile);
if(!f.exists())
{
return false;
}
}
catch (Exception e)
{
return false;
}
return true;
}
//判断是否是文件
public static boolean isFile(String strFile)
{
boolean fileIsExists = fileIsExists(strFile);
if(fileIsExists){
File f=new File(strFile);
boolean file = f.isFile();
return file;
}else{
return false;
}
}
public static String getFileName(String strFile)
{
try
{
File f=new File(strFile);
if(f.exists())
{
String fileName = f.getName();
return fileName;
}
}
catch (Exception e)
{
return null;
}
return null;
}
public static String getFileNameDetail(String strFile)
{
try
{
File f=new File(strFile);
if(f.exists())
{
String name = f.getName();
String type=name.substring(name.lastIndexOf(".") + 1);
name=name.replace("."+type, "");
return name;
}
}
catch (Exception e)
{
return null;
}
return null;
}
/**
* 获取文件类型
* @param fullPath
* @return
*/
public static String getFileType(String fullPath){
try {
File file = new File(fullPath);
if(file.exists()){
String fileName = file.getName();
return fileName.substring(fileName.lastIndexOf(".") + 1);
}else{
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static long getFileLength(String Path){
long fileS=0l;
try {
File file=new File(Path);
if(file.exists() && file.isFile()){
fileS = file.length();
}
} catch (Exception e) {
e.printStackTrace();
}
return fileS;
}
/**
* 获取文件大小
* @param file
* @return
*/
public static String GetFileSize(String Path){
return GetFileSize(new File(Path));
}
public static String GetFileSize(File file){
String size = "";
if(file.exists() && file.isFile()){
long fileS = file.length();
DecimalFormat df = new DecimalFormat("#.00");
if (fileS < 1024) {
size = df.format((double) fileS) + "BT";
} else if (fileS < 1048576) {
size = df.format((double) fileS / 1024) + "KB";
} else if (fileS < 1073741824) {
size = df.format((double) fileS / 1048576) + "MB";
} else {
size = df.format((double) fileS / 1073741824) +"GB";
}
}else if(file.exists() && file.isDirectory()){
size = "";
}else{
size = "0BT";
}
return size;
}
private static final HashMap<String, String> mFileTypes = new HashMap<String, String>();
static {
// images
mFileTypes.put("FFD8FF", "jpg");
mFileTypes.put("89504E47", "png");
mFileTypes.put("47494638", "gif");
mFileTypes.put("49492A00", "tif");
mFileTypes.put("424D", "bmp");
//other
mFileTypes.put("41433130", "dwg"); // CAD
mFileTypes.put("38425053", "psd");
mFileTypes.put("7B5C727466", "rtf"); // 日记本
mFileTypes.put("3C3F786D6C", "xml");
mFileTypes.put("68746D6C3E", "html");
mFileTypes.put("44656C69766572792D646174653A", "eml"); // 邮件
mFileTypes.put("D0CF11E0", "doc");
mFileTypes.put("5374616E64617264204A", "mdb");
mFileTypes.put("252150532D41646F6265", "ps");
mFileTypes.put("255044462D312E", "pdf");
mFileTypes.put("504B0304", "docx");
mFileTypes.put("52617221", "rar");
mFileTypes.put("57415645", "wav");
mFileTypes.put("41564920", "avi");
mFileTypes.put("2E524D46", "rm");
mFileTypes.put("000001BA", "mpg");
mFileTypes.put("000001B3", "mpg");
mFileTypes.put("6D6F6F76", "mov");
mFileTypes.put("3026B2758E66CF11", "asf");
mFileTypes.put("4D546864", "mid");
mFileTypes.put("1F8B08", "gz");
mFileTypes.put("377ABCAF", "7z");
mFileTypes.put("2142444E", "pst");
mFileTypes.put("747874", "txt");
mFileTypes.put("786C73", "xls");
mFileTypes.put("786C7378", "xlsx");
mFileTypes.put("646F6378", "docx");
mFileTypes.put("707074", "ppt");
mFileTypes.put("70707478", "pptx");
mFileTypes.put("545854", "TXT");
mFileTypes.put("584C53", "XLS");
mFileTypes.put("584C5358", "XLSX");
mFileTypes.put("444F4358", "DOCX");
mFileTypes.put("505054", "PPT");
mFileTypes.put("50505458", "PPTX");
mFileTypes.put("726172", "rar");
mFileTypes.put("7A6970", "zip");
mFileTypes.put("524152", "RAR");
mFileTypes.put("5A4950", "ZIP");
mFileTypes.put("4A5047", "JPG");
mFileTypes.put("504E47", "PNG");
mFileTypes.put("474946", "GIF");
mFileTypes.put("544946", "TIF");
mFileTypes.put("424D50", "BMP");
}
/**
* 获取文件类型
* ps:流会关闭
*
* @param inputStream
* @return
*/
public static String getFileType(InputStream inputStream) {
return mFileTypes.get(getFileHeader(inputStream));
}
private static String getFileHeader(InputStream inputStream) {
String value = null;
try {
byte[] b = new byte[4];
/*int read() 从此输入流中读取一个数据字节。
*int read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
* int read(byte[] b, int off, int len) 从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
*/
inputStream.read(b, 0, b.length);
value = bytesToHexString(b);
} catch (Exception e) {
} finally {
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
}
}
}
return value;
}
/**
* 将要读取文件头信息的文件的byte数组转换成string类型表示
*
* @param src 要读取文件头信息的文件的byte数组
* @return 文件头信息
*/
private static String bytesToHexString(byte[] src) {
StringBuilder builder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
String hv;
for (int i = 0; i < src.length; i++) {
// 以十六进制(基数 16)无符号整数形式返回一个整数参数的字符串表示形式,并转换为大写
hv = Integer.toHexString(src[i] & 0xFF).toUpperCase();
if (hv.length() < 2) {
builder.append(0);
}
builder.append(hv);
}
return builder.toString();
}
}