import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
httpclient连接池Utils
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
httpclient连接池Utils
public class HttpClientUtils {
/**httpclient连接池Utils
* produce http client
*
* @return
* @throws KeyStoreException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static CloseableHttpClient acceptsHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClientBuilder b = HttpClientBuilder.create();
PoolingHttpClientConnectionManager connMgr = new PoolingHttpClientConnectionManager();
connMgr.setMaxTotal(200);
connMgr.setDefaultMaxPerRoute(100);
b.setConnectionManager(connMgr);
CloseableHttpClient client = b.build();
return client;
}
}
代码片段模拟Post提交方式
CloseableHttpClient httpclient = HttpClientUitls.acceptsHttpClient();
String JSONstr = null;//求情返回的JSON数据
UrlEncodedFormEntity uefEntity;
CloseableHttpResponse closeableHttpResponse=null;
String url="url路径";
List formparams=null;
try {
HttpPost httppost = new HttpPost(url);
if (formparams == null) {//提交的参数
formparams = new ArrayList();
}
//具体需求具体参数
// formparams.add(new BasicNameValuePair("pageNumber", pageNumber.toString()));
//formparams.add(new BasicNameValuePair("pageSize", pageSize.toString()));
uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
closeableHttpResponse = httpclient.execute(httppost);
HttpEntity entity = closeableHttpResponse.getEntity();
if (entity != null) {
JSONstr = EntityUtils.toString(entity, "UTF-8");
}else{
throw new RuntimeException("httpclient调用失败---检查参数");
}
}catch (IOException e) {
throw new IOException(e);
}finally{
/*
* 释放资源
* */
closeableHttpResponse.close();
httpclient.close();
}
那么如果我们要用上传文件呢?
模拟文件上传 提交 方式,但是如果处理端,就是服务器端的项目使用springMvc 可能引发
List
CloseableHttpClient httpclient = HttpClientUitls.acceptsHttpClient();
// 求情返回的JSON数据
CloseableHttpResponse closeableHttpResponse = null;
String url2 = ZHRTActivityUrlUtils.baseUrl + ZHRTActivityUrlUtils.uploadThirdTppPic;
try {
String realPath = request.getServletContext().getRealPath("/upload");
String[] urlPathArr = repaymentProve.getRepaymentProveUrl().split(",");
List fileBodyList = new ArrayList();
List stringBodyList = new ArrayList();
for (String urlPath : urlPathArr) {
File file = new File(realPath + urlPath);
FileBody fileBody = new FileBody(file);
StringBody stringBody = new StringBody(urlPath, ContentType.DEFAULT_TEXT);
fileBodyList.add(fileBody);
stringBodyList.add(stringBody);
}
HttpPost httppost = new HttpPost(url2);
// InputStreamBody inputStreamBody=new
// InputStreamBody(file.getInputStream(),"inputStream");
// StringBody stringBody=new
// StringBody(repaymentProve.getRepaymentProveUrl(),ContentType.DEFAULT_TEXT);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
for (int i = 0; i < fileBodyList.size(); i++) {
multipartEntityBuilder.addPart("fileBody" + i, fileBodyList.get(i));
multipartEntityBuilder.addPart("stringBody" + i, stringBodyList.get(i));
}
HttpEntity reqEntity = multipartEntityBuilder.build();
httppost.setEntity(reqEntity);
closeableHttpResponse = httpclient.execute(httppost);
HttpEntity entity = closeableHttpResponse.getEntity();
if (entity != null) {
JSONstr = EntityUtils.toString(entity, "UTF-8");
} else {
throw new RuntimeException("httpclient调用失败---检查参数");
}
} catch (IOException e) {
throw new IOException(e);
} finally {
/*
* 释放资源
*/
if (closeableHttpResponse != null) {
closeableHttpResponse.close();
}
if (httpclient != null) {
httpclient.close();
}
}
那么上传文件服务器如何解析呢?
代码片段(这断代码不能直接运行,提供思路的伪代码)
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
try {
if (isMultipart) {
DiskFileItemFactory factory = new DiskFileItemFactory();
/*
* //指定在内存中缓存数据大小,单位为byte,这里设为1Mb
* factory.setSizeThreshold(1024*1024);
* //设置一旦文件大小超过getSizeThreshold()的值时数据存放在硬盘的目录
* factory.setRepository(new File("D://temp"));
*/
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// 指定单个上传文件的最大尺寸,单位:字节,这里设为5Mb
/*
* upload.setFileSizeMax(5 * 1024 * 1024);
* //指定一次上传多个文件的总尺寸,单位:字节,这里设为10Mb upload.setSizeMax(10 * 1024 *
* 1024);
*/
// upload.setHeaderEncoding("UTF-8"); // 设置编码,因为我的jsp页面的编码是utf-8的
ServletFileUpload upload = new ServletFileUpload(facotry);
List items= upload.parseRequest(request);
// 解析request请求
if (items != null) {
// 把上传文件放到服务器的这个目录下
if (!new File(uploadPath).isDirectory()) {
new File(uploadPath).mkdirs(); // 选定上传的目录此处为当前目录,没有则创建
}
// 解析表单项目
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
// 如果是普通表单属性
if (item.isFormField()) {
//
String name = item.getFieldName();// 相当于input的name属性
String value = item.getString();// input的value属性
System.out.println("属性:" + name + " 属性值:" + value);
}
// 如果是上传文件
else {
// 属性名
String fieldName = item.getFieldName();
// 上传文件路径
String fileName = item.getName();
//这是我们需要的流,拿到了这个,就可以保存文件了.剩下代码自己补充吧,
item.getInputStream();
@RequestMapping(value = "/uploadThirdTppPic")
@ResponseBody
public String uploadTTPPic(HttpServletRequest request,HttpServletResponse response) {
// 利用apache的common-upload上传组件来进行 来解析获取到的流文件
Gson gson = new Gson();
// 把上传来的文件放在这里
// CommonsMultipartFile c=(CommonsMultipartFile) request;
try {
String lujing= request.getParameter("uploadPath");
Map map=new HashMap();
Map parameterMap= request.getParameterMap();
for(String key: parameterMap.keySet()){
String[] value= parameterMap.get(key);
if(value.length>0&&key.startsWith("stringBody")){
map.put(Integer.parseInt(key.replaceAll("stringBody", "")) , value[0]);
}
}
String uploadThirdTpp = request.getServletContext().getRealPath("/uploadThirdTpp");// 获取文件路径
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultiValueMap mmap= multipartRequest.getMultiFileMap();
for(String key: mmap.keySet()){
List list=mmap.get(key);
if(list.size()>0){
for(MultipartFile mf:list){
Integer i=Integer.parseInt(key.replaceAll("fileBody", ""));
File file=new File(uploadThirdTpp,map.get(i));
if(file.exists()){
file.delete();
}
// FileUtils.copyInputStreamToFile(mf.getInputStream(), file);
File parentFile= file.getParentFile();
if(parentFile!=null){
parentFile.mkdirs();
}
file.createNewFile();
// file.mkdirs();
InputStream inputStream= mf.getInputStream();
FileOutputStream fileOutputStream=new FileOutputStream(file);
byte[] b=new byte[1024];
int len=0;
while(( len =inputStream.read(b))!=-1 ){
fileOutputStream.write(b, 0, len);
fileOutputStream.flush();
}
inputStream.close();
fileOutputStream.close();
}
}
}
return "success";
}catch (Exception e) {
e.printStackTrace();
return gson.toJson("fail");
}
}
Post提交拿到抓取页面数据.
HttpClient httpClient = acceptsHttpClient();
HttpPost httpPost = new HttpPost("http://www.baidu.com/");
try {
HttpClientContext httpClientContext = HttpClientContext.create();
httpClientContext.setCookieStore(new BasicCookieStore());
// 禁止redirect,手动处理
RequestConfig requestConfig = RequestConfig.custom().setCircularRedirectsAllowed(false).setSocketTimeout(50000)
.setConnectTimeout(50000).setConnectionRequestTimeout(50000).setRedirectsEnabled(false).build();
httpClientContext.setRequestConfig(requestConfig);
List
formparams.add(new BasicNameValuePair("voucherType", ""));
/*
* 设置psot提交的参数
* if (voucherType != null && !"".equals(voucherType)) {
formparams.add(new BasicNameValuePair("voucherType", voucherType));
}
if (isManagerCre != null && !"".equals(isManagerCre)) {
formparams.add(new BasicNameValuePair("isManagerCre", isManagerCre));
}*/
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
httpPost.setEntity(entity);
HttpResponse statusCode = httpClient.execute(httpPost, httpClientContext);
int statuscode = statusCode.getStatusLine().getStatusCode();
if (statuscode == 200) {
String jsoupDocument = EntityUtils.toString(statusCode.getEntity());
System.out.println(jsoupDocument);
}else{
System.err.println("页面无法访问!!!");
}
} catch (Exception e) {
System.err.println("页面无法访问");
}finally{
}