客户端核心代码如下: HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); MultipartEntity multipartEntity = new MultipartEntity(); FileBody fileBody = new FileBody(file); // file 是服务端读取文件的 key "file" name="file" /> 对应的 multipartEntity.addPart("file", fileBody); httpPost.setEntity( multipartEntity); try { HttpResponse response = httpClient.execute(httpPost); if(response.getStatusLine
HttpPost httpPost = new HttpPost("http://localhost/file/upload"); try { final MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create(); multipartEntity.setCharset(Charset.forName("utf-8")); multipartEntity.setMode (HttpMultipartMode.BROWSER_COMPATIBLE); //for (String[] nameValue : datas) {//普通data字段 // multipartEntity.addPart (nameValue[0], new StringBody(nameValue[1], ContentType.APPLICATION_JSON)); //} multipartEntity.addBinaryBody ("file", file);//文件字段 httpPost.setEntity(multipartEntity.build()); //httpPost.setHeader("token"
org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.MultipartEntity HttpPost post = new HttpPost( "http://service.ireadhome.com/api/Upload/Image"); MultipartEntity entity = new MultipartEntity(); // 上传 文本, 转换编码为utf-8 其中"text" 为字段名, // 后边new StringBody(text,
说明:使用是httpclient+MultipartEntity,因此需要导入包:httpmime-4.1.1(这个包非常重要!务必导入!) index.php"; HttpPost httppost = new HttpPost(urlServer); File file = new File(pathToOurFile); MultipartEntity mpEntity = new MultipartEntity(); // 文件传输(这个需要用到httpmime-4.1.1.jar) ContentBody cbFile = new FileBody
实际开发用的很多 post.setEntity(new StringEntity(jsonString)); file形式:需要jar包httpmime:很多框架上传图片就是用他 MultipartEntity entity = new MultipartEntity(); entity.addPart("actimg", new FileBody(file)); post.setEntity 传递二进制类型的参数 MultipartEntity entity = new MultipartEntity(); entity.addPart("actimg", new FileBody
传递二进制类型的参数 MultipartEntity entity = new MultipartEntity(); entity.addPart("actimg", new FileBody
httpResponse.getEntity(); String content = EntityUtils.toString(httpEntity); System.out.println(content); 其中,之前版本的MultipartEntity
封装了一些常用的处理工具如get请求工具HttpGet、post请求工具HttpPost、http响应工具HttpResponse、url编码表单工具UrlEncodedFormEntity、分段传输工具MultipartEntity
-- 提供FileBody、StringBody和MultipartEntity 使用httpClient上传文件需要的类 --> <dependency> <groupId>org.apache.httpcomponents