public static void saveUrlAs(String Url, File fileName){
//此方法只能用HTTP协议
//保存文件到本地
//Url是文件下载地址,fileName 为一个全名(路径+文件名)文件
URL url;
DataOutputStream out = null;
DataInputStream in = null;
try {
url = new URL(Url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
in = new DataInputStream(connection.getInputStream());
out = new DataOutputStream(new FileOutputStream(fileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(out != null){
out.close();
}
if(in != null){
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//此方法只能用HTTP协议
//保存文件到本地
//Url是文件下载地址,fileName 为一个全名(路径+文件名)文件
URL url;
DataOutputStream out = null;
DataInputStream in = null;
try {
url = new URL(Url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
in = new DataInputStream(connection.getInputStream());
out = new DataOutputStream(new FileOutputStream(fileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0) {
out.write(buffer, 0, count);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(out != null){
out.close();
}
if(in != null){
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}