private static void test2() {
// 创建URL 对象
URL url = null;
try {
long l1 = System.currentTimeMillis();
// url = new URL(
// "http://localhost:8080/VerifyInfoService//aaa.txt");
url = new URL(
"http://ica-public.itrus.com.cn/cgi-bin/itruscrl.pl?CA=7D230723785662BF9D2E7A5542E70BCB");
// 获取 httpUrl连接
HttpURLConnection httpUrlConnection = (HttpURLConnection) url
.openConnection();
// 设置参数
// httpUrlConnection.setRequestProperty("Accept-Encoding",
// "identity");
// 设置连接时限
httpUrlConnection.setConnectTimeout(5 * 1000);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setRequestMethod("GET");
httpUrlConnection.setRequestProperty("Charsert", "UTF-8");
httpUrlConnection.connect();
// 设置输入流
InputStream fis = httpUrlConnection.getInputStream();
// int i = httpUrlConnection.getContentLength();
int i = 1024;
byte[] b = new byte[i];
// fis.read(b);
int j;
FileOutputStream fos = new FileOutputStream(new File(
"D://a//RSA10242.crl"));
byte[] temp;
while ((j = fis.read(b)) != -1) {
// 重点注意这样的写法
fos.write(b, 0, j);
fos.flush();
}
fis.close();
fos.close();
httpUrlConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}原文:http://yjm199.blog.51cto.com/4408395/1731755