80,352
社区成员
发帖
与我相关
我的任务
分享
private int GetDownLoadFileSize() throws Exception{
HttpsURLConnection connection=null;
try{
SSLContext sslctxt = SSLContext.getInstance("SSL");
sslctxt.init(null, new TrustManager[]{new MyX509TrustManager()}, new java.security.SecureRandom());
if (sslctxt != null)
{
HttpsURLConnection.setDefaultSSLSocketFactory(sslctxt
.getSocketFactory());
}
HttpsURLConnection.setDefaultHostnameVerifier(new MyHostnameVerifier());
connection=(HttpsURLConnection) this._DownLoadUrl.openConnection();
connection.setInstanceFollowRedirects(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setRequestProperty("Cookie", StringUtil.vpnAuth.cookie);
connection.setRequestProperty("Content-Type","application/octet-stream");
connection.setRequestProperty("Connection", "Keep-Alive");
//connection.setRequestProperty("Accept-Encoding", "identity");
connection.connect();
}catch(Exception ex){
throw new Exception("打开url出错,详细信息:"+ex.getMessage());
}
//获取状态码,判断文件是否存在
if(connection.getResponseCode()!=200)
throw new Exception("要下载的文件不存在");
//返回文件的大小
return connection.getContentLength();
}