maiyuqiang 2016-09-24 03:58 采纳率: 37.5%
浏览 879
已结题

Android文件下载出现问题


                        HttpClient client = new DefaultHttpClient();
                        HttpGet get = new HttpGet(downloadurl);
                        HttpResponse response;
                        try {
                            response = client.execute(get);
                            HttpEntity entity = response.getEntity();
                            long length = entity.getContentLength();
                            InputStream is = entity.getContent();
                            FileOutputStream fileOutputStream = null;
                            if (is != null) {
                                File file = new File(Environment
                                        .getExternalStorageDirectory(),
                                        fileName);
                                fileOutputStream = new FileOutputStream(file);
                                byte[] buf = new byte[1024];

                                int ch = -1;
                                int count = 0;
                                int percent = 0;
                                while ((ch = is.read(buf)) != -1) {
                                    fileOutputStream.write(buf, 0, ch);
                                    count += ch;
                                    if (((int) (100 * count / length)) > percent) {
                                        percent = (int) (100 * count / length);
                                        progressDialog.setProgress(percent);
                                        if (percent == 100) {
                                            successDownload = true;
                                        }
                                    }
                                    if (length > 0) {
                                    }
                                }
                            }
                            fileOutputStream.flush();
                            if (fileOutputStream != null) {
                                fileOutputStream.close();
                            }
                            down(fileName);
                        } catch (ClientProtocolException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                            progressDialog.cancel();

                        }

之前的下载地址是文件路径,可以下载,后来改成我给后台发请求后下载,这样下载就不行了,这个应该怎么改?

  • 写回答

1条回答

  • 王晓恩 2016-09-24 09:38
    关注

    后台请求的下载?什么鬼?
    你看看能不能直接在网页下载或者 找到指定文件路径
    再有 下载为毛自己写 那么多封装的很好的控件 重复 造轮子

    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler