java.net.FileNameMap Java Examples

The following examples show how to use java.net.FileNameMap. 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 enjoyshop 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: 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 #4
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 #5
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 #6
Source File: FileURLConnection.java    From openjdk-8-source 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: Util.java    From httplite with Apache License 2.0 5 votes vote down vote up
public static String guessMediaType(HttpLite lite,File file){
    if(file==null||lite==null){
        return null;
    }
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentType = fileNameMap.getContentTypeFor(file.getAbsolutePath());
    if(contentType==null){
        contentType = MediaType.APPLICATION_STREAM;
    }
    return contentType;
}
 
Example #8
Source File: UploadBuilder.java    From MyOkHttp 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 #9
Source File: HttpUtils.java    From okhttp-OkGo 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 #10
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 #11
Source File: FileURLConnection.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public synchronized InputStream getInputStream()
    throws IOException {

    int iconHeight;
    int iconWidth;

    connect();

    if (is == null) {
        if (isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();

            StringBuffer buf = new StringBuffer();

            if (files == null) {
                throw new FileNotFoundException(filename);
            }

            Collections.sort(files, Collator.getInstance());

            for (int i = 0 ; i < files.size() ; i++) {
                String fileName = files.get(i);
                buf.append(fileName);
                buf.append("\n");
            }
            // Put it into a (default) locale-specific byte-stream.
            is = new ByteArrayInputStream(buf.toString().getBytes());
        } else {
            throw new FileNotFoundException(filename);
        }
    }
    return is;
}
 
Example #12
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 #13
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 #14
Source File: FileURLConnection.java    From openjdk-8 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: FileURLConnection.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public synchronized InputStream getInputStream()
    throws IOException {

    int iconHeight;
    int iconWidth;

    connect();

    if (is == null) {
        if (isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();

            StringBuffer buf = new StringBuffer();

            if (files == null) {
                throw new FileNotFoundException(filename);
            }

            Collections.sort(files, Collator.getInstance());

            for (int i = 0 ; i < files.size() ; i++) {
                String fileName = files.get(i);
                buf.append(fileName);
                buf.append("\n");
            }
            // Put it into a (default) locale-specific byte-stream.
            is = new ByteArrayInputStream(buf.toString().getBytes());
        } else {
            throw new FileNotFoundException(filename);
        }
    }
    return is;
}
 
Example #16
Source File: FileURLConnection.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public synchronized InputStream getInputStream()
    throws IOException {

    int iconHeight;
    int iconWidth;

    connect();

    if (is == null) {
        if (isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();

            StringBuilder sb = new StringBuilder();

            if (files == null) {
                throw new FileNotFoundException(filename);
            }

            Collections.sort(files, Collator.getInstance());

            for (int i = 0 ; i < files.size() ; i++) {
                String fileName = files.get(i);
                sb.append(fileName);
                sb.append("\n");
            }
            // Put it into a (default) locale-specific byte-stream.
            is = new ByteArrayInputStream(sb.toString().getBytes());
        } else {
            throw new FileNotFoundException(filename);
        }
    }
    return is;
}
 
Example #17
Source File: PostFormRequest.java    From NewsMe 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 #18
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 #19
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 #20
Source File: FileURLConnection.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public synchronized InputStream getInputStream()
    throws IOException {

    int iconHeight;
    int iconWidth;

    connect();

    if (is == null) {
        if (isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();

            StringBuffer buf = new StringBuffer();

            if (files == null) {
                throw new FileNotFoundException(filename);
            }

            Collections.sort(files, Collator.getInstance());

            for (int i = 0 ; i < files.size() ; i++) {
                String fileName = files.get(i);
                buf.append(fileName);
                buf.append("\n");
            }
            // Put it into a (default) locale-specific byte-stream.
            is = new ByteArrayInputStream(buf.toString().getBytes());
        } else {
            throw new FileNotFoundException(filename);
        }
    }
    return is;
}
 
Example #21
Source File: FileURLConnection.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public synchronized InputStream getInputStream()
    throws IOException {

    int iconHeight;
    int iconWidth;

    connect();

    if (is == null) {
        if (isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();

            StringBuffer buf = new StringBuffer();

            if (files == null) {
                throw new FileNotFoundException(filename);
            }

            Collections.sort(files, Collator.getInstance());

            for (int i = 0 ; i < files.size() ; i++) {
                String fileName = files.get(i);
                buf.append(fileName);
                buf.append("\n");
            }
            // Put it into a (default) locale-specific byte-stream.
            is = new ByteArrayInputStream(buf.toString().getBytes());
        } else {
            throw new FileNotFoundException(filename);
        }
    }
    return is;
}
 
Example #22
Source File: FileURLConnection.java    From hottub 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 #23
Source File: MimeTypesUtils.java    From yes-cart with Apache License 2.0 5 votes vote down vote up
/**
 * Detect MIME type from file name.
 *
 * @param fileName file name
 *
 * @return MIME type
 */
public static String getMimeType(String fileName) {

    final FileNameMap mimeTypes = URLConnection.getFileNameMap();
    final String contentType = mimeTypes.getContentTypeFor(fileName);

    // nothing found -> look up our in extension map to find manually mapped types
    if (contentType == null) {
        final String extension = fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length());
        return EXT_MIMETYPE_MAP.get(extension);
    }
    return contentType;
}
 
Example #24
Source File: UploadFileRequest.java    From lunzi 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 #25
Source File: FileURLConnection.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public synchronized InputStream getInputStream()
    throws IOException {

    int iconHeight;
    int iconWidth;

    connect();

    if (is == null) {
        if (isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();

            StringBuffer buf = new StringBuffer();

            if (files == null) {
                throw new FileNotFoundException(filename);
            }

            Collections.sort(files, Collator.getInstance());

            for (int i = 0 ; i < files.size() ; i++) {
                String fileName = files.get(i);
                buf.append(fileName);
                buf.append("\n");
            }
            // Put it into a (default) locale-specific byte-stream.
            is = new ByteArrayInputStream(buf.toString().getBytes());
        } else {
            throw new FileNotFoundException(filename);
        }
    }
    return is;
}
 
Example #26
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 #27
Source File: FileURLConnection.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public synchronized InputStream getInputStream()
    throws IOException {

    int iconHeight;
    int iconWidth;

    connect();

    if (is == null) {
        if (isDirectory) {
            FileNameMap map = java.net.URLConnection.getFileNameMap();

            StringBuilder sb = new StringBuilder();

            if (files == null) {
                throw new FileNotFoundException(filename);
            }

            Collections.sort(files, Collator.getInstance());

            for (int i = 0 ; i < files.size() ; i++) {
                String fileName = files.get(i);
                sb.append(fileName);
                sb.append("\n");
            }
            // Put it into a (default) locale-specific byte-stream.
            is = new ByteArrayInputStream(sb.toString().getBytes());
        } else {
            throw new FileNotFoundException(filename);
        }
    }
    return is;
}
 
Example #28
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 #29
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 #30
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;
}