HttpClient4.5.2模拟表单post提交上传文件

1、引入相关jar包

		<dependency>
		    <groupId>org.apache.httpcomponents</groupId>
		    <artifactId>httpclient</artifactId>
		    <version>4.5.2</version>
		</dependency>
		
		<dependency>
            <groupId>org.apache.httpcomponents</groupId>  
            <artifactId>httpmime</artifactId>  
            <version>4.5.2</version>  
        </dependency> 
        
        <dependency>
		    <groupId>org.apache.httpcomponents</groupId>
		    <artifactId>httpcore</artifactId>
		    <version>4.4.5</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpasyncclient</artifactId>
			<version>4.1.2</version>
		</dependency>


2、定义工具类方法

    public static String post(String serverUrl, String fileParamName, File file, Map<String, String> params)
            throws ClientProtocolException, IOException {
        HttpPost httpPost = new HttpPost(serverUrl);
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        // 上传的文件
        builder.addBinaryBody(fileParamName, file);
        // 设置其他参数
        for (Entry<String, String> entry : params.entrySet()) {
            builder.addTextBody(entry.getKey(), entry.getValue(), ContentType.TEXT_PLAIN.withCharset("UTF-8"));
        }
        HttpEntity httpEntity = builder.build();
        httpPost.setEntity(httpEntity);
        HttpClient httpClient = HttpClients.createDefault();
        HttpResponse response = httpClient.execute(httpPost);
        if (null == response || response.getStatusLine() == null) {
            logger.info("Post Request For Url[{}] is not ok. Response is null", serverUrl);
            return null;
        } else if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            logger.info("Post Request For Url[{}] is not ok. Response Status Code is {}", serverUrl,
                    response.getStatusLine().getStatusCode());
            return null;
        }
        return EntityUtils.toString(response.getEntity());
    }


3、测试一下

    public static void main(String[] args) {
        String url = "http://localhost:8081/lieni-app-inf-oa/resume/background/education/verify/sync";
        String fileUrl = "C:/Users/xiangpeng/Pictures/QQ20170417204948.jpg";
        Map<String, String> map = new HashMap<String, String>();
        map.put("operatorJobNumber", "102800038");
        map.put("operatorEmail", "pengyuqian@aimsen.com1");
        map.put("realed", "1");
        map.put("resumeEducationBackgroundId", "6045");
        try {
            System.out.println(HttpCaller.post(url, "degreeIcon", new File(fileUrl), map));
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值