How to use FileNameMap interface in Java?

天大地大妈咪最大 提交于 2019-12-10 12:05:15

问题


This interface provides one method, namely getContentTypeFor(String fileName). However, I have no idea how to use it. I implemented interface in Eclipse and ended with:

import java.net.FileNameMap;

public class Fnam implements FileNameMap {

public static void main(String[] args) {

}

@Override
public String getContentTypeFor(String fileName) {
    return null;
}
}

The method returns null. How should I change it to get the MIME type ?


回答1:


It is interface for inner implementation of JDK, and in most cases you should use just interface implementation, not interface itself.

Here simple sample of usage:

public class Main {
    private static FileNameMap fileNameMap = URLConnection.getFileNameMap();

    public static void main(String... str) {
        System.out.print(fileNameMap.getContentTypeFor("my_file.xml"));
    }

}

prints:

application/xml

And seems only MimeTable is only current implementation of that interface in JDK.




回答2:


A FileNameMap is an interface returned by methods of a class that understands filetypes. For example there is a URLConnection class that has a getFileNameMap() method, which is used like this.

private void requestIntent(Uri uri) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    FileNameMap mime = URLConnection.getFileNameMap();
    String mimeType = mime.getContentTypeFor(uri.getPath());
    intent.setDataAndType(uri, mimeType);
    try {
        mActivity.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(mActivity, OResource.string(mActivity, R.string.toast_no_activity_found_to_handle_file), Toast.LENGTH_LONG).show();
    }
}

That example from here

So, you usually do not use FileNameMap in isolation. Instead you either use an existing library class that creates objects that implement FileNameMap. If you did want to implement such a library you would need to write code like this (taken from the source of UrlConnection)

public static synchronized FileNameMap More getFileNameMap() {
316         if ((fileNameMap == null) && !fileNameMapLoaded) {
317             fileNameMap = sun.net.www.MimeTable.loadTable();
318             fileNameMapLoaded = true;
319         }
320 
321         return new FileNameMap() {
322             private FileNameMap map = fileNameMap;
323             public String getContentTypeFor(String fileName) {
324                 return map.getContentTypeFor(fileName);
325             }
326         };
327     }

You see here that the implementation creates an anonymous class implementing the interface; your responsibility as implementor of the interface would be to figure a way to implement that getContentTypeFor() method.

If all you want to do is get the mime type for a file then you can use URLConnection to give you an object that already has that implementation, and so just use the approach shown in the answer to the related question



来源:https://stackoverflow.com/questions/41423633/how-to-use-filenamemap-interface-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!