java中io流下载文件实例
public static void saveUrlAs(String Url, File fileName){//此方法只能用HTTP协议
//保存文件到本地http://www.cdtarena.com/qrs
//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();
}
}
}
成都电脑培训找成都IT培训机构成都达内是高端C++培训、php培训等10年品牌。成都c++培训咨询:http://www.cdtarena.com
----------------解决方案--------------------------------------------------------