之前在项目中做了一个转pdf格式的需求,网上查了一些资料把完成了。用的是工具转换,今天主要说一些在线预览pdf的java代码的书写,如果需要了解工具的话可以留言,有时间我会把对应的代码整理出来。
不多说,直接上代码。
这个是通过文件的对应路径filePath 来进行访问的
@RequestMapping("/read/{fileName}")
public void readFile(HttpServletResponse res , @PathVariable String fileName) throws Exception{
InputStream in = null;
OutputStream out = null;
//请忽略这段代码
String filePath = fileHandler(fileName);
//判断是pdf还是word还是excel
//若是pdf直接读 否则转pdf 再读
try{
if(filePath != null){
in = new FileInputStream(filePath);
}
res.setContentType("application/pdf");
out = res.getOutputStream();
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[1024]; //buff用于存放循环读取的临时数据
int rc = 0;
while ((rc = in.read(buff, 0, 1024)) > 0) {