Here you can find the source of getCharsetFromContentType(String contentType)
public static String getCharsetFromContentType(String contentType)
//package com.java2s; //License from project: Apache License import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { private static final Pattern charsetPattern = Pattern.compile("(?i)\\bcharset=\\s*(?:\"|')?([^\\s,;\"']*)"); public static String getCharsetFromContentType(String contentType) { if (contentType == null) { return null; }//from w ww .j a v a 2 s.c o m Matcher m = charsetPattern.matcher(contentType); if (m.find()) { String charset = m.group(1).trim(); charset = charset.replace("charset=", ""); if (charset.isEmpty()) return null; try { if (Charset.isSupported(charset)) return charset; charset = charset.toUpperCase(Locale.ENGLISH); if (Charset.isSupported(charset)) return charset; } catch (IllegalCharsetNameException e) { // if our advanced charset matching fails.... we just take the // default return null; } } return null; } }