HttpClient自带支持的,不需要你这么幸苦去搞Google下大把实例代码:HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost");
// 一个本地的文件
FileBody bin = new FileBody(new File("d:/BIMG1181.JPG"));
// 多部分的实体
MultipartEntity reqEntity = new MultipartEntity();
// 增加
reqEntity.addPart("bin", bin);
// 设置
httppost.setEntity(reqEntity);
System.out.println("执行: " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
HttpPost httppost = new HttpPost("http://localhost");
// 一个本地的文件
FileBody bin = new FileBody(new File("d:/BIMG1181.JPG"));
// 多部分的实体
MultipartEntity reqEntity = new MultipartEntity();
// 增加
reqEntity.addPart("bin", bin);
// 设置
httppost.setEntity(reqEntity);
System.out.println("执行: " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
HttpPost post = new HttpPost(urlPath);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("file", cbFile);
Map<String, String> map = new HashMap<String, String>();
map.put("uuid", im);
map.put("token", token);
for (Map.Entry<String, String> entry : map.entrySet()) {
mpEntity.addPart(entry.getKey(), new StringBody(entry.getValue()));
}
post.setEntity(mpEntity);
HttpResponse response = client.execute(post);
String content = EntityUtils.toString(response.getEntity());
System.out.println(content);
client.getConnectionManager().shutdown();
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file);
mpEntity.addPart("uuid", new StringBody(im));
mpEntity.addPart("token", new StringBody(token));
mpEntity.addPart("file", cbFile);
post.setEntity(mpEntity);
就是类似于http://www.xxxxx.com?id=10001&name=abc这样的两个参数
Failed to convert value of type 'java.lang.String' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.commons.CommonsMultipartFile]: no matching editors or conversion strategy found"报这个错误 =。=
MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
mpEntity.addPart("id", new StringBody("10001", "text/plain", Charset.forName("UTF-8")));