可能以下几行可以帮助您。我正在使用 Apache Http api。
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost post = new HttpPost( "http://localhost:8080/service/uploadFile"));
MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
StringBody size= new StringBody("50x50");
entity.addPart("smallSize", size);
entity.addPart("file", new FileBody(new File("D:/abc.txt") ));
post.setEntity(entity);
HttpResponse response = client.execute(post);
String responseFromServer = EntityUtils.toString(response.getEntity(), "UTF-8" );
client.getConnectionManager().shutdown();
System.out.println("response from server: "+responseFromServer);
根据您的请求类型,在哪里HttpPost
和类可用。HttpGet
对于发送文件,我们需要使用MultipartEntity
类。如果您想发送其他表单字段以及您FileBody
喜欢的任何元数据,您可以使用StringBody
.
我希望这可以帮助你一点。:)