首页 > 解决方案 > 如何在 HttpServletResponse 中设置表标题?

问题描述

我们正在使用 broser 中显示文件

shiowMethod(final String viewType, final String filename,HttpServletResponse response){
        response.setHeader("Content-Type", "application/pdf");
        response.setHeader("Content-Length", String.valueOf(docasnyObsah.getSoubor().length));
        response.setHeader("Cache-Control", "private");
        response.setHeader("Pragma", "private");
        response.setHeader("Content-Disposition", ContentDisposition.create(zobrazeni , "my_file.pdf", 
       "My title"));
        response.flushBuffer();
}

public static String create(final String viewType, final String filename, final String fieldName) {
    try {
        final String encoded = URLEncoder.encode(filename, StandardCharsets.UTF_8.name()).replace("+", "%20");
        if(fieldName.isEmpty()){
            return String.format("%1$s; filename*=UTF-8''%2$s; filename=\"%2$s\"", viewType, encoded);
        }
        else {
            String test = String.format("%1$s; filename*=UTF-8''%2$s; name=\"%3$s\"; filename=\"%2$s\"", viewType, encoded, fieldName);
            return test;
        }


    } catch (final UnsupportedEncodingException e) {
        throw new ShouldNotHappenRuntimeException(e);
    }
}

我希望这会将选项卡的标题设置为“我的标题”,但不是这个,而是 viewType 显示如下

在此处输入图像描述

发送到 submetod 的值是

viewType = "inline"
filename = "my_file.pdf"
fieldName = "My title"

我需要更改什么才能设置 html 页面选项卡名称?

标签: javaspring

解决方案


推荐阅读