2012-05-29 26 views
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的教程。

回答

0

嗨,我已经尝试使用映像文件在服务器上发送。你可以做一件事,你的文件转换成的base64字符串,并将其发送到服务器和从服务器端尝试转换的base64到文件

参考如何转换为Base64 SES我的回答:how to send the image in an ImageView to a php server?

+0

是它的工作,但我需要在服务器端对PHP执行base64_decode。我不需要这个,因为我想使用一个HTML POST和Android的表单,HTML使用字段来发送文件。 –

相关问题