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

 

 

 

發佈了89 篇原創文章 · 獲贊 5 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章