mTask = new MyTask(); mTask.execute(filePath, url);
private class MyTask extends AsyncTask<String, Integer, String>{
@Override
protected void onPostExecute(String result) {
//最终结果的显示
mTvProgress.setText(result);
}
@Override
protected void onPreExecute() {
//开始前的准备工作
mTvProgress.setText("loading...");
}
@Override
protected void onProgressUpdate(Integer... values) {
//显示进度
mPgBar.setProgress(values[0]);
mTvProgress.setText("loading..." + values[0] + "%");
}
@Override
protected String doInBackground(String... params) {
//这里params[0]和params[1]是execute传入的两个参数
String filePath = params[0];
String uploadUrl = params[1];
//下面即手机端上传文件的代码
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try {
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setConnectTimeout(6*1000);
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=\"file\"; filename=\""
+ filePath.substring(filePath.lastIndexOf("/") + 1)
+ "\"" + end);
dos.writeBytes(end);
//获取文件总大小
FileInputStream fis = new FileInputStream(filePath);
long total = fis.available();
byte[] buffer = new byte[8192]; // 8k
int count = 0;
int length = 0;
while ((count = fis.read(buffer)) != -1) {
dos.write(buffer, 0, count);
//获取进度,调用publishProgress()
length += count;
publishProgress((int) ((length / (float) total) * 100));
//这里是测试时为了演示进度,休眠500毫秒,正常应去掉
Thread.sleep(500);
}
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);
@SuppressWarnings("unused")
String result = br.readLine();
dos.close();
is.close();
return "上传成功";
}catch (Exception e) {
e.printStackTrace();
return "上传失败";
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有