Here you can find the source of getFileContentFromWeb(final URL srcUrl, final FileOutputStream destination)
Parameter | Description |
---|---|
srcUrl | URL |
destination | FileOutputStream |
Parameter | Description |
---|---|
IOException | an exception |
public static Boolean getFileContentFromWeb(final URL srcUrl, final FileOutputStream destination) throws IOException
//package com.java2s; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class Main { /**/*from w ww . j ava 2 s . c o m*/ * * @param srcUrl * URL * @param destination * FileOutputStream * @return String * @throws IOException */ public static Boolean getFileContentFromWeb(final URL srcUrl, final FileOutputStream destination) throws IOException { if ((srcUrl != null)) { HttpURLConnection conn = (HttpURLConnection) srcUrl.openConnection(); conn.setRequestMethod("GET"); conn.connect(); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { byte tmp_buffer[] = new byte[4096]; InputStream is = conn.getInputStream(); int n; while ((n = is.read(tmp_buffer)) > 0) { destination.write(tmp_buffer, 0, n); destination.flush(); } } else { throw new IllegalStateException("HTTP response: " + responseCode); } destination.close(); return Boolean.TRUE; } return Boolean.FALSE; } }