牛顿的哈密瓜 2021-01-09 14:48 采纳率: 100%
浏览 1160
已采纳

springboot 返回pdf流文件怎么在浏览器显示自定义的title

@RequestMapping(path = "/file", method = RequestMethod.GET)
public void test(HttpServletResponse response) throws IOException {
    String str ="D:/1.pdf";
    FileInputStream fileInputStream= new FileInputStream(str);
    response.addHeader("Content-Disposition", "attachment; filename=" + str.substring(str.lastIndexOf("/") + 1));
    response.setContentType("application/pdf");
    byte[] bytes = new byte[fileInputStream.available()];
    int i;
    if ( (i = fileInputStream.read(bytes)) > 0){
        response.getOutputStream().write(bytes, 0, i);
    }
}

这种方式浏览器显示的title就是 file,而不是1.pdf,请问怎么才能让它显示成自定义的标题呢?

response.addHeader("Content-Disposition", "attachment; filename=" + str.substring(str.lastIndexOf("/") + 1));

这个定义用在下载文件上的,我把attachment改成inline也显示不出来

  • 写回答

2条回答 默认 最新

  • shy好好学习 Java领域新星创作者 2021-03-11 16:15
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?