您的位置:首页 > 编程语言 > Java开发

明日工作中需要用到的两个方法 java中File的基本使用

2017-06-07 22:34 441 查看
/**
* 获取一个文件的类型
* @param path 一个文件路径
* @return
* @throws IOException
*/
public static String getFileType (String path) throws IOException {

File file = new File(path);
if(!file.exists()) {

boolean createNewFile = file.createNewFile();

}
String name = file.getName();

String fileType = name.substring(name.lastIndexOf(".")+1,name.length());

return fileType;

}
/**
*
* @param path  一个文件路径
* @return  获取一个文件的大小
* @throws IOException
*/
public static String getFileSize (String path) throws IOException {

File file = new File(path);
if(!file.exists()) {

boolean createNewFile = file.createNewFile();

}

String filesize = file.length()/1024/1024+"";

return filesize;

}


项目中我已经 过滤出来 是音频 视频和 图片的 localpath 了 再加上这个判断 文件不能大于 20M 和 文件类型做为入参 上传文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java