javascript - HttpRequestHandler : Sending a pdf file in response

标签 javascript java android

我想要服务器一个 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/

相关文章:

javascript - 新选项 json 对象 server.register hapijs 方法的格式是什么

javascript - 如何使用 jquery/javascript 更改/编辑标签的值?

android - 如何在用户离开后立即正确完成(重置) Activity

javascript - 在 HTML 中遍历 map

java - 通过源码包更新现有的类定义

java - 无法通过 id 定位元素

java - 嵌套 JAVA API 的简单异常处理

android - 调试需要服务器 URL 的应用程序

java - SharedPreference 未将任何数据保存到我的设备中

javascript - orderBy $filter 在项目删除(拼接)后阻止 ng-repeat 列表更新