Android上传文件到Web服务器,PHP接收文件

简介:  Android上传文件到服务器,通常采用构造http协议的方法,模拟网页POST方法传输文件,服务器端可以采用JavaServlet或者PHP来接收要传输的文件。

 Android上传文件到服务器,通常采用构造http协议的方法,模拟网页POST方法传输文件,服务器端可以采用JavaServlet或者PHP来接收要传输的文件。使用JavaServlet来接收文件的方法比较常见,在这里给大家介绍一个简单的服务器端使用PHP语言来接收文件的例子。

服务器端代码比较简单,接收传输过来的文件:

[php]  view plain  copy
  1. <?php  
  2. $target_path  = "./upload/";//接收文件目录  
  3. $target_path = $target_path . basename$_FILES['uploadedfile']['name']);  
  4. if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {  
  5.    echo "The file ".  basename$_FILES['uploadedfile']['name']). " has been uploaded";  
  6. }  else{  
  7.    echo "There was an error uploading the file, please try again!" . $_FILES['uploadedfile']['error'];  
  8. }  
  9. ?>  

手机客户端代码:

[java]  view plain  copy
  1. package com.figo.uploadfile;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.DataOutputStream;  
  5. import java.io.FileInputStream;  
  6. import java.io.InputStream;  
  7. import java.io.InputStreamReader;  
  8. import java.net.HttpURLConnection;  
  9. import java.net.URL;  
  10. import android.app.Activity;  
  11. import android.os.Bundle;  
  12. import android.view.View;  
  13. import android.widget.Button;  
  14. import android.widget.TextView;  
  15. import android.widget.Toast;  
  16.   
  17. public class UploadfileActivity extends Activity  
  18. {  
  19.   // 要上传的文件路径,理论上可以传输任何文件,实际使用时根据需要处理  
  20.   private String uploadFile = "/sdcard/testimg.jpg";  
  21.   private String srcPath = "/sdcard/testimg.jpg";  
  22.   // 服务器上接收文件的处理页面,这里根据需要换成自己的  
  23.   private String actionUrl = "http://10.100.1.208/receive_file.php";  
  24.   private TextView mText1;  
  25.   private TextView mText2;  
  26.   private Button mButton;  
  27.   
  28.   @Override  
  29.   public void onCreate(Bundle savedInstanceState)  
  30.   {  
  31.     super.onCreate(savedInstanceState);  
  32.     setContentView(R.layout.main);  
  33.   
  34.     mText1 = (TextView) findViewById(R.id.myText2);  
  35.     mText1.setText("文件路径:\n" + uploadFile);  
  36.     mText2 = (TextView) findViewById(R.id.myText3);  
  37.     mText2.setText("上传网址:\n" + actionUrl);  
  38.     /* 设置mButton的onClick事件处理 */  
  39.     mButton = (Button) findViewById(R.id.myButton);  
  40.     mButton.setOnClickListener(new View.OnClickListener()  
  41.     {  
  42.       @Override  
  43.       public void onClick(View v)  
  44.       {  
  45.         uploadFile(actionUrl);  
  46.       }  
  47.     });  
  48.   }  
  49.   
  50.   /* 上传文件至Server,uploadUrl:接收文件的处理页面 */  
  51.   private void uploadFile(String uploadUrl)  
  52.   {  
  53.     String end = "\r\n";  
  54.     String twoHyphens = "--";  
  55.     String boundary = "******";  
  56.     try  
  57.     {  
  58.       URL url = new URL(uploadUrl);  
  59.       HttpURLConnection httpURLConnection = (HttpURLConnection) url  
  60.           .openConnection();  
  61.       // 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃  
  62.       // 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。  
  63.       httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K  
  64.       // 允许输入输出流  
  65.       httpURLConnection.setDoInput(true);  
  66.       httpURLConnection.setDoOutput(true);  
  67.       httpURLConnection.setUseCaches(false);  
  68.       // 使用POST方法  
  69.       httpURLConnection.setRequestMethod("POST");  
  70.       httpURLConnection.setRequestProperty("Connection""Keep-Alive");  
  71.       httpURLConnection.setRequestProperty("Charset""UTF-8");  
  72.       httpURLConnection.setRequestProperty("Content-Type",  
  73.           "multipart/form-data;boundary=" + boundary);  
  74.   
  75.       DataOutputStream dos = new DataOutputStream(  
  76.           httpURLConnection.getOutputStream());  
  77.       dos.writeBytes(twoHyphens + boundary + end);  
  78.       dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""  
  79.           + srcPath.substring(srcPath.lastIndexOf("/") + 1)  
  80.           + "\""  
  81.           + end);  
  82.       dos.writeBytes(end);  
  83.   
  84.       FileInputStream fis = new FileInputStream(srcPath);  
  85.       byte[] buffer = new byte[8192]; // 8k  
  86.       int count = 0;  
  87.       // 读取文件  
  88.       while ((count = fis.read(buffer)) != -1)  
  89.       {  
  90.         dos.write(buffer, 0, count);  
  91.       }  
  92.       fis.close();  
  93.   
  94.       dos.writeBytes(end);  
  95.       dos.writeBytes(twoHyphens + boundary + twoHyphens + end);  
  96.       dos.flush();  
  97.   
  98.       InputStream is = httpURLConnection.getInputStream();  
  99.       InputStreamReader isr = new InputStreamReader(is, "utf-8");  
  100.       BufferedReader br = new BufferedReader(isr);  
  101.       String result = br.readLine();  
  102.   
  103.       Toast.makeText(this, result, Toast.LENGTH_LONG).show();  
  104.       dos.close();  
  105.       is.close();  
  106.   
  107.     } catch (Exception e)  
  108.     {  
  109.       e.printStackTrace();  
  110.       setTitle(e.getMessage());  
  111.     }  
  112.   }  
  113. }  

