1.页面代码,引入js文件,根据文件id加载pdf预览文件
<html xmlns="http://www.w3.org/1999/xhtml">
<head th:include="commonBase">
<title>文件预览</title>
</head>
<link rel="stylesheet" th:href="@{/common/css/fileIcon.css}"/>
<script type="text/javascript" th:src="@{/common/js/pdfobject.js}"></script>
<body>
<div style="display:none">
<input class="mini-hidden" name="id" id="id" th:value="${id}"/>
</div>
<div id="pdf"></div>
</body>
<script type="text/javascript">
/*<![CDATA[*/
$(function(){
mini.parse();
var id = mini.get("id").getValue();
var myPDF = new PDFObject({url:basePath+'scoreFile/previewFileById?id='+id}).embed("pdf");
})
/*]]>*/
</script>
</html>
2.后台根据文件id返回文件流展示pdf文件
(1)controller层,根据文件id查询文件存在在minio中的位置信息
@RequestMapping(value = { "/previewFileById" }, produces = { "text/html;charset=UTF-8" })
@ResponseBody
public void previewFileById(HttpServletRequest request, HttpServletResponse response) {
try {
response.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
ScoreFile scoreFile = scoreFileService.findById(id);
String fileDirectory = scoreFile.getFileDirectory();
String fileNameUuidPdf = scoreFile.getFileNewName();
String fileName = scoreFile.getFileName();
String fileNamePdf = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf"; // pdf文件名
String path = scoreFile.getFilePath() + fileNameUuidPdf;
FileUtils.previewfileById(fileDirectory, path, fileNamePdf, response); // 跟据存放在minio中目录得到转化好的PDF文件流
} catch (Exception e) {
e.printStackTrace();
}
}
(2)根据存放位置信息加载文件,返回文件流,写在fileutils工具类中
public static void previewfileById(String fileDirectory, String path, String fileNamePdf,HttpServletResponse response) {
InputStream in = null;
OutputStream out = null;
MinioClient minioClient = FileUtils.getInstance();
try
{
in = minioClient.getObject(fileDirectory, path);
int len = 0;
byte[] buffer = new byte[1024];
out = response.getOutputStream();
response.reset();
String filename = getFileNameForISO8859(fileNamePdf); //得到iso-8859-1编码的文件名
response.addHeader("Content-Disposition","inline;filename="+filename);
response.setContentType("application/pdf;charset=UTF-8");
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e){
e.printStackTrace();
} finally {
if (in != null) {
try
{
in.close();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @Description 得到iso-8859-1编码的文件名
* @author [zhanglizeng]
* @throws UnsupportedEncodingException
* @createDate 2018年9月28日 下午6:46:41
*/
private static String getFileNameForISO8859(String fileNamePdf) throws UnsupportedEncodingException {
Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]"); //匹配中文
if (fileNamePdf != null) {
Matcher aMatcher = pattern.matcher(fileNamePdf);
if(!aMatcher.find()) {
fileNamePdf = "下载-"+fileNamePdf;
}
return new String(fileNamePdf.getBytes(),"iso-8859-1");
}
return "文件名为空";
}
//创建minio的链接
public static MinioClient getInstance() {
if (minioClient == null) {
synchronized (FileUtils.class) {
if (minioClient == null) {
try {
//new FileUtils();
minioClient = new MinioClient(minioUrl,minioUsername,minioPassword);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return minioClient;
}