0
嗨,我需要将文件发送到服务器,我使用此代码Android的上传文件到服务器使用FileEntity并没有使用MultipartEntity
public void PostFile() {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
// httpclient.
HttpPost httppost = new HttpPost("http://fastcalc.orionsource.ru/test.php");
File file = cache.getFileFromCache("citys.json");
FileEntity reqEntity = new FileEntity(file,"text/plain");
reqEntity.setContentType("text/plain");
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response;
try {
response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
}
在服务器端代码如下
<?php
print 'Files:';
print_r($_FILES);
print '<br><br>';
print "Response:\n";
print_r($_REQUEST);
print '<br><br>';
?>
而在Android上,我得到回应
05-29 17:26:29.232: I/System.out(26823): executing request POST http://fastcalc.orionsource.ru/test.php HTTP/1.1
05-29 17:26:29.732: I/System.out(26823): HTTP/1.1 200 OK
05-29 17:26:29.732: I/System.out(26823): Files:Array
05-29 17:26:29.732: I/System.out(26823): (
05-29 17:26:29.732: I/System.out(26823):)
05-29 17:26:29.732: I/System.out(26823): <br><br>Response:
05-29 17:26:29.732: I/System.out(26823): Array
05-29 17:26:29.732: I/System.out(26823): (
05-29 17:26:29.732: I/System.out(26823):)
05-29 17:26:29.732: I/System.out(26823): <br><br>
我不能使用MultipartEntity,因为它不包含在任何Android版本中。它是第三部分图书馆。
有人给请链接到只使用FileEntity的教程。
是它的工作,但我需要在服务器端对PHP执行base64_decode。我不需要这个,因为我想使用一个HTML POST和Android的表单,HTML使用字段来发送文件。 –