package com.hare.file.utils;
import org.springframework.http.MediaType;
public class FilePathUtils {
/**
* 获取文件的类型,对应的响应类型
*
* @param namePath 文件名
* @return: org.springframework.http.MediaType
*/
public static MediaType getMediaType(String namePath) {
int i = namePath.indexOf(".") + 1;
String substring = namePath.substring(i);
MediaType mediaType;
switch (substring) {
case "jpg":
mediaType = MediaType.IMAGE_JPEG;
break;
case "png":
mediaType = MediaType.IMAGE_PNG;
break;
case "pdf":
mediaType = MediaType.APPLICATION_PDF;
break;
default:
mediaType = MediaType.APPLICATION_OCTET_STREAM;
break;
}
return mediaType;
}
/**
* 获取文件的类型,对应的响应类型
*
* @param namePath 文件名
* @return: org.springframework.http.MediaType
*/
public static String getMinorMediaType(String namePath) {
int i = namePath.indexOf(".") + 1;
String substring = namePath.substring(i);
String mediaType;
switch (substring) {
case "pdf":
mediaType = "application/pdf";
break;
case "doc":
mediaType = "application/msword";
break;
case "docx":
mediaType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
break;
case "xls":
mediaType = "application/vnd.ms-excel";
break;
case "xlsx":
mediaType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
break;
case "jpg":
mediaType = "image/jpeg";
break;
case "png":
mediaType = "image/png";
break;
case "jpeg":
mediaType = "image/jpeg";
break;
default:
mediaType = "application/octet-stream";
break;
}
return mediaType;
}
}
FilePathUtils_获取文件的类型,对应的响应类型
最新推荐文章于 2022-01-29 18:29:20 发布