Java Code Examples for java.net.URLConnection#getFileNameMap()

The following examples show how to use java.net.URLConnection#getFileNameMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PostFormRequest.java    From okhttputils with Apache License 2.0 6 votes vote down vote up
private String guessMimeType(String path)
{
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentTypeFor = null;
    try
    {
        contentTypeFor = fileNameMap.getContentTypeFor(URLEncoder.encode(path, "UTF-8"));
    } catch (UnsupportedEncodingException e)
    {
        e.printStackTrace();
    }
    if (contentTypeFor == null)
    {
        contentTypeFor = "application/octet-stream";
    }
    return contentTypeFor;
}
 
Example 2
Source File: OkPostFormRequest.java    From FamilyChat with Apache License 2.0 6 votes vote down vote up
private String guessMimeType(String path)
{
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentTypeFor = null;
    try
    {
        contentTypeFor = fileNameMap.getContentTypeFor(URLEncoder.encode(path, "UTF-8"));
    } catch (UnsupportedEncodingException e)
    {
        e.printStackTrace();
    }
    if (contentTypeFor == null)
    {
        contentTypeFor = "application/octet-stream";
    }
    return contentTypeFor;
}
 
Example 3
Source File: AbstractFileTypeDetector.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Invokes the appropriate probe method to guess a file's content type,
 * and checks that the content type's syntax is valid.
 */
@Override
public final String probeContentType(Path file) throws IOException {
    if (file == null)
        throw new NullPointerException("'file' is null");
    String result = implProbeContentType(file);

    // Fall back to content types property.
    if (result == null) {
        Path fileName = file.getFileName();
        if (fileName != null) {
            FileNameMap fileNameMap = URLConnection.getFileNameMap();
            result = fileNameMap.getContentTypeFor(fileName.toString());
        }
    }

    return (result == null) ? null : parse(result);
}
 
Example 4
Source File: FileContentInfoFilenameFactory.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
@Override
public FileContentInfo create(final FileContent fileContent) {
    String contentType = null;

    final String name = fileContent.getFile().getName().getBaseName();
    if (name != null) {
        final FileNameMap fileNameMap = URLConnection.getFileNameMap();
        contentType = fileNameMap.getContentTypeFor(name);
    }

    // optimize object creation for common case
    if (contentType == null) {
        return NULL_INFO;
    } else {
        return new DefaultFileContentInfo(contentType, null);
    }
}
 
Example 5
Source File: FileHandler.java    From raccoon4 with Apache License 2.0 5 votes vote down vote up
public FileHandler(LifecycleManager lm) {
	this.lm = lm;
	fileNameMap = URLConnection.getFileNameMap();
	mappings = new HashStore<File>();
	keys = new ArrayList<String>();
	timeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
			DateFormat.MEDIUM);
}
 
Example 6
Source File: HttpUtils.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
/** 根据文件名获取MIME类型 */
public static MediaType guessMimeType(String fileName) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    fileName = fileName.replace("#", "");   //解决文件名中含有#号异常的问题
    String contentType = fileNameMap.getContentTypeFor(fileName);
    if (contentType == null) {
        return HttpParams.MEDIA_TYPE_STREAM;
    }
    return MediaType.parse(contentType);
}
 
Example 7
Source File: Util.java    From OkHttpPacker with Apache License 2.0 5 votes vote down vote up
public static String getFileMimeType(String path) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentTypeFor = fileNameMap.getContentTypeFor(path);
    if (contentTypeFor == null) {
        contentTypeFor = "application/octet-stream";
    }
    return contentTypeFor;
}
 
Example 8
Source File: UploadRequestBuilder.java    From OkHttpPlus with Apache License 2.0 5 votes vote down vote up
private static String guessMimeType(String path) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentTypeFor = fileNameMap.getContentTypeFor(path);
    if (contentTypeFor == null) {
        contentTypeFor = "application/octet-stream";
    }
    return contentTypeFor;
}
 
Example 9
Source File: KCWebViewClient.java    From kerkee_android with GNU General Public License v3.0 5 votes vote down vote up
public String getFileMimeType(String aUrl)
{
    try
    {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String type = fileNameMap.getContentTypeFor(aUrl);
        return type;
    }
    catch (Exception e)
    {
        KCLog.e(e);
    }
    return null;
}
 
Example 10
Source File: HttpParams.java    From RxEasyHttp with Apache License 2.0 5 votes vote down vote up
private MediaType guessMimeType(String path) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    path = path.replace("#", "");   //解决文件名中含有#号异常的问题
    String contentType = fileNameMap.getContentTypeFor(path);
    if (contentType == null) {
        contentType = "application/octet-stream";
    }
    return MediaType.parse(contentType);
}
 
Example 11
Source File: OkHttpUploadRequest.java    From meiShi with Apache License 2.0 5 votes vote down vote up
private String guessMimeType(String path)
{
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentTypeFor = fileNameMap.getContentTypeFor(path);
    if (contentTypeFor == null)
    {
        contentTypeFor = "application/octet-stream";
    }
    return contentTypeFor;
}
 
