接口:上传文件
2021-03-19 22:25
标签:上传 type exe hash map ash creat tostring keyset 接口:上传文件 标签:上传 type exe hash map ash creat tostring keyset 原文地址:https://www.cnblogs.com/xiaomifeng0510/p/12748829.html 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
header.put("Authorization", Utils.getAuthorization(user.getUserName(),user.getPassword()));//Basic YWRtaW4xMjM6MTIzcXdl
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
header.put("Authorization", authorization);//Basic YWRtaW4xMjM6MTIzcXdl
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();
}
}