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