URL url=new URL("http://86516.onlinedown.net/down/FlashFXP_3.3.8.1133_RC2_SC.exe");
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection ();
返回的文件长度是错误的!按理说不管重定向不重定向,HttpURLConnection是可以自动完成的,但是这次却不行了,这是怎回事呢?
HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection ();
返回的文件长度是错误的!按理说不管重定向不重定向,HttpURLConnection是可以自动完成的,但是这次却不行了,这是怎回事呢?
* 获取文件的长度信息
* @param address String 要获取的文件的地址
* @return int 文件的长度
*/
private int getFileLength(String address)
{
int length = 0;
try
{
// setProxy();
URL url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setReadTimeout(5000);
connection.setRequestProperty("User-Agent", "NetFox");
int code = connection.getResponseCode();
if (code == 401)
{
throw new Exception("Unauthorized!");
}
System.out.println("response code = " + code);
String header;
for (int i = 0;; i++)
{
header = connection.getHeaderFieldKey(i);
// System.out.println(header);
if (header != null && header.equalsIgnoreCase("Content-Length"))
{
length = Integer
.parseInt(connection.getHeaderField(header));
if (length == -1)
throw new Exception(
"The file's length isn't to be know!");
else if (length == -2)
throw new Exception("The file can't be acessed.");
break;
}
}
} catch (Exception e)
{
e.printStackTrace();
}
return length;
}