接口:上传文件

 public static void uploadInterface(User user ,String baseUrl,String filePath ,boolean isOverWrite,String remotePath){

CloseableHttpClient client = HttpClients.createDefault();
String uploadFile_url = Utils.getValue("uploadFile.url");
String url=baseUrl+uploadFile_url;
HttpPost httpPost=new HttpPost(url);//通过post传递

//heard处理
HashMap<String, String> header = new HashMap<>();
header.put("Authorization", Utils.getAuthorization(user.getUserName(),user.getPassword()));//Basic YWRtaW4xMjM6MTIzcXdl
Iterator<String> iterator_header = header.keySet().iterator();
while(iterator_header.hasNext()){
String key = iterator_header.next();
httpPost.addHeader(key,header.get(key));
}
String isOver="";
if(isOverWrite){
isOver="1";
}
else{
isOver="-1";
}
if(!remotePath.endsWith("/")){
remotePath=remotePath+"/";
}
File file=new File(filePath);
String fileName=file.getName();
MultipartEntityBuilder builder = MultipartEntityBuilder.create();

builder.addTextBody("userId", user.getUserId(),ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("remotePath", remotePath,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("isOverWrite", isOver,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("isSendEmail", String.valueOf(false),ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowChunkNumber", "1",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowChunkSize", "104857600",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowCurrentChunkSize", "3493921",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowTotalSize", "3493921",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowIdentifier", "3493921-1jpg",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowFilename", fileName,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowRelativePath", fileName,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowTotalChunks", "1",ContentType.TEXT_PLAIN.withCharset("UTF-8"));

builder.addBinaryBody("file", file, ContentType.create("image/jpeg"), fileName);
HttpEntity entity = builder.build();

httpPost.setEntity(entity);

/**发送请求*/
try {
String result="";
HttpResponse response=client.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
//判断是否上传成功 返回200
if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
}
} catch (ClientProtocolException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}



public static void uploadInterface(String userid ,String authorization,String baseUrl,String filePath ,boolean isOverWrite,String remotePath){

CloseableHttpClient client = HttpClients.createDefault();
String uploadFile_url = Utils.getValue("uploadFile.url");
String url=baseUrl+uploadFile_url;
HttpPost httpPost=new HttpPost(url);//通过post传递

//heard处理
HashMap<String, String> header = new HashMap<>();
header.put("Authorization", authorization);//Basic YWRtaW4xMjM6MTIzcXdl
Iterator<String> iterator_header = header.keySet().iterator();
while(iterator_header.hasNext()){
String key = iterator_header.next();
httpPost.addHeader(key,header.get(key));
}
String isOver="";
if(isOverWrite){
isOver="1";
}
else{
isOver="-1";
}
if(!remotePath.endsWith("/")){
remotePath=remotePath+"/";
}
File file=new File(filePath);
String fileName=file.getName();
MultipartEntityBuilder builder = MultipartEntityBuilder.create();

builder.addTextBody("userId", userid,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("remotePath", remotePath,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("isOverWrite", isOver,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("isSendEmail", String.valueOf(false),ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowChunkNumber", "1",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowChunkSize", "104857600",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowCurrentChunkSize", "3493921",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowTotalSize", "3493921",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowIdentifier", "3493921-1jpg",ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowFilename", fileName,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowRelativePath", fileName,ContentType.TEXT_PLAIN.withCharset("UTF-8"))
.addTextBody("flowTotalChunks", "1",ContentType.TEXT_PLAIN.withCharset("UTF-8"));

builder.addBinaryBody("file", file, ContentType.create("image/jpeg"), fileName);
HttpEntity entity = builder.build();

httpPost.setEntity(entity);

/**发送请求*/
try {
String result="";
HttpResponse response=client.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
//判断是否上传成功 返回200
if (response.getStatusLine().getStatusCode()== HttpStatus.SC_OK){
}
} catch (ClientProtocolException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}
posted @ 2020-04-21 23:23  采蘑菇的小蜜蜂  阅读(228)  评论(0编辑  收藏  举报