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

原文地址: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源码分析视频 (复制链接在浏览器打开)

https://study.163.com/course/courseMain.htm?share=2&shareId=480000001919582&courseId=1209399899&_trace_c_p_k2_=6d81bc445e9c462ab8d6345e40f6b0bf

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值