我想从MimeType获取扩展名。
例如:
video/mp4 ----> mp4
application/x-rar-compressed ----> rar
text/plain ----> txt
application/pdf ----> pdf
Run Code Online (Sandbox Code Playgroud)
为了在 Windows 或 Linux 上保存我的文件,我有这个方法可以让我从 URL 下载文件。所以,我只能在没有扩展的情况下保存它(我不想那样)。
这个方法只让我得到MimeType而不是extension。
public void downloadImage() {
String dirPath = "/home/MyPC/Downloaded";
String fileName = "My downloaded file";
URL fetchFile = new URL(IMAGE_URL);
byte[] fileAsArray = Resources.toByteArray(fetchFile);
//Getting the MimeType
String mimeType = URLConnection.guessContentTypeFromStream(new ByteArrayInputStream(fileAsArray));
System.out.println(mimeType);
File fileToWriteTo = new File(dirPath.concat("/" + fileName));
//Saving the file using Guava
Files.write(fileAsArray, fileToWriteTo);
}
Run Code Online (Sandbox Code Playgroud)
如何使用 MimeType 编写带有扩展名的文件?