php服务端接受程序。。file_up.php。
/* require_once('lib/session_config.php');
require_once('lib/flydc.php');
require_once('lib/common.php');
require_once('config.php'); */
//header("content-Type:text/html;charset=UTF-8");
// var_dump($_SERVER['HTTP_RANGE']);
$fileUp = new FileUp();
$fileUp->uploadFile();
exit;
class FileUp
{
public $isHaveFid = false;
public $target_path = "../files/"; //文件存取的路径
public $uid,$fid,$ext,$alllength,$poss,$finish;
function initData()
{
$this->fid = $_GET['fid'];
$this->ext = $_GET['ext'];
$this->poss = $_GET['pos'];
}
function uploadFile()
{
$this->initData();
$absoluteName = "";//$this->getdir()."/".basename($_FILES['uploadedfile']['name']);
$handleRead = null;
$fid = "";
$handleWrite = null;
if(!empty($_FILES['uploadedfile']['tmp_name']))
{
$handleRead = fopen($_FILES['uploadedfile']['tmp_name'],'rb');
//$extend = pathinfo( $_FILES[$name]['name'] ); //$extend['extension'] 扩展
if(!empty($this->fid)) //fid存在是接着上次上传
$fid = $this->fid;
else //fid不存在,做为第一次上传,生成一个fid
$fid = time().'_'.mt_rand(1,22222).".".$this->ext;
$absoluteName = $this->getdir()."/".$fid;
$handleWrite = fopen($absoluteName,'a');
$buffer = '';
while (!feof($handleRead))
{
$buffer = fread($handleRead, 1024*128);
if(strlen($buffer)<=0)
break;
fwrite($handleWrite,$buffer);
}
fclose($handleWrite);
fclose($handleRead);
echo $fid; //返回fid 给服务器
$this->saveLog("$fid 上传成功");
}else
{
echo "fail";
$this->saveLog(" 上传失败");
}
}
function saveLog($content)
{
$logpath = $this->getdir()."/".date("Y-m-d",time())."_log.txt";
$result = fopen($logpath,'a');
fwrite($result,date("Y-m-d H:i:s",time())." ========== ".$content."\r\n");
fclose($result);
}
function getdir()
{
$day_dir = $this->target_path.date("Ymd",time());
if(!is_dir($day_dir))
{
mkdir($day_dir,0777,true);
}
return $day_dir;
}
}
?>
androiud 客户端java 代码
public void doUpload()
{
//要上传的文件
String pathString = FileManager.getParentDirectory()+"media/video_3_20141222145045024.mp4"; //video_3_20141222145045024.mp4 video_3_20141224153340976.mp4
//上传的地址
String acceptUrl = "http://10.0.10.3/flyguard/mobileapi/file_up.php?fid="+this.fidString+"&pos=&ext=mp4";
RandomAccessFile raf = null;
try
{
raf = new RandomAccessFile(pathString, "r");
long alllength=raf.length();
raf.seek(0);
byte[] buffer = new byte[128*1024];//128k
int count = 0;
while ((count = raf.read(buffer)) != -1)
{
// count = raf.read(buffer);
String result = uploadFil(acceptUrl,buffer);
System.out.println("MediaActivity doUpload return:"+result+ " count:"+count);
break;
}
} catch (Exception e)
{
e.printStackTrace();
}finally{
try
{
if(raf!=null)
raf.close();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String uploadFil(String acceptUrl,byte[] data)
{
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try
{
URL url = new URL(acceptUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
// 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃
// 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
httpURLConnection.setChunkedStreamingMode(data.length);// 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);//application/octet-stream multipart/form-data
DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
// +pathString.substring(pathString.lastIndexOf("/")+1)
+"myfilename"
+"\""
+end);
dos.writeBytes(end);
dos.write(data,0,data.length);
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
String reponse = "";
if(httpURLConnection.getResponseCode() == 200 )
{
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is,"utf-8");
BufferedReader br = new BufferedReader(isr);
while (null !=br.readLine())
{
reponse +=br.readLine();
}
is.close();
}
System.out.println("MediaActivity uploadFil Reponse:"+reponse);
dos.close();
return reponse;
} catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("MediaActivity uploadFil Exception:"+e.getMessage());
}
return "";
}
android上传文件到服务器
package com.spring.sky.image.upload.network; import java.io.DataOutputStream; import java.io.File; i ...
android -上传文件到服务器
android上传文件到服务器       重点:最好是设置好content-type这些参数的配置!     package com.spring.sky.image.upload.network; ...
android 上传文件
android对于上传文件

,还是非常easy的,和java里面的上传都是一样的,基本上都是熟悉操作输出流和输入流!另一个特别重要的就是须要一些content-type这些參数的配置!  假设这些都弄好 ...

android上传文件到wamp服务器

1.php server(wamp)部分 建立unload.php页面代码如下 <?php move_uploaded_file($_FILES["file1"][&quot ...

Android上传文件至服务器&lpar;上&rpar;

每一次都不能上首页,真悲催..管理员让我上一次首页? 很多时候我更愿意一个人写代码,与其在垃圾代码上改改改,我更愿意直接重构. 整洁的代码简单直接.整洁的代码如同优美的散文.整洁的代码从不隐藏设计者的 ...

【经验记录】Android上传文件到服务器

Android中实现上传文件,其实是很简单的,和在java里面是一样的,基本上都是熟悉操作输出流和输入流!还有一个特别重要的就是需要配置content-type的一些参数!如果这些都弄好了,上传就很简 ...

Android上传文件到服务器(转)

Android中实现上传文件,其实是很简单的,和在java里面是一样的,基本上都是熟悉操作输出流和输入流!还有一个特别重要的就是需要配置content-type的一些参数!如果这些都弄好了,上传就很简 ...

Android上传文件之FTP

android客户端实现FTP文件(包括图片)上传应该没什么难度.写下来就了为了记录一下,望能帮到新手. 需要用到 commons-net-3.0.1.jar,后面附上jar包. 直接上代码: /** ...

Android 上传文件到 FTP 服务器

实现背景 近期接触到一个需求,就是将文件从Android系统上传到FTP服务器,虽然之前接触过FTP服务器,了解基本的使用流程,但是将此流程从使用习惯转化为代码实现还是有一定难度的.但是基本的流程还是 ...