Java Code Examples for java.net.FileNameMap#getContentTypeFor()

The following examples show how to use java.net.FileNameMap#getContentTypeFor() . 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: 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 2
Source File: AbstractFileTypeDetector.java    From Bytecoder with Apache License 2.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 3
Source File: FileURLConnection.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void initializeHeaders() {
    try {
        connect();
        exists = file.exists();
    } catch (IOException e) {
    }
    if (!initializedHeaders || !exists) {
        length = file.length();
        lastModified = file.lastModified();

        if (!isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();
            contentType = map.getContentTypeFor(filename);
            if (contentType != null) {
                properties.add(CONTENT_TYPE, contentType);
            }
            properties.add(CONTENT_LENGTH, String.valueOf(length));

            /*
             * Format the last-modified field into the preferred
             * Internet standard - ie: fixed-length subset of that
             * defined by RFC 1123
             */
            if (lastModified != 0) {
                Date date = new Date(lastModified);
                SimpleDateFormat fo =
                    new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
                fo.setTimeZone(TimeZone.getTimeZone("GMT"));
                properties.add(LAST_MODIFIED, fo.format(date));
            }
        } else {
            properties.add(CONTENT_TYPE, TEXT_PLAIN);
        }
        initializedHeaders = true;
    }
}
 
Example 4
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 5
Source File: PostFormRequest.java    From AndroidStudyDemo with GNU General Public License v2.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 File: FileURLConnection.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private void initializeHeaders() {
    try {
        connect();
        exists = file.exists();
    } catch (IOException e) {
    }
    if (!initializedHeaders || !exists) {
        length = file.length();
        lastModified = file.lastModified();

        if (!isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();
            contentType = map.getContentTypeFor(filename);
            if (contentType != null) {
                properties.add(CONTENT_TYPE, contentType);
            }
            properties.add(CONTENT_LENGTH, String.valueOf(length));

            /*
             * Format the last-modified field into the preferred
             * Internet standard - ie: fixed-length subset of that
             * defined by RFC 1123
             */
            if (lastModified != 0) {
                Date date = new Date(lastModified);
                SimpleDateFormat fo =
                    new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
                fo.setTimeZone(TimeZone.getTimeZone("GMT"));
                properties.add(LAST_MODIFIED, fo.format(date));
            }
        } else {
            properties.add(CONTENT_TYPE, TEXT_PLAIN);
        }
        initializedHeaders = true;
    }
}
 
Example 7
Source File: FileURLConnection.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void initializeHeaders() {
    try {
        connect();
        exists = file.exists();
    } catch (IOException e) {
    }
    if (!initializedHeaders || !exists) {
        length = file.length();
        lastModified = file.lastModified();

        if (!isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();
            contentType = map.getContentTypeFor(filename);
            if (contentType != null) {
                properties.add(CONTENT_TYPE, contentType);
            }
            properties.add(CONTENT_LENGTH, String.valueOf(length));

            /*
             * Format the last-modified field into the preferred
             * Internet standard - ie: fixed-length subset of that
             * defined by RFC 1123
             */
            if (lastModified != 0) {
                Date date = new Date(lastModified);
                SimpleDateFormat fo =
                    new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
                fo.setTimeZone(TimeZone.getTimeZone("GMT"));
                properties.add(LAST_MODIFIED, fo.format(date));
            }
        } else {
            properties.add(CONTENT_TYPE, TEXT_PLAIN);
        }
        initializedHeaders = true;
    }
}
 
Example 8
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 9
Source File: FileURLConnection.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void initializeHeaders() {
    try {
        connect();
        exists = file.exists();
    } catch (IOException e) {
    }
    if (!initializedHeaders || !exists) {
        length = file.length();
        lastModified = file.lastModified();

        if (!isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();
            contentType = map.getContentTypeFor(filename);
            if (contentType != null) {
                properties.add(CONTENT_TYPE, contentType);
            }
            properties.add(CONTENT_LENGTH, String.valueOf(length));

            /*
             * Format the last-modified field into the preferred
             * Internet standard - ie: fixed-length subset of that
             * defined by RFC 1123
             */
            if (lastModified != 0) {
                Date date = new Date(lastModified);
                SimpleDateFormat fo =
                    new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
                fo.setTimeZone(TimeZone.getTimeZone("GMT"));
                properties.add(LAST_MODIFIED, fo.format(date));
            }
        } else {
            properties.add(CONTENT_TYPE, TEXT_PLAIN);
        }
        initializedHeaders = true;
    }
}
 
