1. 程式人生 > >Java程式碼可自動從網路上下載檔案

Java程式碼可自動從網路上下載檔案

import java.io.BufferedInputStream;

import java.io.FileWriter; import java.net.URL; public class DownloadFromNetAddr { public static void main(String[] args) throws Exception{ boolean istag = download("http://NetAddress", "D:/111.del"); System.out.println(istag); } public static boolean download(String urlpath, String savepath){ try{ URL url = new URL(urlpath); BufferedInputStream in = new BufferedInputStream(url.openStream()); int line1; StringBuffer sb1 = new StringBuffer(); while((line1=in.read())!=-1){ sb1.append((char)line1); } String str1 = sb1.toString(); FileWriter fw = new FileWriter(savepath); fw.write(str1); fw.close(); }catch(Exception e){ return false; } return true; } }