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();

解决方案 »

  1.   

    高手你在啊  请帮我看看下面这段代码吧  地址需要两个参数  我这样写貌似还是不对啊 HttpClient client = new DefaultHttpClient();
    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();
      

  2.   

    地址需要两个参数是啥意思?不过你设置其它参数的方式有点问题啊:
    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);
      

  3.   


    就是类似于http://www.xxxxx.com?id=10001&name=abc这样的两个参数
      

  4.   

    没必要带在URL里面啊,直接增加到 mpEntity 里面就好了:mpEntity.addPart("id", new StringBody("10001")); 或者你就直接组装URI。
      

  5.   


    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"报这个错误  =。=
      

  6.   

    咦?服务器端抛这个错误?看起来像是类型识别错误,可能要这样修改:
    MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    mpEntity.addPart("id", new StringBody("10001", "text/plain", Charset.forName("UTF-8")));