java实现http下载

public void saveToFile(String destUrl, String fileName) throws IOException {
	        FileOutputStream fos = null;
	        BufferedInputStream bis = null;
	        HttpURLConnection httpUrl = null;
	        URL url = null;
	        byte[] buf = new byte[1024];
	        int size = 0;
	        
	        url = new URL(destUrl);
	        httpUrl = (HttpURLConnection) url.openConnection();
	        httpUrl.connect();
	        bis = new BufferedInputStream(httpUrl.getInputStream());
	        fos = new FileOutputStream(fileName);
	        while ((size = bis.read(buf)) != -1)
	            fos.write(buf, 0, size);
	        fos.close();
	        bis.close();
	        httpUrl.disconnect();
	    }


saveToFile("http://10.1.0.214:8080/SameTimeApp/data/sa.dat", "d:\\sa.dat");

你可能感兴趣的:(java)