Java tutorial
/* Copyright DR.YangLong Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.webarch.common.net.http; import com.alibaba.fastjson.JSONObject; import com.webarch.common.lang.StringSeriesTools; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import java.io.*; import java.net.ConnectException; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * package:common.net.http * functional describe:??????postgethttp * <code>String doHttp*</code> * * @author DR.YangLong [410357434@163.com] * @version 1.0 2015/5/13 10:09 */ public class HttpService { protected static Logger logger = LoggerFactory.getLogger(HttpService.class); private static final String DEFAULT_CHARSET = "UTF-8"; /** * ?Http * * @param requestUrl ? * @param requestMethod ? * @param outputJson ? * @return */ public static String doHttpRequest(String requestUrl, String requestMethod, String outputJson) { String result = null; try { StringBuffer buffer = new StringBuffer(); URL url = new URL(requestUrl); HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection(); httpUrlConn.setDoOutput(true); httpUrlConn.setDoInput(true); httpUrlConn.setUseCaches(false); httpUrlConn.setUseCaches(false); httpUrlConn.setRequestProperty("Accept-Charset", DEFAULT_CHARSET); httpUrlConn.setRequestProperty("Content-Type", "application/json;charset=" + DEFAULT_CHARSET); // ?GET/POST httpUrlConn.setRequestMethod(requestMethod); if ("GET".equalsIgnoreCase(requestMethod)) httpUrlConn.connect(); // ???? if (null != outputJson) { OutputStream outputStream = httpUrlConn.getOutputStream(); //?? outputStream.write(outputJson.getBytes(DEFAULT_CHARSET)); outputStream.close(); } // ??? InputStream inputStream = httpUrlConn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, DEFAULT_CHARSET); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } result = buffer.toString(); bufferedReader.close(); inputStreamReader.close(); // ? inputStream.close(); httpUrlConn.disconnect(); } catch (ConnectException ce) { logger.error("server connection timed out.", ce); } catch (Exception e) { logger.error("http request error:", e); } finally { return result; } } /** * ?Https * * @param requestUrl ? * @param requestMethod ? * @param trustManagers ?? * @param outputJson ? * @return */ public static String doHttpsRequest(String requestUrl, String requestMethod, TrustManager[] trustManagers, String outputJson) { String result = null; try { StringBuffer buffer = new StringBuffer(); // SSLContext?? SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, trustManagers, new java.security.SecureRandom()); // SSLContextSSLSocketFactory SSLSocketFactory ssf = sslContext.getSocketFactory(); URL url = new URL(requestUrl); HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection(); httpUrlConn.setSSLSocketFactory(ssf); httpUrlConn.setDoOutput(true); httpUrlConn.setDoInput(true); httpUrlConn.setUseCaches(false); httpUrlConn.setUseCaches(false); httpUrlConn.setRequestProperty("Accept-Charset", DEFAULT_CHARSET); httpUrlConn.setRequestProperty("Content-Type", "application/json;charset=" + DEFAULT_CHARSET); // ?GET/POST httpUrlConn.setRequestMethod(requestMethod); if ("GET".equalsIgnoreCase(requestMethod)) httpUrlConn.connect(); // ???? if (null != outputJson) { OutputStream outputStream = httpUrlConn.getOutputStream(); //?? outputStream.write(outputJson.getBytes(DEFAULT_CHARSET)); outputStream.close(); } // ??? InputStream inputStream = httpUrlConn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, DEFAULT_CHARSET); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } result = buffer.toString(); bufferedReader.close(); inputStreamReader.close(); // ? inputStream.close(); httpUrlConn.disconnect(); } catch (ConnectException ce) { logger.error("Weixin server connection timed out.", ce); } catch (Exception e) { logger.error("https request error:", e); } finally { return result; } } /** * ?http?json{@link #doHttpRequest} * * @param requestUrl * @param requestMethod * @param outputJson * @return */ public static JSONObject doHttpRequestGetJson(String requestUrl, String requestMethod, String outputJson) { JSONObject object = null; String string = doHttpRequest(requestUrl, requestMethod, outputJson); if (string != null) { object = JSONObject.parseObject(string); } return object; } /** * ?https?json{@link #doHttpsRequest} * * @param requestUrl * @param requestMethod * @param trustManagers * @param outputJson * @return */ public static JSONObject doHttpsRequestGetJson(String requestUrl, String requestMethod, TrustManager[] trustManagers, String outputJson) { JSONObject object = null; String string = doHttpsRequest(requestUrl, requestMethod, trustManagers, outputJson); if (string != null) { object = JSONObject.parseObject(string); } return object; } /** * ?url * * @param inputStream ? * @param fileName ?? * @param fileType * @param contentType * @param identity * @return */ public static String uploadMediaFile(String requestUrl, FileInputStream inputStream, String fileName, String fileType, String contentType, String identity) { String lineEnd = System.getProperty("line.separator"); String twoHyphens = "--"; String boundary = "*****"; String result = null; try { //? URL submit = new URL(requestUrl); HttpURLConnection conn = (HttpURLConnection) submit.openConnection(); // conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); //? conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Charset", DEFAULT_CHARSET); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); //?? DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + fileName + ";Content-Type=\"" + contentType + lineEnd); dos.writeBytes(lineEnd); byte[] buffer = new byte[8192]; // 8k int count = 0; while ((count = inputStream.read(buffer)) != -1) { dos.write(buffer, 0, count); } inputStream.close(); //? dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); dos.flush(); InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is, DEFAULT_CHARSET); BufferedReader br = new BufferedReader(isr); result = br.readLine(); dos.close(); is.close(); } catch (IOException e) { logger.error("", e); } return result; } /** * * * @param media_id ?ID * @param identity * @param filepath ?(????) * @return ?(???)error */ public static String downLoadMediaFile(String requestUrl, String media_id, String identity, String filepath) { String mediaLocalURL = "error"; InputStream inputStream = null; FileOutputStream fileOutputStream = null; DataOutputStream dataOutputStream = null; try { URL downLoadURL = new URL(requestUrl); // URL HttpURLConnection connection = (HttpURLConnection) downLoadURL.openConnection(); //? connection.setRequestMethod("GET"); // connection.connect(); //?,?text,json if (connection.getContentType().equalsIgnoreCase("text/plain")) { // BufferedReader???URL? inputStream = connection.getInputStream(); BufferedReader read = new BufferedReader(new InputStreamReader(inputStream, DEFAULT_CHARSET)); String valueString = null; StringBuffer bufferRes = new StringBuffer(); while ((valueString = read.readLine()) != null) { bufferRes.append(valueString); } inputStream.close(); String errMsg = bufferRes.toString(); JSONObject jsonObject = JSONObject.parseObject(errMsg); logger.error("???" + (jsonObject.getInteger("errcode"))); mediaLocalURL = "error"; } else { BufferedInputStream bis = new BufferedInputStream(connection.getInputStream()); String ds = connection.getHeaderField("Content-disposition"); //?? String fullName = ds.substring(ds.indexOf("filename=\"") + 10, ds.length() - 1); //?--?? String preffix = fullName.substring(0, fullName.lastIndexOf(".")); //? String suffix = fullName.substring(preffix.length() + 1); // String length = connection.getHeaderField("Content-Length"); // String type = connection.getHeaderField("Content-Type"); // byte[] buffer = new byte[8192]; // 8k int count = 0; mediaLocalURL = filepath + File.separator; File file = new File(mediaLocalURL); if (!file.exists()) { file.mkdirs(); } File mediaFile = new File(mediaLocalURL, fullName); fileOutputStream = new FileOutputStream(mediaFile); dataOutputStream = new DataOutputStream(fileOutputStream); while ((count = bis.read(buffer)) != -1) { dataOutputStream.write(buffer, 0, count); } //? mediaLocalURL += fullName; bis.close(); dataOutputStream.close(); fileOutputStream.close(); } } catch (IOException e) { logger.error("?", e); } return mediaLocalURL; } /** * ? * * @param protocol ??http?https?ftp?file?jar * @param host ? * @param file * @return */ public static String getRemoteFile(String protocol, String host, String file) { String content = null; try { URL url = new URL(protocol, host, file); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); DataInputStream input = new DataInputStream(conn.getInputStream()); byte[] buffer = new byte[8192]; int count = input.read(buffer); if (count > 0) { byte[] strBuffer = new byte[count]; System.arraycopy(buffer, 0, strBuffer, 0, count); content = new String(strBuffer, "UTF-8"); } input.close(); } catch (Exception e) { e.printStackTrace(); } //? return StringSeriesTools.replaceAllMatch(content, "\\s*|\t|\r|\n", ""); } /** * apache httpclient?post * @param url ? * @param paramsMap ?map * @return ? */ public static String doHttpClientPost(String url, Map<String, String> paramsMap) { CloseableHttpClient client = HttpClients.createDefault(); String responseText = ""; CloseableHttpResponse response = null; try { HttpPost method = new HttpPost(url); if (paramsMap != null) { List<NameValuePair> paramList = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> param : paramsMap.entrySet()) { NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue()); paramList.add(pair); } method.setEntity(new UrlEncodedFormEntity(paramList, DEFAULT_CHARSET)); } response = client.execute(method); HttpEntity entity = response.getEntity(); if (entity != null) { responseText = EntityUtils.toString(entity); } } catch (Exception e) { logger.error("?HTTP?", e); } finally { try { response.close(); } catch (Exception e) { logger.error("HTTPResponse?", e); } } return responseText; } /** * * @param url ? * @param localPath ? * @param fileName ?? * @return null/??? */ public static String downloadFile(String url, String localPath, String fileName) { int bufferSize = 1024 * 10; String file = null; if (!localPath.endsWith(File.separator)) { localPath += File.separator; } CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; InputStream inputStream = null; FileOutputStream outputStream = null; try { HttpGet httpGet = new HttpGet(url); response = httpclient.execute(httpGet); String fileType = "dat"; Header header = response.getLastHeader("Content-Type"); if (header != null) { String headerValue = header.getValue(); fileType = headerValue.substring(headerValue.indexOf("/") + 1, headerValue.length()); } HttpEntity entity = response.getEntity(); long length = entity.getContentLength(); if (length <= 0) { logger.warn("???"); return file; } inputStream = entity.getContent(); file = fileName + "." + fileType; File outFile = new File(localPath); if (!outFile.exists()) { outFile.mkdirs(); } localPath += file; outFile = new File(localPath); if (!outFile.exists()) { outFile.createNewFile(); } outputStream = new FileOutputStream(outFile); byte[] buffer = new byte[bufferSize]; int len; while ((len = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } inputStream.close(); outputStream.close(); return file; } catch (IOException e) { logger.error("?", e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { logger.error("?", e); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { logger.error("??", e); } } } return file; } public static void main(String args[]) { String name = downloadFile("http://qzapp.qlogo.cn/qzapp/100358052/0615D7F0B314E0653D077684EDD793C8/100", "d:\\group", ""); System.out.print(name); } }