Java URL Read getFileContentFromWeb(final URL srcUrl, final FileOutputStream destination)

Here you can find the source of getFileContentFromWeb(final URL srcUrl, final FileOutputStream destination)

Description

get File Content From Web

License

Open Source License

Parameter

Parameter Description
srcUrl URL
destination FileOutputStream

Exception

Parameter Description
IOException an exception

Return

String

Declaration

public static Boolean getFileContentFromWeb(final URL srcUrl, final FileOutputStream destination)
        throws IOException 

Method Source Code

//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;
    }
}

Related

  1. getBodyResponse(HttpURLConnection con)
  2. getBytes(URL url)
  3. getBytes(URL url)
  4. getFileSize(String sURL)
  5. getFileSize(URL url)
  6. getFilesizeFromUrl(String urlString)
  7. getFromURL(String URL)