代码:
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
public class Test {
public static void main(String[] args) {
String fileName = "1.docx";
String contentType = MediaTypeFactory.getMediaType(fileName).orElse(MediaType.APPLICATION_OCTET_STREAM).toString();
System.out.println("文件名称为“" + fileName + "”的contentType:" + contentType);
}
}
结果:
文件名称为“1.docx”的contentType:application/vnd.openxmlformats-officedocument.wordprocessingml.document