HttpClient的定義:
是一個基於HttpCore的客戶端Http傳輸類庫
基於傳統的(阻塞)IO內容無關
HttpClient不是瀏覽器,它是一個客戶端http協議傳輸類庫。HttpClient被用來發送和接受Http消息。HttpClient不會處理http消息的內容,不會進行javascript解析,不會關心content type,如果沒有明確設置,httpclient也不會對請求進行格式化、重定向url,或者其他任何和http消息傳輸相關的功能。
在項目中引入HttpClient,以Maven爲例:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
二、遇到的問題及解決方案
1、 缺少證書
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
問題的根本是:
缺少安全證書時出現的異常。
解決問題方法:
從網站https://confluence.atlassian.com/download/attachments/180292346/InstallCert.java 下載InstallCert.java程序
編譯InstallCert.java,然後執行:java InstallCert hostname,比如:java InstallCert www.163.com
根據提示操作,會在當前的目錄下產生一個名爲“ssecacerts”的證書。
將證書拷貝到$JAVA_HOME/jre/lib/security目錄下,問題便得到解決。
2、 上傳excel文件
A、請求負載是文件的情形
按F12,打開chrome,在上傳excel文件時,請求負載如圖所示:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5</version>
</dependency>
實現的核心代碼爲:
HttpPost httpPost = new HttpPost(url);
FileBody file = new FileBody(new File(GlobalSetting.getResoucesPath()+fileName));
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("myfile", file)
.build();
httpPost.setEntity(reqEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
其中,fileName爲“商品資料批量導入模板(女裝).xlsx”。需要創建FileBody,並將其添加到由MultipartEntityBuilder創建的HttpEntity中。
B、請求負載中有字符串的情形
實現的核心代碼爲:
HttpPost httpPost = new HttpPost(url);
FileBody file = new FileBody(new File(GlobalSetting.getResoucesPath()+fileName));
StringBody id = new StringBody(PoId,Charset.forName("UTF-8"));
HttpEntity reqEntity = MultipartEntityBuilder.create()
.addPart("scheduleId", id)
.addPart("myfile", file)
.build();
httpPost.setEntity(reqEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
其中,fileName爲“檔期商品批量導入模板.xlsx”;PoId爲“1033046”。需要創建FileBody和StringBody並將其添加到由MultipartEntityBuilder創建的HttpEntity中。
3、 POST請求不是鍵值對的形式
一般情況下post請求的負載中都是鍵值對的形式,如下圖所示:
以下是通過JSON實現:
JSONObject postEntityJSON= new JSONObject();
postEntityJSON.put("curSupplierAreaId","0");
postEntityJSON.put("endDate","1444924799000");
postEntityJSON.put("limit","10");
postEntityJSON.put("offset","0");
postEntityJSON.put("startDate","1410710400000");
postEntityJSON.put("status","0");
現在遇到的難題是請求不是鍵值對的形式,如下圖所示:
實現的核心代碼爲:
HttpPost post = new HttpPost(url);
post.setHeader("Accept", "application/json");
post.setHeader("Content-Type", "application/json");
post.setEntity(new StringEntity("["+PoId+"]"));
CloseableHttpResponse response = httpclient.execute(post);
其中,PoId爲“1033046”,因爲之前沒遇到過請求不是鍵值對的情形,所以不知如何實現,通過查閱官方文檔後發現,直接設置post的實體即可實現。
4、 修改excel文件內容
因爲建立檔期需要通過excel文件上傳,手動修改excel文件的內容比較麻煩,所以想通過java在程序中修改excel文件的相關內容,提高效率。
需要在maven中添加依賴,如下所示:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
其中poi針對後綴爲.xls的excel文件,poi-ooxml針對後綴爲.xlsx的excel文件;
其中部分代碼如下所示:
try
{
FileInputStream file = new FileInputStream(new File(GlobalSetting.getResoucesPath()+excelName));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
int rowNum = sheet.getLastRowNum();
XSSFRow row = sheet.getRow(0);
int index = 0;
for (int i = 2; i <= rowNum; i++) {
row = sheet.getRow(i);
row.getCell(3).setCellValue(list.get(index++));
}
FileOutputStream out = new FileOutputStream(new File(GlobalSetting.getResoucesPath()+excelName));
workbook.write(out);
file.close();
out.close();
} catch (Exception e){
e.printStackTrace();
}
5、 壓縮文件
由於商品圖片是通過上傳ZIP壓縮文件實現的,在圖片上傳之前要把命名號的商品圖片進行壓縮,人爲進行操作也很麻煩,希望通過java程序實現文件的壓縮。
Java中有現存的zip壓縮的API,需要import java.util.zip.*;
操作流程爲:複製文件到指定目錄並按照要求命名各個商品圖片文件名,然後將各個商品文件集體壓縮成zip包,接着刪除各個商品圖片文件名,降低存儲空間。最後便可上傳壓縮的ZIP包了。
原文地址:http://qa.blog.163.com/blog/static/1901470022015712234420/