HttpRequestHandler:发送一个pdf文件作为响应

时间:2016-10-04 06:47:26

标签: 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();
            }

            }

    };

1 个答案:

答案 0 :(得分:1)

将内容类型更改为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