Example 10
Source File: FileURLConnection.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private void initializeHeaders() {
    try {
        connect();
        exists = file.exists();
    } catch (IOException e) {
    }
    if (!initializedHeaders || !exists) {
        length = file.length();
        lastModified = file.lastModified();

        if (!isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();
            contentType = map.getContentTypeFor(filename);
            if (contentType != null) {
                properties.add(CONTENT_TYPE, contentType);
            }
            properties.add(CONTENT_LENGTH, String.valueOf(length));

            /*
             * Format the last-modified field into the preferred
             * Internet standard - ie: fixed-length subset of that
             * defined by RFC 1123
             */
            if (lastModified != 0) {
                Date date = new Date(lastModified);
                SimpleDateFormat fo =
                    new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
                fo.setTimeZone(TimeZone.getTimeZone("GMT"));
                properties.add(LAST_MODIFIED, fo.format(date));
            }
        } else {
            properties.add(CONTENT_TYPE, TEXT_PLAIN);
        }
        initializedHeaders = true;
    }
}
 
Example 11
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 12
Source File: PostFormRequest.java    From styT with Apache License 2.0 5 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 13
Source File: HttpUtils.java    From DoraemonKit 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 14
Source File: FileURLConnection.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private void initializeHeaders() {
    try {
        connect();
        exists = file.exists();
    } catch (IOException e) {
    }
    if (!initializedHeaders || !exists) {
        length = file.length();
        lastModified = file.lastModified();

        if (!isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();
            contentType = map.getContentTypeFor(filename);
            if (contentType != null) {
                properties.add(CONTENT_TYPE, contentType);
            }
            properties.add(CONTENT_LENGTH, String.valueOf(length));

            /*
             * Format the last-modified field into the preferred
             * Internet standard - ie: fixed-length subset of that
             * defined by RFC 1123
             */
            if (lastModified != 0) {
                Date date = new Date(lastModified);
                SimpleDateFormat fo =
                    new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US);
                fo.setTimeZone(TimeZone.getTimeZone("GMT"));
                properties.add(LAST_MODIFIED, fo.format(date));
            }
        } else {
            properties.add(CONTENT_TYPE, TEXT_PLAIN);
        }
        initializedHeaders = true;
    }
}
 
Example 15
Source File: UpdateBody.java    From EasyHttp 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 MEDIA_TYPE;
    }
    MediaType type = MediaType.parse(contentType);
    if (type == null) {
        type = MEDIA_TYPE;
    }
    return type;
}
 
Example 16
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 17
Source File: SFTPRemoteDownloadSourceDelegate.java    From datacollector with Apache License 2.0 4 votes vote down vote up
private String determineContentType(String remotePath) {
  // This is based on VFS's FileContentInfoFilenameFactory and just looks at the filename extension
  String name = Paths.get(remotePath).getFileName().toString();
  FileNameMap fileNameMap = URLConnection.getFileNameMap();
  return fileNameMap.getContentTypeFor(name);
}
 
Example 18
Source File: ImportProcess.java    From proarc with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Simplified version uses filename extension. For niftier alternatives see
 * http://www.rgagnon.com/javadetails/java-0487.html
 */
public static String findMimeType(File f) {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    return fileNameMap.getContentTypeFor(f.getName());
}
 
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: FileUtils.java    From VideoRecord with MIT License 4 votes vote down vote up
public static String getFileType(String fn, String defaultType) {
	FileNameMap fNameMap = URLConnection.getFileNameMap();
	String type = fNameMap.getContentTypeFor(fn);
	return type == null ? defaultType : type;
}