下面进一步优化:
具体逻辑
:删除原数据表Test,路径等文件信息依旧存log_file表。文件预览时,判断文件类型,如果为word类型,进行转换pdf展示;反之直接展示文件。
word类型条件满足后,进一步判断文件是否已经转换过(判断依据是根据路径下文件名查找相同文件名的pdf文件是否存在),如果同名的pdf文件存在,表示已经转换过,直接展示;如果不存在,word转换pdf,再展示。
代码如下:
public void world(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws IOException {
List<LogFile> files = terminalServiceImpl.world();
//用作实例展示,在这取得是数据库第二条信息:以word文件展示。
String path = files.get(1).getFilePath();
String filename=files.get(1).getFileName();
//1.根据文件后缀获取文件类型
File wordName=new File(filename);
String fileName=wordName.getName();
String fileTyle=fileName.substring(fileName.lastIndexOf("."),fileName.length());
logger.info("------文件后缀名------"+fileTyle);
//如果文件类型为word,需要先转换成pdf
if(fileTyle.equals(".doc")||fileTyle.equals(".docx")){
//1.判断文件是否已经转换pdf,文件是否存在:文件前缀+.pdf(word是否已经转换过pdf),如果存在直接展示pdf
File fileOne = new File(path);
String caselsh = filename.substring(0,filename.lastIndexOf("."));
logger.info("------pdf文件名字------"+caselsh+"pdf.pdf");
// File fileIf = new File(fileOne+"\\" + caselsh+".pdf");
String filePath=(fileOne+"\\" + caselsh+"pdf.pdf");
File fileIf=new File(filePath);
if(!fileIf.exists()){
String pdfFilePath = OpenOfficeUtil.wordToPDF(path,filename);
System.err.println(pdfFilePath);
FileInputStream fileInputStream = new FileInputStream(pdfFilePath);
byte[] content = new byte[fileInputStream.available()];
fileInputStream.read(content);
fileInputStream.close();
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
if (servletOutputStream != null && content != null){
httpServletResponse.setContentType("application/pdf");
httpServletResponse.setContentLength(content.length);
httpServletResponse.setHeader("Expires","0");
httpServletResponse.setHeader("Cache-Control","must-revalidate,post-check=0,pre-check=0");
httpServletResponse.setHeader("Pragma","public");
servletOutputStream.write(content);
servletOutputStream.flush();
servletOutputStream.close();
}else{
File file3 = new File(filePath);
//获取pdf文件路径,内容做展示
byte[] resultE = new byte[(int) file3.length()];
FileInputStream fis = new FileInputStream(file3);
fis.read(resultE); //read file into bytes[]
fis.close();
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
if (servletOutputStream != null && resultE != null){
httpServletResponse.setContentType("application/pdf");
httpServletResponse.setContentLength(resultE.length);
httpServletResponse.setHeader("Expires","0");
httpServletResponse.setHeader("Cache-Control","must-revalidate,post-check=0,pre-check=0");
httpServletResponse.setHeader("Pragma","public");
servletOutputStream.write(resultE);
servletOutputStream.flush();
servletOutputStream.close();
}else{
File file = new File(path);
File file2 = new File(file+"\\" + filename);
//获取pdf文件路径,内容做展示
byte[] result = new byte[(int) file2.length()];
FileInputStream fis = new FileInputStream(file2);
fis.read(result); //read file into bytes[]
fis.close();
ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
if (servletOutputStream != null && result != null){
httpServletResponse.setContentType("application/pdf");
httpServletResponse.setContentLength(result.length);
httpServletResponse.setHeader("Expires","0");
httpServletResponse.setHeader("Cache-Control","must-revalidate,post-check=0,pre-check=0");
httpServletResponse.setHeader("Pragma","public");
servletOutputStream.write(result);
servletOutputStream.flush();
servletOutputStream.close();