parent
3502c0c215
commit
6e3eacb6c9
@ -0,0 +1,248 @@
|
||||
package com.ann.utils;
|
||||
|
||||
import org.apache.http.ParseException;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
//import org.springframework.http.HttpEntity;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
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.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public class HttpUtils {
|
||||
|
||||
private static String URL_PATH = "http://10.6.2.88/api/report?siteID=1&OrderNumber=98539980";
|
||||
|
||||
public static final Logger logger = LogManager.getLogger(HttpUtils.class);
|
||||
/*private RequestConfig requestConfig = RequestConfig.custom()
|
||||
.setSocketTimeout(15000)
|
||||
.setConnectTimeout(15000)
|
||||
.setConnectionRequestTimeout(15000)
|
||||
.build();*/
|
||||
private static RequestConfig requestConfig = null;
|
||||
static {
|
||||
requestConfig = RequestConfig.custom()
|
||||
.setSocketTimeout(15000)
|
||||
.setConnectTimeout(15000)
|
||||
.setConnectionRequestTimeout(15000)
|
||||
.build();
|
||||
}
|
||||
private static HttpUtils instance = null;
|
||||
private HttpUtils(){}
|
||||
public static HttpUtils getInstance(){
|
||||
if (instance == null) {
|
||||
instance = new HttpUtils();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
/**
|
||||
* 发送 post请求
|
||||
* @param httpUrl 地址
|
||||
*/
|
||||
public static byte[] sendHttpPost(String httpUrl) {
|
||||
HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
|
||||
return sendHttpPost(httpPost);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送 post请求
|
||||
* @param httpUrl 地址
|
||||
* @param params 参数(格式:key1=value1&key2=value2)
|
||||
*/
|
||||
/*public byte[] sendHttpPost(String httpUrl, byte[] params) {
|
||||
HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
|
||||
try {
|
||||
//设置参数
|
||||
// StringEntity stringEntity = new StringEntity(params, "UTF-8");
|
||||
//HttpEntity byteEntity = new HttpEntity(params);
|
||||
ByteArrayEntity arrayEntity = new ByteArrayEntity(params);
|
||||
httpPost.setEntity(arrayEntity);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return sendHttpPost(httpPost);
|
||||
}*/
|
||||
/**
|
||||
* 发送 post请求
|
||||
* @param httpUrl 地址
|
||||
* @param maps 参数
|
||||
*/
|
||||
/*public byte[] sendHttpPost(String httpUrl, Map<String,String> maps) {
|
||||
HttpPost httpPost = new HttpPost(httpUrl);// 创建httpPost
|
||||
// 创建参数队列
|
||||
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
|
||||
for (String key : maps.keySet()) {
|
||||
nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
|
||||
}
|
||||
try {
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return sendHttpPost(httpPost);
|
||||
}*/
|
||||
/**
|
||||
* 发送Post请求
|
||||
* @param httpPost
|
||||
* @return
|
||||
*/
|
||||
private static byte[] sendHttpPost(HttpPost httpPost) {
|
||||
CloseableHttpClient httpClient = null;
|
||||
CloseableHttpResponse response = null;
|
||||
HttpEntity entity = null;
|
||||
byte[] responseContent = null;
|
||||
try {
|
||||
// 创建默认的httpClient实例.
|
||||
httpClient = HttpClients.createDefault();
|
||||
httpPost.setConfig(requestConfig);
|
||||
// 执行请求
|
||||
response = httpClient.execute(httpPost);
|
||||
entity = response.getEntity();
|
||||
responseContent = EntityUtils.toByteArray(entity);
|
||||
// responseContent = EntityUtils.toString(entity, "UTF-8"); //如果需要返回字符串改这里就行了
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
// 关闭连接,释放资源
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
if (httpClient != null) {
|
||||
httpClient.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return responseContent;
|
||||
}
|
||||
|
||||
/**
|
||||
* httpclient发送get请求
|
||||
*//*
|
||||
public static byte[] httpGet(String uri) {
|
||||
CloseableHttpClient httpclient = HttpClients.createDefault();
|
||||
byte[] fileBytes = null;
|
||||
try {
|
||||
// 创建httpclient get请求.
|
||||
HttpGet httpget = new HttpGet(uri);
|
||||
|
||||
System.out.println(" request for :" + httpget.getURI());
|
||||
// 执行get请求.
|
||||
CloseableHttpResponse response = httpclient.execute(httpget);
|
||||
try {
|
||||
//获取响应实体
|
||||
HttpEntity entity = (HttpEntity) response.getEntity();
|
||||
InputStream inputContent = entity.getContent();
|
||||
fileBytes = input2byte(inputContent);
|
||||
} finally {
|
||||
response.close();
|
||||
}
|
||||
} catch (ClientProtocolException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
// 关闭连接,释放资源
|
||||
try {
|
||||
httpclient.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return fileBytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* inputStream转换为byte字节数组
|
||||
* @param inStream
|
||||
* @return
|
||||
* @throws IOException
|
||||
*//*
|
||||
public static final byte[] input2byte(InputStream inStream) throws IOException {
|
||||
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
|
||||
byte[] buff = new byte[100];
|
||||
int rc = 0;
|
||||
while ((rc = inStream.read(buff, 0, 100)) > 0) {
|
||||
swapStream.write(buff, 0, rc);
|
||||
}
|
||||
byte[] in2b = swapStream.toByteArray();
|
||||
return in2b;
|
||||
}*/
|
||||
|
||||
public static String getPdfByHttpUrl(String address,File pdfFile) throws IOException {
|
||||
byte[] bytes = HttpUtils.sendHttpPost(address);
|
||||
InputStream is = null;
|
||||
OutputStream out = null;
|
||||
try {
|
||||
is = new ByteArrayInputStream(bytes);
|
||||
out = new FileOutputStream(pdfFile.getAbsolutePath());
|
||||
byte[] buff = new byte[1024];
|
||||
int len = 0;
|
||||
while((len=is.read(buff))!=-1){
|
||||
out.write(buff, 0, len);
|
||||
}
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
if (is != null){
|
||||
is.close();
|
||||
}
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
return pdfFile.getAbsolutePath();
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
byte[] bytes = HttpUtils.sendHttpPost(URL_PATH);
|
||||
InputStream is = null;
|
||||
OutputStream out = null;
|
||||
try {
|
||||
out = new FileOutputStream("D:\\test0615\\a.pdf");
|
||||
is = new ByteArrayInputStream(bytes);
|
||||
byte[] buff = new byte[1024];
|
||||
int len = 0;
|
||||
while((len=is.read(buff))!=-1){
|
||||
out.write(buff, 0, len);
|
||||
}
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
if (is != null){
|
||||
is.close();
|
||||
}
|
||||
if (out != null) {
|
||||
out.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue