Java HTTP Post writeFileFromUrl(URL sourceUrl, File targetFile)

Here you can find the source of writeFileFromUrl(URL sourceUrl, File targetFile)

Description

write File From Url

License

Open Source License

Declaration

public static void writeFileFromUrl(URL sourceUrl, File targetFile) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static void writeFileFromUrl(URL sourceUrl, File targetFile) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) sourceUrl.openConnection();
        connection.setRequestMethod("GET");
        InputStream in = connection.getInputStream();
        FileOutputStream out = new FileOutputStream(targetFile);
        FileCopy(in, out, 1024);//from   w  w  w. j  a  v a 2 s  .  c  om
        out.close();
    }

    public static void FileCopy(InputStream input, OutputStream output, int bufferSize) throws IOException {
        byte[] buf = new byte[bufferSize];
        int n = input.read(buf);
        while (n >= 0) {
            output.write(buf, 0, n);
            n = input.read(buf);
        }
        output.flush();
    }
}

Related

  1. sendPostRequest(Reader data, URL endpoint, Writer output)
  2. sendPostRequest(String requestURL, Map params)
  3. URLPost(String strUrl, Map map)
  4. writeBody(HttpURLConnection connection, String body)
  5. writeContent(HttpURLConnection urlConn, String content)
  6. writeString(HttpURLConnection conn, String content, String charsetName)
  7. writeToConnection(final HttpURLConnection couchdbConnection, final String content)