低版本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;
}
}