java根據檔案名稱判斷檔案類型__java,工具類

來源:互聯網
上載者:User

/* author:zss
 * 日期:2017年3月31日
 * 功能:根據檔案名稱判斷類型
 * 接受參數類型:String 
 * 返回參數類型:String
 * 備忘:檔案類型不完善,有需要的自行添加
 */
public class FileType {
public String fileType(String fileName) {
if (fileName == null) {
fileName = "檔案名稱為空白。";
return fileName;


} else {
// 擷取檔案尾碼名並轉化為寫,用於後續比較
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length()).toLowerCase();
// 建立圖片類型數組
String img[] = { "bmp", "jpg", "jpeg", "png", "tiff", "gif", "pcx", "tga", "exif", "fpx", "svg", "psd",
"cdr", "pcd", "dxf", "ufo", "eps", "ai", "raw", "wmf" };
for (int i = 0; i < img.length; i++) {
if (img[i].equals(fileType)) {
return "圖片";
}
}


// 建立文件類型數組
String document[] = { "txt", "doc", "docx", "xls", "htm", "html", "jsp", "rtf", "wpd", "pdf", "ppt" };
for (int i = 0; i < document.length; i++) {
if (document[i].equals(fileType)) {
return "文檔";
}
}
// 建立視頻類型數組
String video[] = { "mp4", "avi", "mov", "wmv", "asf", "navi", "3gp", "mkv", "f4v", "rmvb", "webm" };
for (int i = 0; i < video.length; i++) {
if (video[i].equals(fileType)) {
return "視頻";
}
}
// 建立音樂類型數組
String music[] = { "mp3", "wma", "wav", "mod", "ra", "cd", "md", "asf", "aac", "vqf", "ape", "mid", "ogg",
"m4a", "vqf" };
for (int i = 0; i < music.length; i++) {
if (music[i].equals(fileType)) {
return "音樂";
}
}


}
return "其他";
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.