项目有个新需求,就是用户上传过的文件,可以支持在线预览,不用下载
也感谢有一个技术群里的朋友帮助,非常感谢
采取的是jquery.media.js在页面预览pdf文件。
1、所以首先将上传的文件点击预览时转为pdf文件。
Office的各种类型的文件我们用OpenOffice和jodconverter
OpenOffice:自己到官网下载安装即可。
http://www.openoffice.org/
(安装路径默认即可,因为工具类路面的安装路径就是默认的那个:C:\Program Files (x86)\OpenOffice 4)
Jodconverter
的maven配置如下(好像是阿里云的):
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.2.0</version>
<type>zip</type>
</dependency>
工具类OpenOfficeUtil.java
/** * * @Title:OpenOffice工具类 * @description:利用OpenOffice将office的各种文件转为pdf * @author 何泳锋 * @date 20180515 * @version V1.0 */ public class OpenOfficeUtil { private static OfficeManager manager; /** * 文档2pdf(自动启动服务) * * @param inputFile * @param outputFile * @return * @throws Exception * @date 2018年3月11日 */ public static boolean doc2pdf(File inputFile, File outputFile) { boolean result = false; if (inputFile.exists()) { if (!outputFile.exists()) { // 初始化OpenOffice配置 initOfficeManagerConfiguration(); // 启动服务 manager.start(); // 开始转换 OfficeDocumentConverter converter = new OfficeDocumentConverter(manager); converter.convert(inputFile, outputFile); // 停止服务 manager.stop(); result = true; } else { result = true; System.out.println("****已经转换为pdf,不需要再进行转化****"); } } else { System.out.println("****需要转换的文档不存在,无法转换,文件路径=" + inputFile.getAbsolutePath() + "****"); } return result; } /** * 初始化OpenOffice配置 * * @date 2018年5月16日 */ private static void initOfficeManagerConfiguration() { DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration(); // configuration.setWorkDir(new File("/opt")); String officeHome = getOfficeHome(); configuration.setOfficeHome(officeHome);// 设置安装目录 configuration.setPortNumbers(8100); // 设置端口 configuration.setTaskExecutionTimeout(1000 * 60 * 5L); // 设置任务执行超时为5分钟 configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L); // 设置任务队列超时为24小时 manager = configuration.buildOfficeManager(); if (manager == null) throw new RuntimeException("初始化OpenOffice失败"); } /** * 获取OpenOffice的安装路径 * * @date 2018年5月16日 */ public static String getOfficeHome() { String osName = System.getProperty("os.name"); System.out.println("操作系统名称:"+osName); if (Pattern.matches("Linux.*", osName)) { return "/opt/openoffice.org4"; } else if (Pattern.matches("Windows.*", osName)) { return "C:\\Program Files (x86)\\OpenOffice 4"; } else if (Pattern.matches("Mac.*", osName)) { return "/Application/OpenOffice.org.app/Contents"; } return null; } public static void main(String[] args){ String infile = "D:\\upload\\admin\\2018\\05\\15\\1.xlsx"; String targetfile = "D:\\upload\\admin\\2018\\05\\15\\1.pdf"; File inFile = new File(infile); File targetFile = new File(targetfile); try { doc2pdf(inFile,targetFile); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Openoffice配合Jodconverter能转换doc,docx,pptx,ppt,xsl,xslx,图片等,但是转换不了txt文本文档。
下面用IText来将txt文本文档转换为pdf。
IText需要的Jar包的Maven配置如下:
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.12</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
工具类的代码:
/** * * @Title:IText工具类 * @description:利用IText将txt文本转为pdf * @author 何泳锋 * @date 20180516 * @version V1.0 */ public class ITextUtil { public static boolean text2pdf(String text,String pdf){ try{ File pdfFile = new File(pdf); if(!pdfFile.exists()){ //获取txt文本的编码类型 String charsetName = CpdetecorUtil.getFileEncode(text); /*BaseFont baseFont = getBaseFont();*/ //直接把字体放到项目的resources中,因为服务器是在linux系统 String fontPath = ITextUtil.class.getClassLoader().getResource("font/SIMHEI.TTF").getPath(); BaseFont baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); Font FontChinese = new Font(baseFont, 12, Font.NORMAL); FileOutputStream out = new FileOutputStream(pdf); Rectangle rect = new Rectangle(PageSize.A4.rotate()); Document doc = new Document(rect); PdfWriter writer = PdfWriter.getInstance(doc, out); doc.open(); Paragraph p = new Paragraph(); p.setFont(FontChinese); FileInputStream fileInstream = new FileInputStream(new File(text)); InputStreamReader isr = new InputStreamReader(fileInstream, charsetName); //根据txt文本的编码获取流 BufferedReader read = new BufferedReader(isr); String line = read.readLine(); while(line != null){ System.out.println(line); p.add(line+"\n"); line = read.readLine(); } read.close(); doc.add(p); doc.close(); return true; }else{ System.out.println("****已经转换为pdf,不需要再进行转化****"); return true; } }catch(DocumentException e){ e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } } /** * 根据当前操作系统获取baseFont * */ public static BaseFont getBaseFont() throws DocumentException, IOException { String osName = System.getProperty("os.name"); System.out.println("操作系统名称:"+osName); if (Pattern.matches("Linux.*", osName)) { return BaseFont.createFont("/usr/share/fonts/fallback/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); } else if (Pattern.matches("Windows.*", osName)) { return BaseFont.createFont("C:\\Windows\\Fonts\\simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); } return null; } public static void main(String[] args) throws Exception { String text = "D:\\files\\11.txt"; String pdf = "D:\\files\\11.pdf"; text2pdf(text, pdf); } }
但是有一个问题,因为txt文本文档保存的编码是不统一的,所以获取文件流的时候要先判断文件的编码。我们用到Cpdatector,也是一个开源项目。
需要用到的jar包有下面四个
<dependency>
<groupId>cpdetector</groupId>
<artifactId>cpdetector</artifactId>
<version>1.0.7</version>
</dependency>
cpdetector需要依赖下面三个Jar
<dependency>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.mozilla.intl</groupId>
<artifactId>chardet</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>jargs</groupId>
<artifactId>jargs</artifactId>
<version>1.0</version>
</dependency>
上面正常配置cpdetactor是找不到的,需要在<repositorys>标签路面添加下面的东东
<repository>
<id>ebi</id>
<name>www.ebi.ac.uk</name>
<url>http://www.ebi.ac.uk/intact/maven/nexus/content/groups/public/</url>
</repository>
建议自己导入Jar包,因为其他同事下载不了依赖:
Cpdetector工具类的代码如下:
/** * * @Title:Cpdetecor工具类 * @description:判断文件编码 * @author 何泳锋 * @date 20180516 * @version V1.0 */ public class CpdetecorUtil { public static String getFileEncode(String filePath) { String charsetName = null; try { File file = new File(filePath); CodepageDetectorProxy detector = CodepageDetectorProxy.getInstance(); detector.add(new ParsingDetector(false)); detector.add(JChardetFacade.getInstance()); detector.add(ASCIIDetector.getInstance()); detector.add(UnicodeDetector.getInstance()); Charset charset = null; charset = detector.detectCodepage(file.toURI().toURL()); if (charset != null) { charsetName = charset.name(); } else { charsetName = "UTF-8"; } } catch (Exception ex) { ex.printStackTrace(); return null; } return charsetName; } public static void main(String[] args) { String filePath = "D:\\upload\\admin\\2018\\05\\16\\297e0a366366a7a9016366b330cd0006.txt"; String charsetName = getFileEncode(filePath); System.out.println(charsetName); } }
2、转换后的pdf文件怎么在页面展示。
下面是点击预览的js方法:
看一下预览PDF文件的页面,是比较简单的。
需要注意的是,因为页面是经过跳转的,在http中,页面是不能正常访问到本地的文件的。所以我们需要给Tomcat加上一个虚拟路径。例如将
/file代替D:\uoload
打开Tomcat安装路径下的conf文件夹下的server.xml文件,在Host标签里添加如下:
我们也可以看到Controller里面最后也处理了传到前端的文件路径