为什么 reqwest 下载的 PNG 图像会损坏?

Why is a PNG image downloaded by reqwest corrupt?

我正在按照 https://rust-lang-nursery.github.io/rust-cookbook/web/clients/download.html 的 Rust Cookbook 中提到的代码通过 HTTP GET 请求以异步方式下载文件。

我的代码如下:

#[tokio::main]
async fn main() -> Result<()> {

    let object_path = "logos/rust-logo-512x512.png";
    let target = format!("https://www.rust-lang.org/{}", object_path);  
    let response = reqwest::get(&target).await?;

    let mut dest = {
    
        let fname = response
            .url()
            .path_segments()
            .and_then(|segments| segments.last())
            .and_then(|name| if name.is_empty() { None } else { Some(name) })
            .unwrap_or("tmp.bin");
            
            
        println!("file to download: '{}'", fname);

        let object_prefix = &object_path[..object_path.rfind('/').unwrap()];
        let object_name = &object_path[object_path.rfind('/').unwrap()+1..];
        let output_dir = format!("{}/{}", env::current_dir().unwrap().to_str().unwrap().to_string(), object_prefix);
        fs::create_dir_all(output_dir.clone())?;

        println!("will be located under: '{}'", output_dir.clone());
                
        let output_fname = format!("{}/{}", output_dir, object_name);
        println!("Creating the file {}", output_fname);
        
        File::create(output_fname)?
        
    };
    let content =  response.text().await?;
    copy(&mut content.as_bytes(), &mut dest)?;
    Ok(())
}

它创建目录并下载文件。 但是,当我打开文件时,它显示损坏的文件错误 我也尝试过使用其他一些 URL,但文件损坏问题仍然存在

我是否遗漏了代码中的某些内容?

正在替换

let content =  response.text().await?;
copy(&mut content.as_bytes(), &mut dest)?;

来自

let content =  response.bytes().await?;
    
let mut pos = 0;
while pos < content.len() {
    let bytes_written = dest.write(&content[pos..])?;
    pos += bytes_written;
}

成功了! :)

如果此代码效率低下,请回复 感谢大家的帮助。

只用bytes and Cursor也行,而且更简单:

let mut content =  Cursor::new(response.bytes().await?);
copy(&mut content, &mut dest)?;