我尝试用actix web发送一个带有HttpResponse的图像,我的响应看起来像这样,我的问题是我只能返回一个静态u8,但缓冲区是一个[u8; 4096]而不是静态的,有没有什么方法可以使发送图像成为可能?
HttpResponse::Ok()
.content_type("image/jpeg")
.body(buffer)
buffer是:
let mut f = fs::File::open(x).expect("Somthing went wrong");
let mut buffer = [0;4096];
let n = f.read(&mut buffer[..]);
完整功能:
fn img_response(x: PathBuf, y: Image)->HttpResponse{
let mut f = fs::File::open(x).expect("Somthing went wrong");
let mut buffer = [0;4096];
let n = f.read(&mut buffer[..]);
match y{
Image::JPG =>{
HttpResponse::Ok()
.content_type("image/jpeg")
.body(buffer)}
Image::PNG =>{
HttpResponse::Ok()
.content_type("image/png")
.body(buffer)}
Image::ICO => {
HttpResponse::Ok()
.content_type("image/x-icon")
.body(buffer)}
}
}
在我的索引函数中调用函数img_response
match path.extension().unwrap().to_str().unwrap(){
"png" => {return img_response(path, Image::PNG);}
"jpeg" => {return img_response(path, Image::JPG);}
"ico" => {return img_response(path, Image::ICO);}
};
完整代码:https://github.com/Benn1x/Kiwi代码压缩:
#![allow(non_snake_case)]
use actix_web::{ web, App, HttpRequest,HttpResponse , HttpServer};
use mime;
use std::path::PathBuf;
use serde_derive::Deserialize;
use std::process::exit;
use toml;
use std::fs::read_to_string;
use actix_web::http::header::ContentType;
use std::fs;
use std::io::prelude::*;
use std::io;
fn img_response(x: PathBuf)->HttpResponse{
let mut f = fs::File::open(x).expect("Somthing went wrong");
let mut buffer = [0;4096];
let n = f.read(&mut buffer[..]);
HttpResponse::Ok()
.content_type("image/jpeg")
.body(buffer)
}
async fn index(req: HttpRequest) -> HttpResponse {
let mut path: PathBuf = req.match_info().query("file").parse().unwrap();
match path.extension().unwrap().to_str().unwrap(){
"jpeg" => {return img_response(path);}
_ => {return img_response(path);}
}
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.route("/{file:.*}", web::get().to(index))
.service(actix_files::Files::new("/", ".").index_file("index.html"))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
这是main.rs,但只是返回图像的func
2条答案
按热度按时间oprakyz71#
HttpResponse::Ok()
返回HttpResponseBuilder
。它的方法.body()
接受一个泛型参数,该参数必须实现MessageBody
trait。你的问题是
[u8; 4096]
不实现MessageBody
。但是,**实现MessageBody
的是Vec<u8>
。因此,通过将静态数组修改为动态向量,您的代码似乎可以编译:
但是,你的代码仍然存在一些问题:
下面是你的代码的工作版本:
请注意,对于较大的图像尺寸,使用
.streaming()
主体将是有益的:a11xaf1n2#
作为替代:
你可以这样做(也流文件):
https://github.com/actix/actix-web/discussions/2720#discussioncomment-2510752