0
A
回答
0
2
下面是你可以通过Java发送$_POST
(特别是在Android设备)。它不应该太难转换为$_FILE
。这里的一切都是奖金。
public void sendPostData(String url, String text) {
// Setup a HTTP client, HttpPost (that contains data you wanna send) and
// a HttpResponse that gonna catch a response.
DefaultHttpClient postClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse response;
try {
// Make a List. Increase the size as you wish.
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
// Add your form name and a text that belongs to the actual form.
nameValuePairs.add(new BasicNameValuePair("your_form_name", text));
// Set the entity of your HttpPost.
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute your request against the given url and catch the response.
response = postClient.execute(httpPost);
// Status code 200 == successfully posted data.
if(response.getStatusLine().getStatusCode() == 200) {
// Do something. Maybe you wanna get your response
// and see what it contains, with HttpEntity class?
}
} catch (Exception e) {
}
}
0
听起来像是你需要一个org.apache.http.entity.mime.MultipartEntity
的神奇,因为你用文件中的字段混合表单域。
File fileObject = ...;
MultiPartEntity entity = new MultiPartEntity();
entity.addPart("exampleField", new StringBody("exampleValue")); // probably need to URL encode Strings
entity.addPart("exampleFile", new FileBody(fileObject));
httpPost.setEntity(entity);
0
下载,其中包括了Apache httpmime-4.0.1.jar和Apache的mime4j-0.6.jar。之后,通过发布请求发送文件非常简单。
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://url.to.your/html-form.php");
try {
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("file", new FileBody(new File("/sdcard/my_file_to_upload.jpg")));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
Log.e(this.getClass().getSimpleName(), response.toString());
} catch (IOException e) {
e.printStackTrace();
}
相关问题
- 1. 用Java发送POST数据
- 2. 用Java发送POST数据
- 3. 发送POST数据
- 4. 发送POST数据
- 5. POST请求发送json数据java HttpUrlConnection
- 6. Java发送POST数据(POST数据集但空)
- 7. 从AJAX POST获取JSON数据发送
- 8. 从日历发送POST数据到json_events.php
- 9. 从AJAX Post发送安全数据
- 10. 发送POST数据安全地从iPhone
- 11. 发送POST数据,卷曲和PHP
- 12. TTURL请求和发送POST数据
- 13. 使用post方法将数据从java android发送到webserver
- 14. 发送Post方法从Android客户端到Java Servlet的数据
- 15. PHP不发送POST数据
- 16. Python urllib2发送POST数据
- 17. IE不发送POST数据
- 18. Jquery .post不发送数据
- 19. Ajax post不发送数据
- 20. RestKit不发送post数据
- 21. response.writeHead不发送POST数据
- 22. 发送大量POST数据
- 23. Siege不发送POST数据
- 24. libcurl HTTPS POST数据发送?
- 25. POST数据不发送
- 26. 重新发送POST数据
- 27. cURL不发送POST数据
- 28. CakePHP发送POST数据
- 29. xmlHTTPRequest POST不发送数据
- 30. 提取 - 发送POST数据
你是什么意思的“PHP表单”?将数据发送到PHP应用程序的HTML表单? – 2011-02-28 20:00:51
是的我有一个PHP应用程序,处理来自HTML表格的数据输入 – nmock 2011-02-28 20:10:19