问题描述:
通过配置nginx在浏览器预览本地pdf文件,文件名称为test.pdf,在浏览器进行预览打开,浏览器标签展示为一串数字,这里是因为浏览器标签识别的是文件的标题,并不是文件名
文件简介展示:

文件在浏览器中访问展示:

解决步骤
1、利用java代码进行解决,首先在pom中引用jar包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
2、代码书写
/**
* 修改文件标题
* @param filePath 文件路径
*/
public static void modifyPdfProperties(String filePath) {
log.info("modifyPdfProperties==filePath==[{}]",filePath);
try {
File file = new File(filePath);
//包含后缀文件名
String fileName = file.getName();
String pdfName = "";
if (fileName != null) {
//去除后缀文件名,用作设置标题属性
pdfName = fileName.substring(0, fileName.lastIndexOf("."));
}
PdfReader pdfReader = new PdfReader(filePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper pdfStamper = new PdfStamper(pdfReader, bos);
//获取文件属性
HashMap<String, String> info = pdfReader.getInfo();
//添加文件标题属性
info.put("Title", pdfName);
pdfStamper.setMoreInfo(info);
pdfStamper.close();
OutputStream fos = new FileOutputStream(file.getPath());
fos.write(bos.toByteArray());
fos.flush();
fos.close();
bos.close();
FileInputStream input = new FileInputStream(file);
IOUtils.toByteArray(input);
} catch (Exception e) {
log.info("增加标题属性错误==[{}]",e);
}
}
3、 main方法执行
public static void main(String[] args) {
String inPath = "/Users/Desktop/test.pdf";
modifyPdfProperties(inPath);
}
4、执行成功之后查看文件简介

5、浏览器进行预览
