2、  上传excel文件
  A、请求负载是文件的情形
  按F12,打开chrome,在上传excel文件时,请求负载如图所示:
  HttpClient 接口测试遇到的问题及解决方案 - 葛庆阳 - dreamsyeah
  通过post请求上传文件用到org.apache.httpcomponents的httpmime jar包,在Maven中加入依赖:
  <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、请求负载中有字符串的情形
  HttpClient 接口测试遇到的问题及解决方案 - 葛庆阳 - dreamsyeah
  实现的核心代码为:
  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请求的负载中都是键值对的形式,如下图所示:
  HttpClient 接口测试遇到的问题及解决方案 - 葛庆阳 - dreamsyeah
  通过JSON或者List <NameValuePair> nvps = new ArrayList <NameValuePair>()的方式实现;
  以下是通过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");
  现在遇到的难题是请求不是键值对的形式,如下图所示:
  HttpClient 接口测试遇到的问题及解决方案 - 葛庆阳 - dreamsyeah
  实现的核心代码为:
  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包了。