post提交文件类型

低版本httpclient4.1.2.jar使用例子:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

public class TestPost {

	public static void main(String[] args) throws ClientProtocolException, IOException {
		String url = "链接地址";
		String key = "key";
		String codeType = "3006";
		File file = new File("C:\\Users\\Desktop\\图片\\jpg\\32.jpg");

		MultipartEntity multipartEntity = new MultipartEntity();
		multipartEntity.addPart("codeType", new StringBody(codeType, Charset.forName("UTF-8")));
		multipartEntity.addPart("key", new StringBody(key, Charset.forName("UTF-8")));
		//提交文件类型
		multipartEntity.addPart("image", new FileBody(file, file.getName(), "image/jpeg", "utf-8"));

		HttpPost request = new HttpPost(url);
		request.setEntity(multipartEntity);

		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpResponse response = httpClient.execute(request);
		
		BufferedInputStream inputStream = new BufferedInputStream(response.getEntity().getContent());
		StringBuffer result = new StringBuffer();
		byte[] bytes = new byte[1024];
		while(inputStream.read(bytes) != -1) {
			result.append(new String(bytes, 0, bytes.length));
		}
		System.out.println("发送消息收到的返回:" + result.toString());
	}
}

高版本httpclient4.3.jar以上使用例子:

import java.io.BufferedInputStream;
import java.io.File;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class JuheDemo {
	public static final String APPKEY = "key";

	public static String post(String type, File file) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		CloseableHttpResponse response = null;
		String result = null;
		try {
			RequestConfig config = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build();
			HttpPost httppost = new HttpPost("链接地址");
			StringBody keyBody = new StringBody(APPKEY, ContentType.TEXT_PLAIN);
			StringBody typeBody = new StringBody(type, ContentType.TEXT_PLAIN);
			HttpEntity reqEntity = MultipartEntityBuilder.create()
					.addBinaryBody("image", file, ContentType.create("image/jpeg"), file.getName())
					.addPart("key", keyBody).addPart("codeType", typeBody).build();
			httppost.setEntity(reqEntity);
			httppost.setConfig(config);
			response = httpClient.execute(httppost);
			HttpEntity resEntity = response.getEntity();
			byte[] bytes = new byte[1024];
			if (resEntity != null) {
				BufferedInputStream bufferedInputStream = new BufferedInputStream(resEntity.getContent());
				while (bufferedInputStream.read(bytes) != -1) {
					result += new String(bytes, 0, bytes.length);
				}
			}
			EntityUtils.consume(resEntity);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			response.close();
			httpClient.close();
		}
		return result;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值