maven 引用MultipartEntityBuilder
昵称
密码

maven 引用MultipartEntityBuilder

fl0919
2021-08-23 / 0 评论 / 15 阅读 / 正在检测是否收录...
/
 <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.5</version>
        </dependency>

上传示例

  public static void main(String[] args) throws URISyntaxException, IOException {
     //httpclient
        CloseableHttpClient httpClient= HttpClientBuilder.create().build();
//        http://cloud.liangblog.xyz/?user/index/loginSubmit&name=admin&password=19990919
//        51ceOMbDCZjs5O8fy3LKYO8DrLiAFaL3fQDVbydicnYx-ds78IHUxr_ybcaFMU7e9DYEfCBT1M4t2yunWg
       // http://server/index.php?explorer/upload/fileUpload
        HttpPost httpPost=new HttpPost("http://cloud.liangblog.xyz/index.php?explorer/upload/fileUpload");
        CloseableHttpResponse response=null;
        MultipartEntityBuilder multipartEntityBuilder=MultipartEntityBuilder.create();
        // 必须是file
        String filekey="file";
        File file1=new File("C:\\Users\\WS666\\Desktop\\测试图片.jpg");
        multipartEntityBuilder.addBinaryBody(filekey,file1);
        ContentType contentType=ContentType.create("text/plain", Charset.forName("UTF-8"));
        multipartEntityBuilder.addTextBody("file","测试.text",contentType);
        multipartEntityBuilder.addTextBody("name","测试图片.jpg",contentType);
        multipartEntityBuilder.addTextBody("path","{source:968}/");
        multipartEntityBuilder.addTextBody("accessToken","7e07NJcBUQ_p5vpADx6zz7kxaEVl4_mCtTku4E85fv22NOQc8-6-Yas1557x9FYZzj4KC2L6ewR2pGdBcA");
        HttpEntity httpEntity=multipartEntityBuilder.build();
        httpPost.setEntity(httpEntity);
        response=httpClient.execute(httpPost);
        HttpEntity responseEntity=response.getEntity();
        String responseStr = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);
        System.out.println("http响应状态码: "+response.getStatusLine());
        System.out.println("http响应长度: "+responseEntity.getContentLength());
        System.out.println("http响应内容: "+responseStr);

        httpClient.close();

    }
0

评论

博主关闭了所有页面的评论