我想从MimeType获得扩展.
例如:
video/mp4 ----> mp4
application/x-rar-compressed ----> rar
text/plain ----> txt
application/pdf ----> pdf
要在Windows或Linux上保存我的文件,我有这个方法,让我从URL下载文件.所以,我只能在没有扩展名的情况下保存它(我不想那样).
这个方法只允许我获取MimeType而不是扩展名.
public void DownloadImage() {
String TheFolderPath = "/home/MyPC/Downloaded";
String File_NAME = "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(TheFolderPath.concat("/" + File_NAME));
//Saving the file using Guava
Files.write(FileAsArray, fileToWriteTo);
}
如何使用MimeType编写带扩展名的文件?