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
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型