接口:上传文件

Posted xiaomifeng0510

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了接口:上传文件相关的知识,希望对你有一定的参考价值。

 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();
}

}

以上是关于接口:上传文件的主要内容,如果未能解决你的问题,请参考以下文章

文件如何上传接口又如何下载下来

Python接口测试文件上传

requests请求接口---上传文件

layui文件上传 接口怎么写

接口规范 13. 文件上传及管理相关接口

layui文件上传接口怎么写