我想要服务器一个 pdf 文件而不是 mp3 文件,请帮忙
private HttpRequestHandler mRequestHandler = new HttpRequestHandler()
{
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
throws HttpException, IOException
{
try{
File file = new File("/mnt/sdcard/Music/song.mp3");
FileEntity body = new FileEntity(file, "audio/mpeg");
response.setHeader("Content-Type", "application/force-download");
response.setHeader("Content-Disposition","attachment; filename=song.mp3");
response.setEntity(body);
}catch(Exception e)
{
e.printStackTrace();
}
}
};
最佳答案
将内容类型更改为 application/pdf
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context)
throws HttpException, IOException
{
try{
File file = new File("/my/path/file.pdf");
FileEntity body = new FileEntity(file, "application/pdf");
response.setHeader("Content-Type", "application/force-download");
response.setHeader("Content-Disposition","attachment; filename=file.pdf");
response.setEntity(body);
}catch(Exception e)
{
e.printStackTrace();
}
}
};
您可以检查 mime 类型 here
关于javascript - HttpRequestHandler : Sending a pdf file in response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39845827/