1.先列出Android端使用的library:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
dependencies {
...
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.squareup:otto:1.3.5'
compile 'cn.finalteam:galleryfinal:1.4.8.4'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.jcodecraeer:xrecyclerview:1.2.6'
compile 'com.baoyz.actionsheet:library:1.1.5'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'io.reactivex:rxjava:1.0.6'
compile 'io.reactivex:rxandroid:0.23.0'
compile files('libs/fastjson-1.2.7.jar')
}
Android端代码:
1 | public interface ApiService { |
1 | public class Client { |
上传关键代码: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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44//使用MultipartTypedOutput类来上传多张图片
public void uploadFiles(List<String> paths) {
MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput();
for (String imgPath : paths){
//注意“uploadfile[]”一定要带“[]”,表明上传的是数组,也就是多张图片,不能写“uploadfile”
multipartTypedOutput.addPart("uploadfile[]", new TypedFile("", new File(imgPath)));
}
mApi.uploadFiles(multipartTypedOutput)
.subscribeOn(Schedulers.computation())
.subscribe(response -> handleUploadFile(response), error -> handleFailure(error));
}
private void handleUploadFile(Response response) {
System.out.println(response.getUrl());
try {
InputStream in = response.getBody().in();
String responseString = inputStream2String(in);
Log.i("AAA", responseString);
//解析json数据
//otto事件传递
mBus.post(responseString);
} catch (IOException e) {
e.printStackTrace();
}
}
//接收事件代码
//需要注解@Subscribe ,表明在这个函数接收数据
public void uploadFileResponse(String response) {
Message msg = JSON.parseObject(response, Message.class);
List<String> imgUrls = msg.getImgUrls();
if (msg.getCode() == 1) {
if (choosePhotoListAdapter == null) {
choosePhotoListAdapter = new ChoosePhotoListAdapter(this, null, imgUrls);
lvPhotoShow.setAdapter(choosePhotoListAdapter);
}
choosePhotoListAdapter.notifyDataSetChanged();
}
}
Php端代码: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
26
27
28
29
30
31
32
33
34
35
36
37
38//初学php,写的比较简单,不太健壮,像图片是否已经上传过,都没有判断,大家自己补充啊^_^
/**
* Created by PhpStorm.
* User: zhangyipeng
* Date: 16/2/18
* Time: 下午3:48
*/
$base_path = "./upload_file/"; //接收文件目录
$imgs = array(); //定义一个数组存放上传图片的路径
$isSave = false;
if (!file_exists($base_path)) {
mkdir($base_path);
}
foreach ($_FILES["uploadfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["uploadfile"]["tmp_name"][$key];
$name = $_FILES["uploadfile"]["name"][$key];
$uploadfile = $base_path . $name;
$isSave = move_uploaded_file($tmp_name, $uploadfile);
if ($isSave){
$imgs[]=$uploadfile;
}
}
}
if ($isSave) {
$array = array("code" => "1", "message" =>"上传图片成功"
, "imgUrls" => $imgs);
echo json_encode($array);
} else {
$array = array("code" => "0", "message" => "上传图片失败," . $_FILES ['uploadfile'] ['error']
, "imgUrls" => $imgs);
echo json_encode($array);
}
其实上传成功的关键是part需要带uploadfile[] 。1
multipartTypedOutput.addPart("uploadfile[]", new TypedFile("", new File(imgPath)));
我在这个地方失败了几十次,无意中看见html网页批量上传的name属性中带了[],我就试了试,果然成功了。
补充:php环境使用的是MAMP,文件所在目录如下,想要测试的可以看看
1.超级简单的Android Studio jni 实现(无需命令行)
2.让Android开发者相见恨晚的软件及插件
3.GitHub上一些超炫的Android开源项目推荐