Java HTTP Read readHttpFile(String fileUrl, String referer)

Here you can find the source of readHttpFile(String fileUrl, String referer)

Description

read Http File

License

Open Source License

Declaration

public static byte[] readHttpFile(String fileUrl, String referer) throws IOException 

Method Source Code


//package com.java2s;
import java.io.ByteArrayOutputStream;

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

public class Main {
    private final static int FILE_READ_BUFFER_SIZE = 4096;

    public static byte[] readHttpFile(String fileUrl, String referer) throws IOException {
        InputStream inputStream = null;
        ByteArrayOutputStream fileContent = null;

        try {/*  w ww  . j  a v  a 2s.c  o m*/
            URL url = new URL(fileUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            if (referer != null) {
                connection.setRequestProperty("Referer", referer);
            }
            inputStream = connection.getInputStream();
            fileContent = new ByteArrayOutputStream();
            byte[] buffer = new byte[FILE_READ_BUFFER_SIZE];
            int n = 0;
            while (-1 != (n = inputStream.read(buffer))) {
                fileContent.write(buffer, 0, n);
            }
        } finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (fileContent != null) {
                fileContent.close();
            }
        }
        return fileContent.toByteArray();
    }
}

Related

  1. readData(HttpURLConnection conn)
  2. readError(HttpURLConnection conn)
  3. readGetEx(String url)
  4. readHTTPConnection(HttpURLConnection conn)
  5. readHttpConnection(HttpURLConnection h)
  6. readRaw(URL url)