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

The following examples show how to use java.net.URLConnection#getFileNameMap() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: FamilyChat   File: OkPostFormRequest.java    License: 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
@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 3
/**
 * 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 Project: okhttputils   File: PostFormRequest.java    License: 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 5
Source Project: meiShi   File: OkHttpUploadRequest.java    License: 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 6
Source Project: DUtil   File: Utils.java    License: 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 7
Source Project: tutorials   File: MimeTypeUnitTest.java    License: 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 8
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 9
Source Project: NewFastFrame   File: FileUtil.java    License: 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 10
Source Project: raccoon4   File: FileHandler.java    License: 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 11
Source Project: RxEasyHttp   File: HttpParams.java    License: 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 12
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 13
Source Project: OkHttpPlus   File: UploadRequestBuilder.java    License: 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 14
Source Project: OkHttpPacker   File: Util.java    License: 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 15
Source Project: BaseProject   File: HttpUtils.java    License: 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 16
Source Project: ModTheSpire   File: MIMETypeUtil.java    License: 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 17
Source Project: YImagePicker   File: PBitmapUtils.java    License: 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 Project: qaf   File: FileUtil.java    License: 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 19
Source Project: scipio-erp   File: UtilHttp.java    License: 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 20
Source Project: utils   File: FileUtil.java    License: 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);
}