SpringBoot-PDF预览(⾮前后端分离)⽂章⽬录
集成开发环境:Spring Tool Suite 4 - Version: 4.4.0.RELEASE
测试环境:Google Chrome - Version 88.0.4324.104 (Official Build) (64-bit)
项⽬
效果
新建 Spring Starter Project,项⽬结构:
编辑 l ⽂件,引⼊依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"xsi="/2001/XMLSchema-instance"
schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>com.mk</groupId>
<artifactId>spring-boot-pdf-preview</artifactId>
<version>1.0.0</version>
<name>spring-boot-pdf-preview</name>
<properties>
<java.version>1.8</java.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
启动类:
package com.mk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootPdfPreviewApplication {
public static void main(String[] args){
SpringApplication.run(SpringBootPdfPreviewApplication.class, args); }
}
IndexController 控制器类,提供 PDF 预览功能:
package ller;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.URLEncoder;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apachemons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@GetMapping({"","/"})
public String index(){
return"index";
}
@GetMapping("/preview")
public void download(HttpServletResponse response){
String pathname ="E:/数据⼿册";
String filename ="C2689406_轻触开关_6x6贴⽚_2021-02-19.pdf";
File file =new File(pathname, filename);
try{
response.setHeader("Content-Disposition","inline; filename*=UTF-8''"+ Name(),"UTF-8"));
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}
ServletOutputStream os = null;
try{
os = OutputStream();
os.adFileToByteArray(file));
os.flush();
}catch(IOException e){
e.printStackTrace();
}finally{
if(os != null){
close(os);
}
}
}
private void close(Closeable closeable){
if(closeable != null){
try{
closeable.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
⾸页 src/main/resources/templates/index.html:
<!DOCTYPE html>
<html>
springboot结构<head>
<meta charset="utf-8">
<style type="text/css">
a{
text-decoration: none;
display: block;
margin-top: 5px;
}
</style>
</head>
<body >
<a href="/preview">PDF 预览</a>
</body>
</html>
参考
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论