lzy7422
public String dataPicAdd( @RequestParam(value = "img",required = false) MultipartFile uploadImage,
                             Model model)  throws Exception{
	String sTestsetFile=facePath;
    String sURL="https://www.baidu.com";
    //CloseableHttpClient意思是:可关闭的
    CloseableHttpClient httpClient = HttpClients.createDefault();//1、创建实例
    HttpPost uploadFile = new HttpPost(sURL);//2、创建请求
    
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    builder.addTextBody("text", "111", ContentType.TEXT_PLAIN);//传参
    builder.setCharset(Charset.forName("utf8"));//设置请求的编码格式
    //builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);//设置浏览器兼容模式
    
    // 把文件加到HTTP的post请求中
    File f = new File(sTestsetFile);
    builder.addBinaryBody(
            "img",
            new FileInputStream(f),
            ContentType.APPLICATION_OCTET_STREAM,
            f.getName()
    );

    HttpEntity multipart = builder.build();
    uploadFile.setEntity(multipart);//对于HttpPost对象而言,可调用setEntity(HttpEntity entity)方法来设置请求参数。
    CloseableHttpResponse response = httpClient.execute(uploadFile);//3、执行
    HttpEntity responseEntity = response.getEntity();//4、获取实体

    Header header=responseEntity.getContentType();
    //打印内容
    String sResponse= EntityUtils.toString(responseEntity, "UTF-8");//5、获取网页内容,并且指定编码
    System.out.println("Post 返回结果"+sResponse);
    httpClient.close();
    response.close();
    }
原文链接:https://blog.csdn.net/qq_43571415/article/details/100737810

分类:

技术点:

相关文章:

  • 2021-11-11
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2021-08-04
猜你喜欢
  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2021-07-09
  • 2022-12-23
  • 2022-12-23
  • 2021-12-09
相关资源
相似解决方案