HttpURLConnection响应head内容的编码方式,如何正确识别,100分在线等
本帖最后由 JavaLover00000 于 2013-11-04 12:43:34 编辑 我用HttpUrlConnection 连接下载一个mp3
地址如下:
http://zhangmenshiting.baidu.com/data2/music/91731224/91731224.mp3?xcode=35364211a9bc8d289ad59150fa1ad1eb5dc4eabdf6e0aebb
返回的 response head中可以读取到文件名字
如下:
Content-Disposition:attachment; filename="2??μμ?.mp3"
现在我的需求是,正确识别这个filename的编码方式
这是个汉字名字,我用 new String(str.getBytes("iso-8859-1"),"gbk");
能正确的到名字,但是我希望能不是由我代码写死的来转成gbk,而是有代码或者其他方式动态识别出来
注:用浏览器访问这个地址,是可以获得正确的文件名的,指定浏览器编码方式并不会导致乱码,希望有经验的或者做过浏览器的指点指点,浏览器是如何做到的
下面是完整测试代码,拷贝即可运行:
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
public class T1 {
public static void main(String[] args) throws UnsupportedEncodingException, Throwable {
String downloadUrl = "http://zhangmenshiting.baidu.com/data2/music/91731224/91731224.mp3?xcode=35364211a9bc8d289ad59150fa1ad1eb5dc4eabdf6e0aebb";
URL url = new URL(downloadUrl);
// 打开HttpURLConnection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置 HttpURLConnection的断开时间
conn.setConnectTimeout(5000);
// 设置 HttpURLConnection的请求方式
conn.setRequestMethod("GET");
// 设置 HttpURLConnection的接收的文件类型
conn.setRequestProperty(
"Accept",
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, "
+ "application/x-shockwave-flash, application/xaml+xml, "
+ "application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, "
+ "application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
// 设置 HttpURLConnection的接收语音
conn.setRequestProperty("Accept-Language", Locale.getDefault().toString());
// 指定请求uri的源资源地址
conn.setRequestProperty("Referer", downloadUrl);
// 设置 HttpURLConnection的字符编码
conn.setRequestProperty("Accept-Charset", "UTF-8");
// 检查浏览页面的访问者在用什么操作系统(包括版本号)浏览器(包括版本号)和用户个人偏好
// conn.setRequestProperty(
// "User-Agent",
// "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2;"
// + " Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; "
// + ".NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152;"
// + " .NET CLR 3.5.30729)");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.connect();
printResponseHeader(conn);
}
private static void printResponseHeader(HttpURLConnection http) throws UnsupportedEncodingException {
Map<String, String> header = getHttpResponseHeader(http);
for (Map.Entry<String, String> entry : header.entrySet()) {
String key = entry.getKey() != null ? entry.getKey() + ":" : "";
System.out.println(key + entry.getValue());
}
}
private static Map<String, String> getHttpResponseHeader(
HttpURLConnection http) throws UnsupportedEncodingException {
Map<String, String> header = new LinkedHashMap<String, String>();
for (int i = 0;; i++) {
String mine = http.getHeaderField(i);
if (mine == null)
break;
header.put(http.getHeaderFieldKey(i), mine);
}
return header;
}
}
HTTP/1.1 200 OK
Server:JSP2/1.0.16
Date:Mon, 04 Nov 2013 03:06:26 GMT
Content-Type:audio/mpeg
Connection:close
Content-Length:2518244
Accept-Ranges:bytes
Last-Modified:Wed, 23 Oct 2013 02:56:23 GMT
Expires:Fri, 22 Nov 2013 12:20:18 GMT
x-bs-version:9AB3778C345E22493B1B8D3D1E7D92C1
ETag:f05c8c3e7e7328b5ec3913051c256170
x-bs-request-id:MTAuNDYuMjI4LjQ0OjgwODA6Mjk3MDIxMTc1NToyMy9PY3QvMjAxMyAyMDoyMDoxOCA=
Content-Disposition:attachment; filename="2??μμ?.mp3"
x-bs-meta-crc32:657750654
Content-MD5:f05c8c3e7e7328b5ec3913051c256170
x-bs-client-ip:NTguMjE1LjEyMy45Mg==
x-bs-uncopyable:enable
Cache-Control:max-age=2592000
private static String getEncode(InputStream inputStream){
String code = "gb2312";
try {
byte[] head = new byte[3];
inputStream.read(head);
if (head[0] == -17 && head[1] == -69 && head[2] == -65)
code = "UTF-8";
if (head[0] == -1 && head[1] == -2)
code = "UTF-16";
if (head[0] == -2 && head[1] == -1)
code = "Unicode";
inputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return code;
}