public static void getNetFile(String url, String fileName) {
try{
InputStream i = new java.net.URL(url).openStream();
OutputStream o = new FileOutputStream(fileName);
byte[] buf = new byte[2000];
int len;
int count = 0;
while((len = i.read(buf)) != -1) {
count +=len;
System.out.println(count);
o.write(buf,0,len);
} i.close();o.close();
}catch(Exception e){ e.printStackTrace();}
}
try{
InputStream i = new java.net.URL(url).openStream();
OutputStream o = new FileOutputStream(fileName);
byte[] buf = new byte[2000];
int len;
int count = 0;
while((len = i.read(buf)) != -1) {
count +=len;
System.out.println(count);
o.write(buf,0,len);
} i.close();o.close();
}catch(Exception e){ e.printStackTrace();}
}
而且你的InputStream和OutputStream没有必要都要!
(cookie和session已知)比如:http://localhost:8080/download.jsp?name=aaa
其中对session的处理:
String cookieVal = uc0.getHeaderField("Set-Cookie");
if(cookieVal != null)
{
sessionId = cookieVal.substring(0, cookieVal.indexOf(";"));//将cookie记下来
}之后:uc1.setRequestProperty("Cookie", sessionId);