From bd946b3758c86dab28f044c0a411adca2c0b55f9 Mon Sep 17 00:00:00 2001 From: liurunyu <lry9898@163.com> Date: 星期一, 27 十一月 2023 14:32:42 +0800 Subject: [PATCH] 增加了相关apache poi及apache http相关 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/CreateRadom.java | 117 ++++++++ pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HttpUtils.java | 295 +++++++++++++++++++++ pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HSSF.java | 402 ++++++++++++++++++++++++++++ pipIrr-platform/pipIrr-common/pom.xml | 14 + 4 files changed, 828 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-common/pom.xml b/pipIrr-platform/pipIrr-common/pom.xml index 893e832..dac97f0 100644 --- a/pipIrr-platform/pipIrr-common/pom.xml +++ b/pipIrr-platform/pipIrr-common/pom.xml @@ -115,6 +115,20 @@ <artifactId>mina-core</artifactId> <version>2.2.2</version> </dependency> + + <!-- apache httpClient --> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>4.5.14</version> + </dependency> + <!-- apache poi --> + <dependency> + <groupId>org.apache.poi</groupId> + <artifactId>poi</artifactId> + <version>3.14</version> + </dependency> + <!-- quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/CreateRadom.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/CreateRadom.java new file mode 100644 index 0000000..aeb5ee2 --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/CreateRadom.java @@ -0,0 +1,117 @@ +package com.dy.common.util; + +import java.util.Random; + +public class CreateRadom { + + public static int radom(int max, int min){ + return new Random().nextInt(max) % (max - min + 1) + min; + } + + public static void main(String[] args) { + System.out.println(CreateRadom.radom(1, 0)); + System.out.println(CreateRadom.radom(2, 0)); + System.out.println(CreateRadom.radom(3, 0)); + System.out.println(CreateRadom.radom(4, 0)); + System.out.println(CreateRadom.radom(5, 0)); + System.out.println(CreateRadom.radom(100, 0)); + System.out.println(CreateRadom.radom(1256, 1234)); + } + + /** + * 4浣嶉殢鏈烘暟鎹� + * @return 闅忔椂鏁� + */ + public static int radom_4(){ + return new Random().nextInt(9999) % (9999 - 1000 + 1) + 1000; + } + /** + * 5浣嶉殢鏈烘暟鎹� + * @return 闅忔椂鏁� + */ + public static int radom_5(){ + return new Random().nextInt(99999) % (99999 - 10000 + 1) + 10000; + } + /** + * 6浣嶉殢鏈烘暟鎹� + * @return 闅忔椂鏁� + */ + public static int radom_6(){ + return new Random().nextInt(999999) % (999999 - 100000 + 1) + 100000; + } + + + /** + * 鍒涘缓scape浣嶉殢鏈烘暟 + * @return 闅忔椂鏁� + */ + public String create(int scape){ + if(scape < 1){ + scape = 6 ; + } + double d = Math.random(); + String s = String.valueOf(d); + int index; + String ss; + try{ + index = s.indexOf('.') + 1; + ss = s.substring(index , index + scape); + } catch(Exception e){ + ss = "740414"; + } + return ss ; + } + + /** + * 鍒涘缓涓や釜鏁存暟涔嬮棿鐨勯殢鏈烘暟 + * @param min 鏈�灏忓�� + * @param max 鏈�澶у�� + * @return 闅忔椂鏁� + */ + public static int create_between(int min , int max){ + if(max < min){ + return min ; + } + if(max - min < min/2){ + return min ; + } + String mins = String.valueOf(min) ; + int len = mins.length() ; + char minfirst = mins.charAt(0) ; + double d = Math.random(); + d = d * 10000000 ; + String s = String.valueOf(d); + s = minfirst + s ; + s = s.substring(0 ,len) ; + int n = Integer.parseInt(s) ; + if(n < min || n > max){ + n = create_between(min , max) ; + } + return n ; + } + + + /** + * 寰楀埌涓�涓皬浜巑ax鐨勯殢鏈烘暟 + * @param max int 鏈�澶у�� + * @return int 闅忔椂鏁� + */ + public int create_less(int max){ + if(max > 9){ + max = 9 ; + } + double d = Math.random(); + int n = 0 ; + int m; + String s = String.valueOf(d); + for(int i = 4 ; i < s.length() ; i++){ + m = Integer.parseInt(s.charAt(i)+""); + if(m < max){ + n = m ; + break ; + } + } + return n ; + } + +} diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HSSF.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HSSF.java new file mode 100644 index 0000000..430373f --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HSSF.java @@ -0,0 +1,402 @@ +package com.dy.common.util; + +import org.apache.poi.hssf.usermodel.*; +import org.apache.poi.hssf.util.HSSFColor; +import org.apache.poi.ss.usermodel.RichTextString; +import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.ss.util.RegionUtil; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +public abstract class HSSF { + + public HSSFCellStyle createTitleStyle_1(HSSFWorkbook workbook, boolean hasForegroundColor, int fontSize){ + HSSFCellStyle style = workbook.createCellStyle();// Create a style + if(hasForegroundColor) { + style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// The style settings + }else{ + style.setFillForegroundColor(HSSFColor.WHITE.index);// The style settings + } + style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setBorderBottom(HSSFCellStyle.BORDER_NONE); + style.setBorderLeft(HSSFCellStyle.BORDER_NONE); + style.setBorderRight(HSSFCellStyle.BORDER_NONE); + style.setBorderTop(HSSFCellStyle.BORDER_NONE); + style.setAlignment(HSSFCellStyle.ALIGN_CENTER); + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + style.setWrapText(true);// 鑷姩鎹㈣ + + HSSFFont font = workbook.createFont();// Create a font + font.setFontName("瀹嬩綋"); + font.setFontHeightInPoints((short) fontSize); + font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); + font.setColor(HSSFColor.BLACK.index); + style.setFont(font); + return style ; + } + public HSSFCellStyle createTitleStyle_2(HSSFWorkbook workbook, boolean hasForegroundColor, int fontSize){ + HSSFCellStyle style = workbook.createCellStyle();// Create a style + if(hasForegroundColor) { + style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// The style settings + }else{ + style.setFillForegroundColor(HSSFColor.WHITE.index);// The style settings + } + style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setBorderBottom(HSSFCellStyle.BORDER_NONE); + style.setBorderLeft(HSSFCellStyle.BORDER_NONE); + style.setBorderRight(HSSFCellStyle.BORDER_NONE); + style.setBorderTop(HSSFCellStyle.BORDER_NONE); + style.setAlignment(HSSFCellStyle.ALIGN_LEFT); + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + style.setWrapText(true);// 鑷姩鎹㈣ + + HSSFFont font = workbook.createFont();// Create a font + font.setFontName("瀹嬩綋"); + font.setFontHeightInPoints((short) fontSize); + //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); + font.setColor(HSSFColor.BLACK.index); + style.setFont(font); + return style ; + } + public HSSFCellStyle createTitleStyle_3(HSSFWorkbook workbook, boolean hasForegroundColor, int fontSize){ + HSSFCellStyle style = workbook.createCellStyle();// Create a style + if(hasForegroundColor) { + style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// The style settings + }else{ + style.setFillForegroundColor(HSSFColor.WHITE.index);// The style settings + } + style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); + style.setBorderLeft(HSSFCellStyle.BORDER_THIN); + style.setBorderRight(HSSFCellStyle.BORDER_THIN); + style.setBorderTop(HSSFCellStyle.BORDER_THIN); + style.setAlignment(HSSFCellStyle.ALIGN_CENTER); + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + style.setWrapText(true);// 鑷姩鎹㈣ + + HSSFFont font = workbook.createFont();// Create a font + font.setFontName("瀹嬩綋"); + font.setFontHeightInPoints((short) fontSize); + //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); + font.setColor(HSSFColor.BLACK.index); + style.setFont(font); + return style ; + } + + + public HSSFCellStyle createTitleStyle(HSSFWorkbook workbook, boolean hasForegroundColor){ + HSSFCellStyle style = workbook.createCellStyle();// Create a style + if(hasForegroundColor) { + style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// The style settings + }else{ + style.setFillForegroundColor(HSSFColor.WHITE.index);// The style settings + } + style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); + style.setBorderLeft(HSSFCellStyle.BORDER_THIN); + style.setBorderRight(HSSFCellStyle.BORDER_THIN); + style.setBorderTop(HSSFCellStyle.BORDER_THIN); + style.setAlignment(HSSFCellStyle.ALIGN_CENTER); + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + style.setWrapText(true);// 鑷姩鎹㈣ + + HSSFFont font = workbook.createFont();// Create a font + font.setFontName("瀹嬩綋"); + font.setFontHeightInPoints((short) 11); + font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); + font.setColor(HSSFColor.GREY_80_PERCENT.index); + style.setFont(font); + return style ; + } + + public HSSFCellStyle createContentCenterStyle(HSSFWorkbook workbook){ + HSSFCellStyle style = workbook.createCellStyle();// Create a style + style.setFillForegroundColor(HSSFColor.WHITE.index);// The style settings + style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); + style.setBorderLeft(HSSFCellStyle.BORDER_THIN); + style.setBorderRight(HSSFCellStyle.BORDER_THIN); + style.setBorderTop(HSSFCellStyle.BORDER_THIN); + style.setAlignment(HSSFCellStyle.ALIGN_CENTER); + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + style.setWrapText(true);// 鑷姩鎹㈣ + + HSSFFont font = workbook.createFont();// Create a font + font.setFontHeightInPoints((short) 10); + font.setFontName("瀹嬩綋"); + font.setColor(HSSFColor.GREY_80_PERCENT.index); + + style.setFont(font); + + return style ; + } + + public HSSFCellStyle createContentCenterStyleBFH(HSSFWorkbook workbook){ + HSSFCellStyle style = workbook.createCellStyle();// Create a style + style.setFillForegroundColor(HSSFColor.WHITE.index);// The style settings + style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); + style.setBorderLeft(HSSFCellStyle.BORDER_THIN); + style.setBorderRight(HSSFCellStyle.BORDER_THIN); + style.setBorderTop(HSSFCellStyle.BORDER_THIN); + style.setAlignment(HSSFCellStyle.ALIGN_CENTER); + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + style.setWrapText(true);// 鑷姩鎹㈣ + + HSSFFont font = workbook.createFont();// Create a font + font.setFontHeightInPoints((short) 10); + font.setFontName("瀹嬩綋"); + font.setColor(HSSFColor.GREY_80_PERCENT.index); + + style.setFont(font); + + style.setDataFormat(workbook.createDataFormat().getFormat("0.00%")); + + return style ; + } + public HSSFCellStyle createContentCenterStyle_1(HSSFWorkbook workbook, int fontSize){ + HSSFCellStyle style = workbook.createCellStyle();// Create a style + style.setFillForegroundColor(HSSFColor.WHITE.index);// The style settings + style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); + style.setBorderLeft(HSSFCellStyle.BORDER_THIN); + style.setBorderRight(HSSFCellStyle.BORDER_THIN); + style.setBorderTop(HSSFCellStyle.BORDER_THIN); + style.setAlignment(HSSFCellStyle.ALIGN_CENTER); + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + style.setWrapText(true);// 鑷姩鎹㈣ + + HSSFFont font = workbook.createFont();// Create a font + font.setFontName("瀹嬩綋"); + font.setFontHeightInPoints((short) fontSize); + font.setColor(HSSFColor.GREY_80_PERCENT.index); + style.setFont(font); + + return style ; + } + + + public HSSFCellStyle createContentLeftStyle(HSSFWorkbook workbook){ + HSSFCellStyle style = workbook.createCellStyle();// Create a style + style.setFillForegroundColor(HSSFColor.WHITE.index);// The style settings + style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); + style.setBorderLeft(HSSFCellStyle.BORDER_THIN); + style.setBorderRight(HSSFCellStyle.BORDER_THIN); + style.setBorderTop(HSSFCellStyle.BORDER_THIN); + style.setAlignment(HSSFCellStyle.ALIGN_LEFT); + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + style.setWrapText(true);// 鑷姩鎹㈣ + + HSSFFont font = workbook.createFont();// Create a font + font.setFontHeightInPoints((short) 10); + font.setFontName("瀹嬩綋"); + font.setColor(HSSFColor.GREY_80_PERCENT.index); + + style.setFont(font); + return style ; + } + + + public HSSFCellStyle createContentLeftStyle_1(HSSFWorkbook workbook, int fontSize){ + HSSFCellStyle style = workbook.createCellStyle();// Create a style + style.setFillForegroundColor(HSSFColor.WHITE.index);// The style settings + style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); + style.setBorderLeft(HSSFCellStyle.BORDER_THIN); + style.setBorderRight(HSSFCellStyle.BORDER_THIN); + style.setBorderTop(HSSFCellStyle.BORDER_THIN); + style.setAlignment(HSSFCellStyle.ALIGN_LEFT); + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + style.setWrapText(true);// 鑷姩鎹㈣ + + HSSFFont font = workbook.createFont();// Create a font + font.setFontName("瀹嬩綋"); + font.setFontHeightInPoints((short) fontSize); + font.setColor(HSSFColor.GREY_80_PERCENT.index); + + style.setFont(font); + return style ; + } + + + public HSSFCellStyle createContentRightStyle(HSSFWorkbook workbook){ + HSSFCellStyle style = workbook.createCellStyle();// Create a style + style.setFillForegroundColor(HSSFColor.WHITE.index);// The style settings + style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); + style.setBorderLeft(HSSFCellStyle.BORDER_THIN); + style.setBorderRight(HSSFCellStyle.BORDER_THIN); + style.setBorderTop(HSSFCellStyle.BORDER_THIN); + style.setAlignment(HSSFCellStyle.ALIGN_RIGHT); + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + style.setWrapText(true);// 鑷姩鎹㈣ + + HSSFFont font = workbook.createFont();// Create a font + font.setFontHeightInPoints((short) 10); + font.setFontName("瀹嬩綋"); + font.setColor(HSSFColor.GREY_80_PERCENT.index); + + style.setFont(font); + return style ; + } + + public HSSFCellStyle createContentRightStyle_1(HSSFWorkbook workbook, int fontSize){ + HSSFCellStyle style = workbook.createCellStyle();// Create a style + style.setFillForegroundColor(HSSFColor.WHITE.index);// The style settings + style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); + style.setBorderBottom(HSSFCellStyle.BORDER_THIN); + style.setBorderLeft(HSSFCellStyle.BORDER_THIN); + style.setBorderRight(HSSFCellStyle.BORDER_THIN); + style.setBorderTop(HSSFCellStyle.BORDER_THIN); + style.setAlignment(HSSFCellStyle.ALIGN_RIGHT); + style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); + style.setWrapText(true);// 鑷姩鎹㈣ + + HSSFFont font = workbook.createFont();// Create a font + font.setFontName("瀹嬩綋"); + font.setFontHeightInPoints((short) fontSize); + font.setColor(HSSFColor.GREY_80_PERCENT.index); + + style.setFont(font); + return style ; + } + + public HSSFCell createTitleCell(HSSFSheet sheet, HSSFRow row, String name, int index, Integer width, HSSFCellStyle style){ + HSSFCell cell = row.createCell(index);//Create first column cell + if(style != null){ + cell.setCellStyle(style); + } + cell.setCellType(HSSFCell.CELL_TYPE_STRING); + cell.setCellValue(new HSSFRichTextString(name)); + if(width != null){ + sheet.setColumnWidth(index, width * 1000); + } + return cell ; + } + public HSSFCell createTitleCell_1(HSSFSheet sheet, HSSFRow row, String name, int index, Double width, HSSFCellStyle style){ + HSSFCell cell = row.createCell(index);//Create first column cell + if(style != null){ + cell.setCellStyle(style); + } + cell.setCellType(HSSFCell.CELL_TYPE_STRING); + cell.setCellValue(new HSSFRichTextString(name)); + if(width != null){ + sheet.setColumnWidth(index, (int)(width * 1000)); + } + return cell ; + } + + public void setMergedRegionStyle(HSSFSheet sheet, CellRangeAddress region, HSSFCellStyle style) { + for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) { + HSSFRow row = sheet.getRow(i); + if (row == null) { + row = sheet.createRow(i); + } + for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) { + HSSFCell cell = row.getCell(j); + if (cell == null) { + cell = row.createCell(j); + cell.setCellValue(""); + } + cell.setCellStyle(style); + } + } + } + public void setRegionBorder(int border, CellRangeAddress region, HSSFSheet sheet, HSSFWorkbook wb){ + RegionUtil.setBorderBottom(border,region, sheet, wb); + RegionUtil.setBorderLeft(border,region, sheet, wb); + RegionUtil.setBorderRight(border,region, sheet, wb); + RegionUtil.setBorderTop(border,region, sheet, wb); + } + + public HSSFCell createDataDouCell(HSSFRow row, int index, Double value, HSSFCellStyle style){ + HSSFCell cell = row.createCell(index); + cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); + cell.setCellStyle(style); + if(value != null){ + cell.setCellValue(value); + } + return cell ; + } + + public HSSFCell createDataNumCell(HSSFRow row, int index, Integer value, HSSFCellStyle style){ + HSSFCell cell = row.createCell(index); + cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); + cell.setCellStyle(style); + if(value != null){ + cell.setCellValue(value); + } + return cell ; + } + + public HSSFCell createDataStrCell(HSSFRow row, int index, String value, HSSFCellStyle style){ + HSSFCell cell = row.createCell(index); + cell.setCellType(HSSFCell.CELL_TYPE_STRING); + cell.setCellStyle(style); + if(value != null){ + cell.setCellValue(value); + } + return cell ; + } + + + public HSSFCell createDataStrCell(HSSFRow row, int index, RichTextString value, HSSFCellStyle style){ + HSSFCell cell = row.createCell(index); + cell.setCellType(HSSFCell.CELL_TYPE_STRING); + cell.setCellStyle(style); + if(value != null){ + cell.setCellValue(value); + } + return cell ; + } + + + public HSSFCell[] createTitleCells(int startColIndex, Object[][] tcs, HSSFSheet sheet, HSSFRow row, HSSFCellStyle style){ + HSSFCell[] cells = new HSSFCell[tcs.length] ; + for(int i = 0; i < tcs.length; i++){ + cells[i] = createTitleCell(sheet, row, (String)tcs[i][0], startColIndex+ i, (Integer)tcs[i][1], style); + } + return cells ; + } + public HSSFCell[] createTitleCells_1(int startColIndex, Object[][] tcs, HSSFSheet sheet, HSSFRow row, HSSFCellStyle style){ + HSSFCell[] cells = new HSSFCell[tcs.length] ; + for(int i = 0; i < tcs.length; i++){ + cells[i] = createTitleCell_1(sheet, row, (String)tcs[i][0], startColIndex+ i, (Double)tcs[i][1], style); + } + return cells ; + } + + public void createContentCells(Object[][] ccs, HSSFRow row){ + for(int i = 0; i < ccs.length; i++){ + createDataStrCell(row, i, (String)ccs[i][0], (HSSFCellStyle)ccs[i][1]) ; + } + } + + + + /** + * 杈撳叆EXCEL鏂囦欢 + * @param workbook + * @param fileName 鏂囦欢鍚� + */ + public boolean outputExcel(HSSFWorkbook workbook, String fileName) { + boolean flag = true ; + FileOutputStream fos = null; + try { + fos = new FileOutputStream(new File(fileName)); + workbook.write(fos); + fos.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + flag = false ; + } catch (IOException e) { + e.printStackTrace(); + flag = false ; + } + return flag ; + } +} diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HttpUtils.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HttpUtils.java new file mode 100644 index 0000000..633f940 --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/HttpUtils.java @@ -0,0 +1,295 @@ +package com.dy.common.util; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.cert.X509Certificate; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.HttpClient; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpPut; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.conn.ClientConnectionManager; +import org.apache.http.conn.scheme.Scheme; +import org.apache.http.conn.scheme.SchemeRegistry; +import org.apache.http.conn.ssl.SSLSocketFactory; + +public class HttpUtils { + /** + * get + * + * @param host + * @param path + * @param headers + * @param querys + * @return + * @throws Exception + */ + public static HttpResponse doGet(String host, String path, Map<String, String> headers, + Map<String, String> querys) throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpGet request = new HttpGet(buildUrl(host, path, querys)); + if(headers != null){ + for (Map.Entry<String, String> e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + } + + return httpClient.execute(request); + } + + /** + * post form + * + * @param host + * @param path + * @param headers + * @param querys + * @param bodys + * @return + * @throws Exception + */ + public static HttpResponse doPost(String host, String path, Map<String, String> headers, + Map<String, String> querys, Map<String, String> bodys) throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPost request = new HttpPost(buildUrl(host, path, querys)); + for (Map.Entry<String, String> e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (bodys != null) { + List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); + + for (String key : bodys.keySet()) { + nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key))); + } + UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8"); + formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8"); + request.setEntity(formEntity); + } + + return httpClient.execute(request); + } + + /** + * Post String + * + * @param host + * @param path + * @param headers + * @param querys + * @param body + * @return + * @throws Exception + */ + public static HttpResponse doPost(String host, String path, Map<String, String> headers, + Map<String, String> querys, String body) throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPost request = new HttpPost(buildUrl(host, path, querys)); + for (Map.Entry<String, String> e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (StringUtils.isNotBlank(body)) { + request.setEntity(new StringEntity(body, "utf-8")); + } + + return httpClient.execute(request); + } + + /** + * Post stream + * + * @param host + * @param path + * @param headers + * @param querys + * @param body + * @return + * @throws Exception + */ + public static HttpResponse doPost(String host, String path, Map<String, String> headers, + Map<String, String> querys, byte[] body) throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPost request = new HttpPost(buildUrl(host, path, querys)); + if(headers != null){ + for (Map.Entry<String, String> e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + } + + if (body != null) { + request.setEntity(new ByteArrayEntity(body)); + } + + return httpClient.execute(request); + } + + /** + * Put String + * + * @param host + * @param path + * @param headers + * @param querys + * @param body + * @return + * @throws Exception + */ + public static HttpResponse doPut(String host, String path, Map<String, String> headers, + Map<String, String> querys, String body) throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPut request = new HttpPut(buildUrl(host, path, querys)); + for (Map.Entry<String, String> e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (StringUtils.isNotBlank(body)) { + request.setEntity(new StringEntity(body, "utf-8")); + } + + return httpClient.execute(request); + } + + /** + * Put stream + * + * @param host + * @param path + * @param headers + * @param querys + * @param body + * @return + * @throws Exception + */ + public static HttpResponse doPut(String host, String path, Map<String, String> headers, + Map<String, String> querys, byte[] body) throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpPut request = new HttpPut(buildUrl(host, path, querys)); + for (Map.Entry<String, String> e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + if (body != null) { + request.setEntity(new ByteArrayEntity(body)); + } + + return httpClient.execute(request); + } + + /** + * Delete + * + * @param host + * @param path + * @param headers + * @param querys + * @return + * @throws Exception + */ + public static HttpResponse doDelete(String host, String path, Map<String, String> headers, + Map<String, String> querys) throws Exception { + HttpClient httpClient = wrapClient(host); + + HttpDelete request = new HttpDelete(buildUrl(host, path, querys)); + for (Map.Entry<String, String> e : headers.entrySet()) { + request.addHeader(e.getKey(), e.getValue()); + } + + return httpClient.execute(request); + } + + private static String buildUrl(String host, String path, Map<String, String> querys) + throws UnsupportedEncodingException { + StringBuilder sbUrl = new StringBuilder(); + sbUrl.append(host); + if (!StringUtils.isBlank(path)) { + sbUrl.append(path); + } + if (null != querys) { + StringBuilder sbQuery = new StringBuilder(); + for (Map.Entry<String, String> query : querys.entrySet()) { + if (0 < sbQuery.length()) { + sbQuery.append("&"); + } + if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) { + sbQuery.append(query.getValue()); + } + if (!StringUtils.isBlank(query.getKey())) { + sbQuery.append(query.getKey()); + if (!StringUtils.isBlank(query.getValue())) { + sbQuery.append("="); + sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8")); + } + } + } + if (0 < sbQuery.length()) { + sbUrl.append("?").append(sbQuery); + } + } + + return sbUrl.toString(); + } + + private static HttpClient wrapClient(String host) { + CloseableHttpClient httpClient = HttpClients.createDefault(); + if (host.startsWith("https://")) { + sslClient(httpClient); + } + + return httpClient; + } + + private static void sslClient(HttpClient httpClient) { + try { + SSLContext ctx = SSLContext.getInstance("TLS"); + X509TrustManager tm = new X509TrustManager() { + public X509Certificate[] getAcceptedIssuers() { + return null; + } + + public void checkClientTrusted(X509Certificate[] xcs, String str) { + + } + + public void checkServerTrusted(X509Certificate[] xcs, String str) { + + } + }; + ctx.init(null, new TrustManager[] { tm }, null); + SSLSocketFactory ssf = new SSLSocketFactory(ctx); + ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + ClientConnectionManager ccm = httpClient.getConnectionManager(); + SchemeRegistry registry = ccm.getSchemeRegistry(); + registry.register(new Scheme("https", 443, ssf)); + } catch (KeyManagementException ex) { + throw new RuntimeException(ex); + } catch (NoSuchAlgorithmException ex) { + throw new RuntimeException(ex); + } + } +} -- Gitblit v1.8.0