100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java string 包含http_Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件)...

java string 包含http_Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件)...

时间:2022-10-30 13:58:14

相关推荐

java string 包含http_Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件)...

一、HttpPost上传文件

public static String getSuffix(final MultipartFile file){

if(file == null || file.getSize() == 0){

return null;

}

String fileName = file.getOriginalFilename();

return fileName.substring(fileName.lastIndexOf(".")+1);

}

public static JSONObject uploadFile(String urlStr, MultipartFile file, String token) throws IOException {

// 后缀

String suffix = getSuffix(file);

CloseableHttpClient httpClient = HttpClients.createDefault();

HttpPost uploadFile = new HttpPost(urlStr);

uploadFile.setHeader("authorization","Bearer " + token);

DecimalFormat df = new DecimalFormat("#.##");

MultipartEntityBuilder builder = MultipartEntityBuilder.create();

// HTTP.PLAIN_TEXT_TYPE,HTTP.UTF_8

builder.addTextBody("name", file.getOriginalFilename(), ContentType.create("text/plain", Consts.UTF_8));

builder.addTextBody("size", df.format((double) file.getSize() / 1024), ContentType.TEXT_PLAIN);

builder.addTextBody("suffix", suffix, ContentType.TEXT_PLAIN);

// 把文件加到HTTP的post请求中

// String filepath = "/user/test/123.png"

// File f = new File(filepath);

builder.addBinaryBody(

"file",

file.getInputStream(),

// new FileInputStream(f),

ContentType.APPLICATION_OCTET_STREAM,

file.getOriginalFilename()

);

HttpEntity multipart = builder.build();

uploadFile.setEntity(multipart);

CloseableHttpResponse response = httpClient.execute(uploadFile);

HttpEntity responseEntity = response.getEntity();

String sResponse= EntityUtils.toString(responseEntity, "UTF-8");

JSONObject jsonObject = JSONObject.parseObject(sResponse);

// {"code":1,"data":"7efb19980373dd90f5077576afa7481a","message":""}

// {"code":401,"httpStatus":null,"data":"373656a2-baff-423a-93fb-704f51003509","message":"error"}

return jsonObject;

}复制代码

二、HttpGet对API进行Get请求

两张方式:

1、在URL中拼接

CloseableHttpClient httpClient = HttpClients.createDefault();

String urlStr ="/oss/getUrl?id=" + ossFileId;

HttpGet httpGet = new HttpGet(urlStr);

// 使用Oauth2进行权限验证

httpGet.setHeader("authorization","Bearer 34195fa8-00a6-4288-bda9-7d37541c3a39");

CloseableHttpResponse response = httpClient.execute(httpGet);

HttpEntity responseEntity = response.getEntity();

String sResponse= EntityUtils.toString(responseEntity, "UTF-8");

System.out.println(sResponse);

JSONObject jsonObject = JSONObject.parseObject(sResponse);

System.out.println(jsonObject);复制代码

2、使用参数的方式

CloseableHttpClient httpClient = HttpClients.createDefault();

/*

* 由于GET请求的参数都是拼装在URL地址后方,所以我们要构建一个URL,带参数

*/

URIBuilder uriBuilder = new URIBuilder("");

/** 第一种添加参数的形式 */

/*uriBuilder.addParameter("name", "root");

uriBuilder.addParameter("password", "123456");*/

/** 第二种添加参数的形式 */

List list = new LinkedList<>();

BasicNameValuePair param1 = new BasicNameValuePair("name", "root");

BasicNameValuePair param2 = new BasicNameValuePair("password", "123456");

list.add(param1);

list.add(param2);

uriBuilder.setParameters(list);

// 根据带参数的URI对象构建GET请求对象

HttpGet httpGet = new HttpGet(uriBuilder.build());

// 使用Oauth2进行权限验证

httpGet.setHeader("authorization","Bearer 34195fa8-00a6-4288-bda9-7d37541c3a39");

CloseableHttpResponse response = httpClient.execute(httpGet);

HttpEntity responseEntity = response.getEntity();

String sResponse= EntityUtils.toString(responseEntity, "UTF-8");

System.out.println(sResponse);

JSONObject jsonObject = JSONObject.parseObject(sResponse);

System.out.println(jsonObject);复制代码

本文由博客一文多发平台 OpenWrite 发布!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。