publicvoidtest(){File file =newFile("d:/test.png"); String mimeType = URLConnection.guessContentTypeFromName(file.getName());} 结果:该方法利用内部的FileNameMap来判断MIME类型。结论:根据文件扩展名判断。2.3 使用guessContentTypeFromStream publicstaticvoidtest()throws Exception{FileInputStream i...
3.3. 使用getFileNameMap () 使用URLConnection获取 MIME 类型的更快方法是使用getFileNameMap()方法: @Test public void whenUsingGetFileNameMap_thenSuccess(){ File file = new File("product.png"); FileNameMap fileNameMap = URLConnection.getFileNameMap(); String mimeType = fileNameMap.getContent...
put("wmv", "video/x-ms-wmv"); } public static void main(String[] args) throws IOException { String filePath = "path/to/your/file"; String mimeType = getMimeType(filePath); System.out.println("MIME type of the file: " + mimeType); } public static String getMimeType(String fileP...
最有趣的类是javax.activation.MimeType-一个实际的MIME型保持架-和javax.activation.MimetypesFileTypeMap-类,其实例可以将MIME类型解析为文件的字符串:String fileName = "/path/to/file";MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();// only by file nameString mimeType = mimeTypesMap.getConte...
上述代码中,我们首先创建了一个File对象,指定了文件的名称为example.txt。然后,我们调用getMimeType方法来获取文件的MIME类型。该方法使用URLConnection类的guessContentTypeFromName方法来猜测文件的MIME类型。 输出结果为: File MIME type: text/plain 1.
mimeType= URLConnection.guessContentTypeFromName(file.getName()); 还有以下内容,它的优点不仅仅是使用文件扩展名,还可以查看内容。 InputStream is = new BufferedInputStream(new FileInputStream(file));mimeType = URLConnection.guessContentTypeFromStream(is); //...close stream 然而,正如上面的注释所建议的...
publicvoidtest(){File file =newFile("d:/test.png"); String mimeType = URLConnection.guessContentTypeFromName(file.getName());} 结果: 该方法利用内部的FileNameMap来判断MIME类型。 结论:根据文件扩展名判断。 2.3 使用guessContentTypeFromStream ...
Reads and stores the mime type setting corresponding to a file extension, by reading text from an InputStream. If a mime type setting already exists when this method is run, the mime type value is replaced with the newer one. 参数: is - 抛出: IOException getMimetype public String getMim...
@TestpublicvoidwhenUsingGuessContentTypeFromName_thenSuccess(){Filefile=newFile("product.png");StringmimeType=URLConnection.guessContentTypeFromName(file.getName()); assertEquals(mimeType,"image/png"); }Copy This method makes use of the internalFileNameMaptoresolve the MIME type from the extension...
String fileName = UUID.randomUUID() + "_" + System.currentTimeMillis() + "_" + fileItem.getName(); fileItem.write(new File(fileRealPathDir, fileName)); //7、拷贝 System.out.println("文件传输成功"); resp.setContentType("text/html;charset=utf-8"); ...