du8828 2017-05-18 11:52
浏览 83
已采纳

来自PHP服务器的Android下载文件

I am trying to download a file from my PHP server. Here is my PHP code:

$file = 'androidApps/app.apk';

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($file));
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);

exit;

File name and file size are correct. When I use my browser (on my computer AND on my phone) everything is fine. The file is correctly downloaded.

When I try to download it from my android app it will download a 38-byte file. Still in my android app if I use the direct link to my file it will download it correctly. Here is my android code:

    String path ="http://myserverURLAndPhpFileUrl.php";//If I put the path of my file it will download it correctly
    String PATH = Environment.getExternalStorageDirectory() + "/download/";
    URL u = null;

    u = new URL(path);
    HttpURLConnection c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.connect();

    InputStream in = c.getInputStream();

    File file = new File(PATH);
    file.mkdirs();
    File outputFile = new File(file, "app.apk");
    FileOutputStream fos = new FileOutputStream(outputFile);
    InputStream is = c.getInputStream();

    byte[] buffer = new byte[1024];
    int len1 = 0;
    while ((len1 = is.read(buffer)) != -1) {
        fos.write(buffer, 0, len1);
        System.out.println(len1);
    }
    fos.close();
    is.close();`

I have also tried to use the DownloadManager but I get the same issue:

  String path = "http://myserverURLAndPhpFileUrl.php";
   UpdateService.this.getSystemService(UpdateService.this.DOWNLOAD_SERVICE);
    DownloadManager.Request dlrequest = new DownloadManager.Request(Uri.parse(path));

    dlrequest.setVisibleInDownloadsUi(true);


dlrequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
    dlrequest.setDestinationInExternalFilesDir(UpdateService.this, null, "app.apk");
    System.out.println("FILE dir: " + getExternalFilesDir(null));
    //nqueue this request e
    DownloadManager downloadManager = (DownloadManager) UpdateService.this.getSystemService(UpdateService.this.DOWNLOAD_SERVICE);
    long downloadID = downloadManager.enqueue(dlrequest);
    downloadManager.enqueue(dlrequest);

I am using this code in a service. I have tested on android 5.0.1 and Android 4.3

PHP version: 5.4.45-0+deb7u8

How can I manage to download my file without using the path of the file ?

Thanks

  • 写回答

1条回答 默认 最新

  • dspows0637 2017-05-18 13:12
    关注

    My code is fine. I just had to add cookies in my request:

     dlrequest.addRequestHeader("Cookie","PHPSESSID="+session);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启