android原生POST、httpClient4.X实现向PHP服务器上传文件
前言:虽然网上代码一堆,相对减轻了尝试的复杂程度,但真正运行起来还是有各种问题的,没有源码直接运行是一个很大问题,我尝试了一天之后,终于弄懂了些门路,分享给大家,对于原生POST方式上传,因为我也不会用这种方式上传文件,效率低,不好用,所以我
前言:虽然网上代码一堆,相对减轻了尝试的复杂程度,但真正运行起来还是有各种问题的,没有源码直接运行是一个很大问题,我尝试了一天之后,终于弄懂了些门路,分享给大家,对于原生POST方式上传,因为我也不会用这种方式上传文件,效率低,不好用,所以我也没有深究具体代码及原理,但我给出的代码是可行的,我们先从PHP在浏览器中上传图片到服务器开始,然后逐步讲解通过android是如何上传的,android的那些参数该如何构造。
一、通过网页上传图片到PHP服务器
具体讲解参考我这篇文章《不刷新实现图片上传功能》,里面源码啥啥的都有,下面我只贴出前后台部分代码,讲解一个问题
前台部分代码:
1 2 3 4 5 6 7 8 9 10 11 |
|
注意一个input控件type='file',name='upfile'
后台部分代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
这里注意一个变量:$_FILES['upfile']['name'],$_FILES的第一个参数是upfile,表示就是接收我们上面定义的 ,所传上来的文件
二、android原生POST上传文件
基本用不到,不细讲了,直接看源码吧(源码在最后)
三、httpClient 4.X实现向PHP服务器上传文件
上效果图:
android端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
1 2 3 4 5 |
|
2、下句:entity.addPart("uploadedfile", fileBody);这句,把我们的fileBody加入到表单中,而前面的uploadedfile就相当于input type=file name='xxx'的name字段的值,这就是为什么在PHP接收中,$_FILES['uploadedfile']['name'],这个$_FILES第一个参数要与entity.addPart("uploadedfile", fileBody);的第一个参数一样的原因;
看服务器端(PHP):
1 2 3 4 5 6 7 |
|
难度不大,只注意一点:$_FILES['uploadedfile']['tmp_name']的第一个参数要与android代码中的entity.addPart("uploadedfile", fileBody);第一个参数名保持一致;
在源码中,这对这种方法稍微进行了扩充,同时上传两个图片到PHP服务器;
上源码啦:http://download.csdn.net/detail/harvic880925/6769671(不要分,仅供分享)
实例运行方法:
1、使用真机测试,使服务器与真机处于同一局域网中;
2、将Desert.jpg和1.jpg复制到手机sdcard根目录下,即Desert.jpg所处的位置为:sdcard/Desert.jpg和sdcard/1.jpg;
3、将receive_file.php,放在Apache服务器htdocs目录下;
4、将android代码中的:222.195.151.19,改成自己电脑PHP服务器的地址;
http://blog.csdn.net/harvic880925/article/details/17565481

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

OnePlus'sister brand iQOO has a 2023-4 product cycle that might be nearlyover; nevertheless, the brand has declared that it is not done with itsZ9series just yet. Its final, and possibly highest-end,Turbo+variant has just beenannouncedas predicted. T
