欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-06-14 09:32:31
android上传文件到服务器,通常采用构造http协议的方法,模拟网页post方法传输文件,服务器端可以采用javaservlet或者php来接收要传输的文件。使用jav...

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

<?php 
$target_path = "./upload/";//接收文件目录 
$target_path = $target_path . basename( $_files['uploadedfile']['name']); 
if(move_uploaded_file($_files['uploadedfile']['tmp_name'], $target_path)) { 
  echo "the file ". basename( $_files['uploadedfile']['name']). " has been uploaded"; 
} else{ 
  echo "there was an error uploading the file, please try again!" . $_files['uploadedfile']['error']; 
} 
?> 

手机客户端代码:

package com.figo.uploadfile; 
 
import java.io.bufferedreader; 
import java.io.dataoutputstream; 
import java.io.fileinputstream; 
import java.io.inputstream; 
import java.io.inputstreamreader; 
import java.net.httpurlconnection; 
import java.net.url; 
import android.app.activity; 
import android.os.bundle; 
import android.view.view; 
import android.widget.button; 
import android.widget.textview; 
import android.widget.toast; 
 
public class uploadfileactivity extends activity 
{ 
 // 要上传的文件路径,理论上可以传输任何文件,实际使用时根据需要处理 
 private string uploadfile = "/sdcard/testimg.jpg"; 
 private string srcpath = "/sdcard/testimg.jpg"; 
 // 服务器上接收文件的处理页面,这里根据需要换成自己的 
 private string actionurl = "http://10.100.1.208/receive_file.php"; 
 private textview mtext1; 
 private textview mtext2; 
 private button mbutton; 
 
 @override 
 public void oncreate(bundle savedinstancestate) 
 { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.main); 
 
  mtext1 = (textview) findviewbyid(r.id.mytext2); 
  mtext1.settext("文件路径:\n" + uploadfile); 
  mtext2 = (textview) findviewbyid(r.id.mytext3); 
  mtext2.settext("上传网址:\n" + actionurl); 
  /* 设置mbutton的onclick事件处理 */ 
  mbutton = (button) findviewbyid(r.id.mybutton); 
  mbutton.setonclicklistener(new view.onclicklistener() 
  { 
   @override 
   public void onclick(view v) 
   { 
    uploadfile(actionurl); 
   } 
  }); 
 } 
 
 /* 上传文件至server,uploadurl:接收文件的处理页面 */ 
 private void uploadfile(string uploadurl) 
 { 
  string end = "\r\n"; 
  string twohyphens = "--"; 
  string boundary = "******"; 
  try 
  { 
   url url = new url(uploadurl); 
   httpurlconnection httpurlconnection = (httpurlconnection) url 
     .openconnection(); 
   // 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃 
   // 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 http 请求正文的流。 
   httpurlconnection.setchunkedstreamingmode(128 * 1024);// 128k 
   // 允许输入输出流 
   httpurlconnection.setdoinput(true); 
   httpurlconnection.setdooutput(true); 
   httpurlconnection.setusecaches(false); 
   // 使用post方法 
   httpurlconnection.setrequestmethod("post"); 
   httpurlconnection.setrequestproperty("connection", "keep-alive"); 
   httpurlconnection.setrequestproperty("charset", "utf-8"); 
   httpurlconnection.setrequestproperty("content-type", 
     "multipart/form-data;boundary=" + boundary); 
 
   dataoutputstream dos = new dataoutputstream( 
     httpurlconnection.getoutputstream()); 
   dos.writebytes(twohyphens + boundary + end); 
   dos.writebytes("content-disposition: form-data; name=\"uploadedfile\"; filename=\"" 
     + srcpath.substring(srcpath.lastindexof("/") + 1) 
     + "\"" 
     + end); 
   dos.writebytes(end); 
 
   fileinputstream fis = new fileinputstream(srcpath); 
   byte[] buffer = new byte[8192]; // 8k 
   int count = 0; 
   // 读取文件 
   while ((count = fis.read(buffer)) != -1) 
   { 
    dos.write(buffer, 0, count); 
   } 
   fis.close(); 
 
   dos.writebytes(end); 
   dos.writebytes(twohyphens + boundary + twohyphens + end); 
   dos.flush(); 
 
   inputstream is = httpurlconnection.getinputstream(); 
   inputstreamreader isr = new inputstreamreader(is, "utf-8"); 
   bufferedreader br = new bufferedreader(isr); 
   string result = br.readline(); 
 
   toast.maketext(this, result, toast.length_long).show(); 
   dos.close(); 
   is.close(); 
 
  } catch (exception e) 
  { 
   e.printstacktrace(); 
   settitle(e.getmessage()); 
  } 
 } 
} 

在androidmanifest.xml文件里添加网络访问权限:
<uses-permission android:name="android.permission.internet" /> 

运行结果:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。