Java URL Download downloadData(String url, String params)

Here you can find the source of downloadData(String url, String params)

Description

download Data

License

Apache License

Declaration

public static byte[] downloadData(String url, String params) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static byte[] downloadData(String url, String params) throws IOException {
        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);/*from   w  w  w  . j  a v a 2s  .c om*/
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        OutputStream output = connection.getOutputStream();
        output.write(params.getBytes());
        output.close();
        int code = connection.getResponseCode();
        if (code == 200) {
            InputStream in = connection.getInputStream();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            return out.toByteArray();
        }
        throw new IOException("error");
    }
}

Related

  1. downloadAndExtract(String fileURL, String targetDirectory)
  2. downloadAndSaveImage(URL imageUrl, String path)
  3. downloadAndUnzip(String url, File location)
  4. downloadAsString(String url)
  5. downloadBinary(URL BaseURL, String Name, File TargetDirectory)
  6. downloadDirectory(URL dirUrl, File destDir)
  7. downloadFile(File parent, String prefix, String suffix, URL url)
  8. downloadFile(File target, String urlStr)
  9. downloadFile(final String url)