开发中有时有这么一个需求,要求对文件进行上传或下载,但是在上传或者下载前,你需要给用户一个友好的提示,在上传或者下载中,你需要将进度展示给用户,下载或者完成后提示用户下载完成。
这里给出服务器端(我用的jsp)和Android客户端的代码的github地址
JSP服务器端
https://github.com/zhouxu88/UploadFileWebDemo.git
Android客户端
https://github.com/zhouxu88/OkHttp3_UploadFile.git
一、多文件上传(不带进度)
生成RequestBody
/**
* 通过上传的文件的完整路径生成RequestBody
* @param fileNames 完整的文件路径
* @return
*/
private static RequestBody getRequestBody(List fileNames) {
//创建MultipartBody.Builder,用于添加请求的数据
MultipartBody.Builder builder = new MultipartBody.Builder();
for (int i = 0; i < fileNames.size(); i++) { //对文件进行遍历
File file = new File(fileNames.get(i)); //生成文件
//根据文件的后缀名,获得文件类型
String fileType = getMimeType(file.getName());
builder.addFormDataPart( //给Builder添加上传的文件
"image", //请求的名字
file.getName(), //文件的文字,服务器端用来解析的
RequestBody.create(MediaType.parse(fileType), file) //创建RequestBody,把上传的文件放入
);
}
return builder.build(); //根据Builder创建请求
}
生成Request
/**
* 获得Request实例
* @param url
* @param fileNames 完整的文件路径
* @return
*/
private static Request getRequest(String url, List fileNames) {
Request.Builder builder = new Request.Builder();
builder.url(url)
.post(getRequestBody(fileNames));
return builder.build();
}
上传文件
/**
* 根据url,发送异步Post请求
* @param url 提交到服务器的地址
* @param fileNames 完整的上传的文件的路径名
* @param callback OkHttp的回调接口
*/
public static void upLoadFile(String url,List fileNames,Callback callback){
OkHttpClient okHttpClient = new OkHttpClient();
Call call = okHttpClient.newCall(getRequest(url,fileNames)) ;
call.enqueue(callback);
}
二、多文件上传(带进度值)
由于对进度的处理,比较复杂,此处只做使用的介绍,具体的实现可在github上查看
1、对于进度已经封装成libary,直接调用即可
2、带进度的多文件上传的调用如下
MainActivity:(关键代码)
//提交文件到服务器的地址(使用的时候替换成自己的服务器地址)
private static final String POST_FILE_URL = "http://192.168.1.3:8080/UploadFileDemo/MutilUploadServlet";
private ProgressBar uploadProgress, downloadProgeress;
private TextView uploadTV,downloadTv;
//多文件上传(带进度)
private void upload() {
//这个是非ui线程回调,不可直接操作UI
final ProgressListener progressListener = new ProgressListener() {
@Override
public void onProgress(long bytesWrite, long contentLength, boolean done) {
Log.i("TAG", "bytesWrite:" + bytesWrite);
Log.i("TAG", "contentLength" + contentLength);
Log.i("TAG", (100 * bytesWrite) / contentLength + " % done ");
Log.i("TAG", "done:" + done);
Log.i("TAG", "================================");
}
};
//这个是ui线程回调,可直接操作UI
UIProgressListener uiProgressRequestListener = new UIProgressListener() {
@Override
public void onUIProgress(long bytesWrite, long contentLength, boolean done) {
Log.i("TAG", "bytesWrite:" + bytesWrite);
Log.i("TAG", "contentLength" + contentLength);
Log.i("TAG", (100 * bytesWrite) / contentLength + " % done ");
Log.i("TAG", "done:" + done);
Log.i("TAG", "================================");
//ui层回调,设置当前上传的进度值
int progress = (int) ((100 * bytesWrite) / contentLength);
uploadProgress.setProgress(progress);
uploadTV.setText("上传进度值:" + progress + "%");
}
//上传开始
@Override
public void onUIStart(long bytesWrite, long contentLength, boolean done) {
super.onUIStart(bytesWrite, contentLength, done);
Toast.makeText(getApplicationContext(),"开始下载",Toast.LENGTH_SHORT).show();
}
//上传结束
@Override
public void onUIFinish(long bytesWrite, long contentLength, boolean done) {
super.onUIFinish(bytesWrite, contentLength, done);
//uploadProgress.setVisibility(View.GONE); //设置进度条不可见
Toast.makeText(getApplicationContext(),"下载结束",Toast.LENGTH_SHORT).show();
}
};
//开始Post请求
OKHttpUtils.doPostRequest(POST_FILE_URL, initUploadFile(), uiProgressRequestListener, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("TAG", "error------> "+e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("TAG", "success---->"+response.body().string());
}
});
//初始化上传文件的数据
private List initUploadFile(){
List fileNames = new ArrayList<>();
fileNames.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
+ File.separator + "test.txt"); //txt文件
fileNames.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
+ File.separator + "bell.png"); //图片
fileNames.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)
+ File.separator + "kobe.mp4"); //视频
fileNames.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
+ File.separator + "xinnian.mp3"); //音乐
return fileNames;
}
}
效果图