android开发利用AsyncHttpClient下载服务器文件

                // 服务器路径
                final String realPath = f.getLocation();
                // 本地路径
                final String path = DownloadUtil.getGroupFilePath(realPath);
                java.io.File file = new java.io.File(path);
                // 文件本地不存在,先下载到本地,再打开文件
                if (!file.exists()) {
                    String romoteUrl = Constants.API_WWW + Constants.API_WWW_SPLIT + f.getLocation();
                    AsyncHttpClient client = new AsyncHttpClient();
                    client.get(romoteUrl, new AsyncHttpResponseHandler() {
                        @Override
                        public void onStart() {
                            // called before request is started
                        }

                        @Override
                        public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                            // called when response HTTP status is "200 OK"
                            if (statusCode == 200) {
                                BufferedOutputStream bos = null;
                                FileOutputStream fos = null;
                                java.io.File localFile = null;
                                try {
                                    localFile = new java.io.File(path);
                                    fos = new FileOutputStream(localFile);
                                    bos = new BufferedOutputStream(fos);
                                    bos.write(response);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                } finally {
                                    if (bos != null) {
                                        try {
                                            bos.close();
                                        } catch (IOException e1) {
                                            e1.printStackTrace();
                                        }
                                    }
                                    if (fos != null) {
                                        try {
                                            fos.close();
                                        } catch (IOException e1) {
                                            e1.printStackTrace();
                                        }
                                    }
                                }
                            }
                            // 根据后缀名来判断打开什么文件
                            Intent intent = openFiles(path);
                            if (IntentUtil.isIntentAvailable(context, intent)) {
                                startActivity(intent);
                            } else {
                                showToast("请安装第三方预览软件,如WPS");

                            }
                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
                            // called when response HTTP status is "4XX" (eg. 401, 403, 404)
                        }

                        @Override
                        public void onRetry(int retryNo) {
                            // called when request is retried
                        }
                    });

利用AsyncHttpClient实现图片的上传与下载

http://blog.csdn.net/xinzheng_wang/article/details/38925731

猜你喜欢

转载自blog.csdn.net/snn1410/article/details/51516214