Java 在线预览pdf

参考文献

Java实现word文档在线预览,读取office(word,excel,ppt)文件 https://blog.csdn.net/weixin_34004576/article/details/94525426?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-1.pc_relevant_default&utm_relevant_index=2
Java 实现word pdf在线预览 https://blog.csdn.net/Black_Tshirt/article/details/81066520?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-3.pc_relevant_default&utm_relevant_index=6
SpringBoot读取Resource下文件的4种方法 http://cache.baiducontent.com/c?m=O4TEIcBO532jq6TszxU4g3APaU5-epsr6GjDxXiJmcb2mUbu3Zn3NLcVzn05T3fRjCkG9l-sJoCtBMS27gWNFu1duPewVlo8A51ySBdVDW6_1O4PLc6TDHIimjeoR-SsAzlYf2YBSd9RPT_ChLNg8EHBb6gzlx0QHTwLWQW32kqhpcnTjy0vWJyNDZ7fHNew&p=8e67c64ad4934eac59ecd23c48529f&newp=837e8b1e908912a05abd9b7d0c17c4231610db2151d7d11f6b82c825d7331b001c3bbfb422201a01d5ce7a6c03af485ce1f437783d0923a3dda5c91d9fb4c57479&s=cfcd208495d565ef&user=baidu&fm=sc&query=springboot++%B6%C1%C8%A1resources%CF%C2%CE%C4%BC%FE&qid=a9314b3f00084a77&p1=2

正文

Java 在线预览pdf_第1张图片

pom.xml



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.5.12
		 
	
	com.example
	test3
	0.0.1-SNAPSHOT
	test3
	Demo project for Spring Boot
	
		1.8
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.projectlombok
			lombok
			true
		
		
			commons-io
			commons-io
			2.11.0
		
	

TestController.java 

package com.example.test3.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@RestController
public class TestController {
    @GetMapping("/home")
    public void home(HttpServletResponse response) throws IOException {
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("pdf/123.pdf");
        ServletOutputStream outputStream = response.getOutputStream();
        long length = IOUtils.copy(inputStream, outputStream);
        System.out.println("该文件字节长度为 = " + length);
        IOUtils.close(inputStream);
        IOUtils.close(outputStream);
    }
}

测试

Java 在线预览pdf_第2张图片

进阶,增加前端代码

Java 在线预览pdf_第3张图片

index.html




    
    清算值班
    


预览 预览

 测试

Java 在线预览pdf_第4张图片

点击预览

在新标签页打开pdf

Java 在线预览pdf_第5张图片 

 

你可能感兴趣的:(java)