在AndroidManifest.xml文件里添加网络访问权限:

[plain]  view plain  copy
  1. <uses-permission android:name="android.permission.INTERNET" />  

运行结果:



目录
相关文章
|
1月前
|
存储 资源调度 应用服务中间件
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
浅谈本地开发好的 Web 应用部署到 ABAP 应用服务器上的几种方式
26 0
|
1月前
thinkphp5.1隐藏index.php入口文件
thinkphp5.1隐藏index.php入口文件
30 0
thinkphp5.1隐藏index.php入口文件
|
2月前
|
Android开发
安卓SO层开发 -- 编译指定平台的SO文件
安卓SO层开发 -- 编译指定平台的SO文件
31 0
|
2月前
|
数据可视化 Shell Linux
shell+crontab+gitlab实现ecs服务器文件的web展示
本文通过把ecs服务器上的文件定时上传至gitlab,实现文件的页面可视化和修改历史。技术点:shell、crontab、gitlab。
50 3
|
2月前
|
负载均衡 应用服务中间件 API
什么是 Web 服务器领域的 openresty
什么是 Web 服务器领域的 openresty
42 0
|
1月前
|
缓存 网络协议 数据可视化
WinSCP下载安装并实现远程SSH本地服务器上传文件
WinSCP下载安装并实现远程SSH本地服务器上传文件
|
1天前
|
安全 Android开发
Android 预置可卸载分区接收不到任何广播问题分析和解决
Android 预置可卸载分区接收不到任何广播问题分析和解决
6 0
|
1月前
|
应用服务中间件 Apache nginx
web后端-web服务器对比
web后端-web服务器对比
|
2月前
|
安全 网络安全 开发者
如何在OpenWRT部署uhttpd搭建服务器实现远程访问本地web站点
如何在OpenWRT部署uhttpd搭建服务器实现远程访问本地web站点
112 0
|
2月前
|
JSON Java Go