android 文件上传与下载(带进度条)

来源:互联网 发布:软件许可使用证书 编辑:程序博客网 时间:2024/05/22 06:24
原文地址:http://write.blog.csdn.net/postedit

   

    privatevoiduploadAttachment(finalString path) {
              File file =newFile(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";

              // Stringurl
              // ="http://192.168.0.139:8080/app/worklog/addOrUpdateWorkLog.htm";
              UserInfo userInfo =jyBoxApplication.getUserInfo();
              String keyCode =jyBoxApplication.getKeyCode();
              RequestParams params =newRequestParams();

              params.put("keyCode", keyCode);
              params.put("devicesType", 2);
              params.put("person_id", userInfo.getPersonId());
              try{
                     params.put("file", file);
              }catch(FileNotFoundException e1) {
                     //TODOAuto-generated catch block
                     e1.printStackTrace();
              }
              params.put("type", 1);

              HttpAsyncUtil.post(url, params,newJsonHttpResponseHandler() {
                     @Override
                     publicvoidonSuccess(intstatusCode, Header[] headers,
                                  JSONObject response) {

                           try{
                                  intstatus = response.getInt("status");
                                  if(status == 1) {
                                         progress.dismiss();
                                         Toast.makeText(EditWorkDiaryActivity.this,"上传成功",
                                                       Toast.LENGTH_SHORT).show();

                                         JSONObject dataJsonObject = response
                                                       .getJSONObject("data");
                                         longid = 0;
                                         if(dataJsonObject !=null) {
                                                String attachmentId = dataJsonObject
                                                              .getString("attachmentId");
                                                id = Long.valueOf(attachmentId);
                                         }

                                         String fileName = path.substring(path.lastIndexOf("/") + 1);
                                         Attachment attachment =newAttachment();
                                         attachment.setName(fileName);
                                         attachment.setPath(path);
                                         attachment.setId(id);
                                         attachmentList.add(attachment);
                                         attachmentAdapter.notifyDataSetChanged();

                                         intheight = 70 *attachmentList.size();
                                         ViewGroup.LayoutParams params =attmentListView
                                                       .getLayoutParams();
                                         DisplayMetrics metrics =newDisplayMetrics();
                                         getWindowManager().getDefaultDisplay().getMetrics(
                                                       metrics);
                                         intheighDP = 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();
                                  //TODOAuto-generated catch block
                                  e.printStackTrace();
                           }

                     }

                     @Override
                     publicvoidonProgress(longbytesWritten,longtotalSize) {
                           super.onProgress(bytesWritten, totalSize);
                           intcount = (int) ((bytesWritten * 1.0 / totalSize) * 100);
                           // 上传进度显示
                           progress.setProgress(count);
                           Log.e("上传 Progress>>>>>", bytesWritten + " / " + totalSize);
                     }

                     @Override
                     publicvoidonFailure(intstatusCode, Header[] headers,
                                  String responseString, Throwable throwable) {
                           progress.dismiss();
                           Toast.makeText(EditWorkDiaryActivity.this,"上传失败",
                                         Toast.LENGTH_SHORT).show();
                     }
              });

       }

1.通过 newAsyncHttpClient().post(urlString, params, res);  直接可上传文件
回调方法  onProgress 可计算上传的百分比

2.初始化进度条组件
                                  progress=newProgressDialog(EditWorkDiaryActivity.this);
                                  progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                                  progress.setMessage("附件上传");
                                  progress.setOnCancelListener(newDialogInterface.OnCancelListener() {

                                         @Override
                                         publicvoidonCancel(DialogInterface dialog) {
                                                progress.dismiss();
                                                HttpAsyncUtil.cancel(EditWorkDiaryActivity.this,
                                                              true);

                                         }
                                  });
                                  progress.setCancelable(true);
                                  progress.setButton("取消",
                                                newDialogInterface.OnClickListener() {
                                                       @Override
                                                       publicvoidonClick(DialogInterface dialog,
                                                                     intwhich) {
                                                              progress.dismiss();
                                                              HttpAsyncUtil.cancel(
                                                                            EditWorkDiaryActivity.this,true);
                                                       }
                                                });
                                  progress.show();





0 0