原文地址:http://write.blog.csdn.net/postedit
private void uploadAttachment (final String path) {
File file = new File(path);
if (!file.exists()) {
Toast. makeText(EditWorkDiaryActivity.this, "不存在该附件文件" ,
Toast. LENGTH_SHORT).show();
return;
}
String serviceAddress = jyBoxApplication.getServiceAddress();
String url = "http://" + serviceAddress + "/app/worklog/uploadFile.htm" ;
// String url
// ="http://192.168.0.139:8080/app/worklog/addOrUpdateWorkLog.htm";
UserInfo userInfo = jyBoxApplication.getUserInfo();
String keyCode = jyBoxApplication.getKeyCode();
RequestParams params = new RequestParams();
params.put( "keyCode", keyCode);
params.put( "devicesType", 2);
params.put( "person_id", userInfo.getPersonId());
try {
params.put( "file", file);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
params.put( "type", 1);
HttpAsyncUtil. post(url, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
JSONObject response) {
try {
int status = response.getInt("status" );
if (status == 1) {
progress.dismiss();
Toast. makeText(EditWorkDiaryActivity.this, "上传成功" ,
Toast. LENGTH_SHORT).show();
JSONObject dataJsonObject = response
.getJSONObject( "data");
long id = 0;
if (dataJsonObject != null) {
String attachmentId = dataJsonObject
.getString( "attachmentId");
id = Long.valueOf(attachmentId);
}
String fileName = path.substring(path.lastIndexOf("/" ) + 1);
Attachment attachment = new Attachment();
attachment.setName(fileName);
attachment.setPath(path);
attachment.setId(id);
attachmentList.add(attachment);
attachmentAdapter.notifyDataSetChanged();
int height = 70 * attachmentList .size();
ViewGroup.LayoutParams params = attmentListView
.getLayoutParams();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(
metrics);
int heighDP = Utils.getDPI(height, metrics);
params. height = heighDP;
attmentListView.setLayoutParams(params);
attmentListView.setVisibility(View.VISIBLE);
attachmentAdapter.notifyDataSetChanged();
} else {
progress.dismiss();
Toast. makeText(EditWorkDiaryActivity.this, "提交异常" ,
Toast. LENGTH_SHORT).show();
}
} catch (JSONException e) {
progress.dismiss();
Toast. makeText(EditWorkDiaryActivity.this, "系统异常" ,
Toast. LENGTH_SHORT).show();
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onProgress(long bytesWritten, long totalSize) {
super.onProgress(bytesWritten, totalSize);
int count = (int) ((bytesWritten * 1.0 / totalSize) * 100);
// 上传进度显示
progress.setProgress(count);
Log. e("上传 Progress>>>>>", bytesWritten + " / " + totalSize);
}
@Override
public void onFailure(int statusCode, Header[] headers,
String responseString, Throwable throwable) {
progress.dismiss();
Toast. makeText(EditWorkDiaryActivity.this, "上传失败" ,
Toast. LENGTH_SHORT).show();
}
});
}
1.通过 new AsyncHttpClient().post(urlString, params, res); 直接可上传文件
回调方法 onProgress 可计算上传的百分比
2.初始化进度条组件
progress = new ProgressDialog(EditWorkDiaryActivity.this );
progress.setProgressStyle(ProgressDialog. STYLE_HORIZONTAL);
progress.setMessage( "附件上传" );
progress.setOnCancelListener( new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
progress.dismiss();
HttpAsyncUtil. cancel(EditWorkDiaryActivity.this,
true);
}
});
progress.setCancelable( true);
progress. setButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
progress.dismiss();
HttpAsyncUtil. cancel(
EditWorkDiaryActivity. this, true);
}
}) ;
progress.show();
--------------
spring mvc+tomcat源码分析视频 (复制链接在浏览器打开)