本文整理汇总了Java中java.net.URLConnection.getContentEncoding方法的典型用法代码示例。如果您正苦于以下问题:Java URLConnection.getContentEncoding方法的具体用法?Java URLConnection.getContentEncoding怎么用?Java URLConnection.getContentEncoding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.URLConnection
的用法示例。
在下文中一共展示了URLConnection.getContentEncoding方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: initialiseResponse
import java.net.URLConnection; //导入方法依赖的package包/类
/**
* Initialise response
*
* @param urlConnection url connection
*/
protected void initialiseResponse(URLConnection urlConnection)
{
String type = urlConnection.getContentType();
if (type != null)
{
int encodingIdx = type.lastIndexOf("charset=");
if (encodingIdx == -1)
{
String encoding = urlConnection.getContentEncoding();
if (encoding != null && encoding.length() > 0)
{
type += ";charset=" + encoding;
}
}
response.setContentType(type);
}
}
示例2: getInputStream
import java.net.URLConnection; //导入方法依赖的package包/类
private static TrackingInputStream getInputStream(URLConnection conn) throws IOException {
String encoding = conn.getContentEncoding();
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
return new TrackingGZIPInputStream(new ExposedGZIPInputStream(conn.getInputStream()));
}
return new SimpleTrackingInputStream(conn.getInputStream());
}
示例3: makeData
import java.net.URLConnection; //导入方法依赖的package包/类
@Override
protected Data makeData () throws Exception
{
final URLConnection connection = this.url.openConnection ();
connection.connect ();
final int len = connection.getContentLength ();
final ByteArrayOutputStream bos = new ByteArrayOutputStream ( len > 0 ? len : 0 );
final byte[] buffer = new byte[4096];
try ( InputStream stream = connection.getInputStream () )
{
int rc;
while ( ( rc = stream.read ( buffer ) ) > 0 )
{
bos.write ( buffer, 0, rc );
}
bos.close ();
}
final String encoding = connection.getContentEncoding ();
final String type = connection.getContentType ();
logger.debug ( "Content-Encoding: {}", encoding );
logger.debug ( "Content-Type: {}", type );
Charset charset = null;
if ( this.charset != null )
{
charset = this.charset;
}
else if ( this.probeCharset )
{
charset = makeCharsetFromType ( type );
}
return new UrlConnectionData ( convert ( buffer, charset ), null );
}
示例4: getInputStream
import java.net.URLConnection; //导入方法依赖的package包/类
private static TrackingInputStream getInputStream(URLConnection conn) throws IOException {
String encoding = conn.getContentEncoding();
if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
return new TrackingGZIPInputStream(new ExposedGZIPInputStream(conn.getInputStream()));
}
return new SimpleTrackingInputStream(conn.getInputStream());
}
示例5: getWebsiteReader
import java.net.URLConnection; //导入方法依赖的package包/类
public static BufferedReader getWebsiteReader(String link) throws IOException {
URLConnection conn = new URL(link).openConnection();
conn.setRequestProperty("User-Agent", "Mozilla");
String encoding = conn.getContentEncoding() == null ? "UTF-8" : conn.getContentEncoding();
return new BufferedReader(new InputStreamReader(conn.getInputStream(), encoding));
}
示例6: readFromConnection
import java.net.URLConnection; //导入方法依赖的package包/类
private static String readFromConnection(URLConnection connection) throws IOException {
InputStream stream;
if (connection instanceof HttpURLConnection) {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
try {
stream = connection.getInputStream();
} catch (Exception e) {
stream = httpConnection.getErrorStream();
}
} else {
stream = connection.getInputStream();
}
try {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
while (true) {
int read = stream.read(buffer);
if (read == -1) {
break;
}
output.write(buffer, 0, read);
}
String charset = connection.getContentEncoding();
if (charset == null) {
for (String part : connection.getContentType().split(";")) {
String part2 = part2.trim();
if (part2.startsWith("charset=")) {
charset = part2.substring("charset=".length());
break;
}
}
if (charset == null) {
charset = "UTF-8";
}
}
String str = new String(output.toByteArray(), charset);
return str;
} finally {
stream.close();
}
}