Example 12
Source File: FileUtil.java    From NewFastFrame with Apache License 2.0 5 votes vote down vote up
/**
 * 根据文件名获取MIME类型
 */
public static MediaType guessMimeType(String fileName) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    fileName = fileName.replace("#", "");   //解决文件名中含有#号异常的问题
    String contentType = fileNameMap.getContentTypeFor(fileName);
    if (contentType == null) {
        return MediaType.parse("application/octet-stream");
    }
    return MediaType.parse(contentType);
}
 
Example 13
Source File: OFileManager.java    From hr with GNU Affero General Public License v3.0 5 votes vote down vote up
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();
    }
}
 
Example 14
Source File: MimeTypeUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
/**
 * Test method demonstrating the usage of FileNameMap from URLConnection
 * to resolve MIME type of a file.
 * 
 */
@Test
public void whenUsingGetFileNameMap_thenSuccess() {
    final File file = new File(FILE_LOC);
    final FileNameMap fileNameMap = URLConnection.getFileNameMap();
    final String mimeType = fileNameMap.getContentTypeFor(file.getName());
    assertEquals(mimeType, PNG_EXT);
}
 
Example 15
Source File: Utils.java    From DUtil with Apache License 2.0 5 votes vote down vote up
/**
 * 根据文件名解析contentType
 *
 * @param name
 * @return
 */
public static String getMimeType(String name) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentTypeFor = null;
    try {
        contentTypeFor = fileNameMap.getContentTypeFor(URLEncoder.encode(name, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    if (contentTypeFor == null) {
        contentTypeFor = "application/octet-stream";
    }
    return contentTypeFor;
}
 
Example 16
Source File: FileUtil.java    From qaf with MIT License 4 votes vote down vote up
public static String getContentType(File f) {
	MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
	FileNameMap fileNameMap = URLConnection.getFileNameMap();
	String ct = fileNameMap.getContentTypeFor(f.getName());
	return StringUtil.isBlank(ct) ? fileTypeMap.getContentType(f) : ct;
}
 
Example 17
Source File: PBitmapUtils.java    From YImagePicker with Apache License 2.0 4 votes vote down vote up
public static String getMimeTypeFromPath(String path) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    return fileNameMap.getContentTypeFor(new File(path).getName());
}
 
Example 18
Source File: UtilHttp.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
public static String getContentTypeByFileName(String fileName) {
    FileNameMap mime = URLConnection.getFileNameMap();
    return mime.getContentTypeFor(fileName);
}
 
Example 19
Source File: MIMETypeUtil.java    From ModTheSpire with MIT License 4 votes vote down vote up
/**
 * Get the MIME type for a file name. This method is simply a convenient
 * front-end for <tt>java.net.FileNameMap.getContentTypeFor()</tt>,
 * but it applies the supplied default when <tt>getContentTypeFor()</tt>
 * returns null (which can happen).
 *
 * @param fileName        the file name
 * @param defaultMIMEType the default MIME type to use if one cannot
 *                        be determined from the file name, or null to
 *                        use {@link #DEFAULT_MIME_TYPE}
 *
 * @return the MIME type to use
 *
 * @see #MIMETypeForFile(File,String)
 * @see #MIMETypeForFileName(String)
 * @see #MIMETypeForFileExtension(String)
 * @see #MIMETypeForFileExtension(String,String)
 * @see #DEFAULT_MIME_TYPE
 */
public static String MIMETypeForFileName (String fileName,         // NOPMD
                                          String defaultMIMEType)
{
    String mimeType = null;
    FileNameMap fileNameMap = URLConnection.getFileNameMap();

    // Check ours first.

    loadMappings();

    String extension = FileUtil.getFileNameExtension (fileName);
    mimeType = (String) extensionToMIMETypeMap.get (extension);

    if (mimeType == null)
    {
        // Check the system one.

        mimeType = fileNameMap.getContentTypeFor (fileName);
    }

    if (mimeType != null)
    {
        if (mimeType.equals (DEFAULT_MIME_TYPE) &&
            (defaultMIMEType != null))
        {
            // Substitute the caller's default, if there is one, on the
            // assumption that it'll be more useful.

            mimeType = defaultMIMEType;
        }
    }

    else
    {
        mimeType = (defaultMIMEType == null) ? DEFAULT_MIME_TYPE
                                             : defaultMIMEType;
    }

    return mimeType;
}
 
Example 20
Source File: FileUtil.java    From utils with Apache License 2.0 2 votes vote down vote up
/**
 * 获取文件的Mime类型.
 *
 * @param file 需要处理的文件
 * @return 返回文件的mime类型
 * @throws java.io.IOException
 */
public static String mimeType(String file) throws java.io.IOException {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    return fileNameMap.getContentTypeFor(file);
}