通过URLConnection的guessContentTypeFromName方法可以获取文件的内容类型。 // 从文件名中猜测内容类型StringcontentType=URLConnection.guessContentTypeFromName(file.getName());// 检查内容类型是否为空if(contentType==null){contentType="application/octet-stream";// 默认内容类型}// 输出内容类型System.out.prin...
importjava.io.File;importandroid.webkit.MimeTypeMap;publicclassFileContentType{publicstaticStringgetFileContentType(StringfilePath){Filefile=newFile(filePath);StringfileName=file.getName();Stringextension=fileName.substring(fileName.lastIndexOf(".")+1);MimeTypeMapmime=MimeTypeMap.getSingleton();Stringm...
import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class ContentTypeExample { public static String getContentTypeUsingFiles(String filePath) { String contentType = null; try { Path path = Paths.get(filePath); contentType = Files.probeContentType(path)...
public static String getContentTypeByType(String fileUrl) { String contentType = null; try { FileNameMap fileNameMap = URLConnection.getFileNameMap(); contentType = fileNameMap.getContentTypeFor(fileUrl); } catch (Exception e) { e.printStackTrace(); } System.out.println("getContentTypeBy...
* Java获取文件ContentType *@authorouyangjun */publicclassContentTypeUtils{publicstaticvoidmain(String[] args){// 文件路径StringfileUrl="C:\\Users\\admin\\Desktop\\tttt.rar";// 方式一getContentTypeByLocal(fileUrl);// 方式二,推荐使用getContentType(fileUrl);// 方式三getContentTypeByType(file...
publicvoidtest(){File file =newFile("d:/test.png"); FileNameMap fileNameMap = URLConnection.getFileNameMap(); String mimeType = fileNameMap.getContentTypeFor(file.getName());} 结果:该方法返回URLConnection的所有实例使用的MIME类型表。然后,该表用于解析输入文件类型。当涉及URLConnection...
Java Oracle Java 是第一大编程语言和开发平台。它有助于企业降低成本、缩短开发周期、推动创新以及改善应用程序服务。Java 现在仍是企业和开发人员的首选开发平台。 用于运行桌面应用程序的 Java 面向使用台式机和笔记本电脑的最终用户 下载适用于台式机的 Java...
addPart("filesFileName", 16 new StringBody(fileParamName, ContentType.create("text/plain", Consts.UTF_8))); 17 if (params != null) { 18 for (String key : params.keySet()) { 19 builder.addPart(key, 20 new StringBody(params.get(key), ContentType.create("text/plain", Consts.UTF...
使用URLConnection获得MIME类型的更快方法是使用getFileNameMap()方法:public void test(){ File file = new File("d:/test.png"); FileNameMap fileNameMap = URLConnection.getFileNameMap(); String mimeType = fileNameMap.getContentTypeFor(file.getName());}结果: 文件结果结论 该方法返回URL...
}@OverridepublicStringgetName(){returnthis.name; }@OverridepublicStringgetOriginalFilename(){returnthis.originalFilename; }@OverridepublicStringgetContentType(){returnthis.contentType; }@OverridepublicbooleanisEmpty(){return(this.content.length ==0); ...