import java.io.BufferedInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;
public class DownFile { final int BUFFER_SIZE=1024; public void saveToFile(String downUrl,String filePath) { HttpURLConnection connect=null; BufferedInputStream in=null; FileOutputStream file=null; byte[] buf=new byte[BUFFER_SIZE]; int size=0; try { URL url=new URL(downUrl); connect=(HttpURLConnection) url.openConnection(); connect.connect(); in=new BufferedInputStream(connect.getInputStream());
file=new FileOutputStream(filePath);
while((size=in.read(buf))!=-1) { file.write(buf,0,size); } } catch (MalformedURLException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally { try { file.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } connect.disconnect(); } } public static void main(String[]args){ DownFile d= new DownFile(); d.saveToFile(" } }
|