Java HTTP Request saveHttpImage(String requestUrl, String requestMethod, String outputStr, File target)

Here you can find the source of saveHttpImage(String requestUrl, String requestMethod, String outputStr, File target)

Description

save Http Image

License

Apache License

Declaration

public static void saveHttpImage(String requestUrl,
            String requestMethod, String outputStr, File target) 

Method Source Code

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

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    public static void saveHttpImage(String requestUrl,
            String requestMethod, String outputStr, File target) {
        try {//from  w w w. j  a v a2s. co m
            URL url = new URL(requestUrl);

            HttpURLConnection conn = (HttpURLConnection) url
                    .openConnection();

            conn.setRequestMethod(requestMethod);

            conn.setConnectTimeout(5000);

            InputStream inStream = conn.getInputStream();

            byte[] data = readInputStream(inStream);

            FileOutputStream outStream = new FileOutputStream(target);

            outStream.write(data);

            outStream.close();
        } catch (Exception e) {
        }
    }

    public static byte[] readInputStream(InputStream inStream)
            throws Exception {
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while ((len = inStream.read(buffer)) != -1) {
            outstream.write(buffer, 0, len);
        }
        outstream.close();
        inStream.close();

        return outstream.toByteArray();
    }
}

Related

  1. request(String httpUrl, Map httpArgMap)
  2. request(String url, int timeout, String method, Map header)
  3. request(String url, Map cookies, Map parameters)
  4. requestData(String url)
  5. requestDataFromUrl(URL url, byte[] tosend, String userAgent)
  6. sendGet(String url)
  7. sendGet(String url, String param)
  8. sendGetRequest(String requestURL)
  9. sendGetRequest(String url, String cookies)