Here you can find the source of saveHttpImage(String requestUrl, String requestMethod, String outputStr, File target)
public static void saveHttpImage(String requestUrl, String requestMethod, String outputStr, File target)
//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(); } }