简体   繁体   English

使用Android Webview下载文件

[英]Download files with Android Webview

I am making an Android app that uses a WebView to access to a webpage. 我正在制作一个使用WebView来访问网页的Android应用。 To handle downloads I am using AsyncTask in method onDownloadStart of WebView's DownloadListener. 为了处理下载,我在WebView的DownloadListener的onDownloadStart方法中使用了AsyncTask。 However files downloaded are blank (although the filename and extension are correct). 但是,下载的文件为空白(尽管文件名和扩展名正确)。 My Java code is this: 我的Java代码是这样的:

protected String doInBackground(String... url) {  
    try {
        URL url = new URL(url[0]);    

        //Creating directory if not exists

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(true);        
        connection.connect();

        //Obtaining filename

        File outputFile = new File(directory, filename);
        InputStream input   = new BufferedInputStream(connection.getInputStream());
        OutputStream output = new FileOutputStream(outputFile);

        byte data[] = new byte[1024];
        int count = 0;
        Log.e(null, "input.read(data) = "+input.read(data), null);
        while ((count = input.read(data)) != -1) {
            output.write(data, 0, count);
        }              
        connection.disconnect();
        output.flush();
        output.close();
        input.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
         e.printStackTrace();
    }
    return null;
}

log.e line gives -1 value for input.read(data). log.e行为input.read(data)给出-1值。 PHP code of download page is this (works in all platforms). 下载页面的PHP代码是这个(在所有平台上都可以使用)。 Files are stored in non-public directories of my HTML server. 文件存储在我的HTML服务器的非公共目录中。

<?php
$guid = $_GET['id'];
$file = get_file($guid);

if (isset($file['path'])) { 
    $mime = $file['MIMEType'];
    if (!$mime) {
        $mime = "application/octet-stream";
    }

    header("Pragma: public");
    header("Content-type: $mime");
    header("Content-Disposition: attachment; filename=\"{$file['filename']}\"");
    header('Content-Transfer-Encoding: binary');

    ob_clean();
    flush();
    readfile($file['path']);
    exit();

}
?>

I've noticed that if I write some text after "?>" of PHP file, this text is written in the file downloaded. 我注意到,如果我在PHP文件的“?>”之后写一些文本,则此文本将写在下载的文件中。

In your code, you are using ob_clean() , which will just erase the output buffer. 在您的代码中,您使用的是ob_clean() ,这只会删除输出缓冲区。 Your subsequent call to flush() therefore doesn't return anything, because the output buffer has been flushed beforehand. 因此,您随后对flush()调用不会返回任何内容,因为输出缓冲区已预先刷新。

Instead of ob_clean() and flush() , use ob_end_flush() . 代替ob_clean()flush() ,使用ob_end_flush() This will stop output buffering and it will send all the output it withheld. 这将停止输出缓冲,并将发送所有保留的输出。

ob_end_flush — Flush (send) the output buffer and turn off output buffering ob_end_flush —刷新(发送)输出缓冲区并关闭输出缓冲

If you want to stop output buffering without outputting whatever is saved, you can use ob_end_clean() . 如果要停止输出输出缓冲而不输出任何已保存的内容,则可以使用ob_end_clean() Anything after this command will be output again, but anything between ob_start() and ob_end_clean() will be "swallowed." 此命令之后的所有内容都会再次输出,但是ob_start()ob_end_clean()都会被“吞咽”。

ob_end_clean — Clean (erase) the output buffer and turn off output buffering ob_end_clean —清理(擦除)输出缓冲区并关闭输出缓冲

What are the benefits of output buffering in the first place? 首先,输出缓冲的好处是什么? If you are doing ob_start() and then using flush() on everything you might as well output everything directly. 如果您正在执行ob_start() ,然后对所有内容都使用flush() ,则不妨直接输出所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM