这是一种从Android上传文件的简单方法。
String url = "http://yourserver.com/upload.php";
File file = new File("myfileuri");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
} catch (Exception e) {
e.printStackTrace();
}
我想做的是在我的请求中添加更多POST
变量。我该怎么做?当我们在POST
请求中上传普通字符串时,我们使用URLEncodedFormEntity
。
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
在上传文件时,我们使用InputStreamEntity
。
此外,我该如何将文件特定地上传到$_FILES['myfilename']
?