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

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

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?