性能优化之通过Aspose组件将WordExcelPPTPDF转成HTML文件,解决大附件。。。
性能优化之通过Aspose组件将WordExcelPPTPDF转成HTML⽂件,解决⼤附
件。。。
在最近的⼀个项⽬中,遇到⼀个⾮常棘⼿的性能问题,场景是这样的:有PC端和⼿机端两个应⽤,⽤户在PC端上传的附件,如
word,Excel,pdf等,当⽤户出差或不在电脑边上时,上传的附件在⼿机端能够打开预览。然后问题就来了,当在PC端上传的附件⽐较⼤,在⼿机端想要预览就⽐较慢,有时候甚⾄打不开,怎么解决这个性能问题呢?
⼀、多线程分段下载
分析:在预览的时候,将原⽂件切分成多分,使⽤多线程分段下载,这种⽅案可以快速的下载⼀个⼤⽂件到本地,然后直接打开预览,但是⽤户不同意将附件下载到⼿机上,因为⼿机内存有限,⼏个⼤附件就把内存占满了。
⼆、⽣成静态HTML⽂件
分析:在PC端上传附件的时候,启⽤异步线程,借助aspose组件,将附件转成成静态HTML⽂件,并放
在单独的⼀个Tomcat下⾯,⼿机端预览附件的时候,直接访问这个Tomcat中的静态HTML,这样⼤⼤提升了预览速度,还不会占⽤⼿机内存,上传及预览的逻辑如下
英法海底隧道转静态资源时,需要aspose组件的⽀持,因此需要在l中引⼊aspoxe相关组件
<dependency>
<groupId>aspose</groupId>
<artifactId>aspose.pdf</artifactId>
<version>17.2.0</version>
</dependency>
<dependency>
<groupId>aspose</groupId>
<artifactId>aspose-cells</artifactId>
<version>17.3.0</version>
</dependency>
<dependency>
<groupId>aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>17.2-jdk16</version>
</dependency>
<dependency>
<groupId>aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>17.3.0-jdk16</version>
</dependency>
转静态HTML⽂件核⼼逻辑如下
package com.mairuanmon.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
怎么显示隐藏的文件夹
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import llections.CollectionUtils;
import org.apachemons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lls.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import base.system.service.IFileOperateService;
public class TransferToHtmlThread extends Thread{
private static final Logger logger = Logger(TransferToHtmlThread.class);
private String sourceFileRoot;
private String htmlPathRoot;
private List<Map<String, Object>> fileList;
private HttpServletRequest req;
private String tranFileType;
private String fontPath;
public TransferToHtmlThread(String sourceFileRoot, String htmlPathRoot, List<Map<String, Object>> fileList,    HttpServletRequest req, String tranFileType,String fontPath) {
this.sourceFileRoot = sourceFileRoot;
this.htmlPathRoot = htmlPathRoot + File.separator + "filedownload" + File.separator;
this.fileList = fileList;
this.fontPath = fontPath;
}
中国城市人口密度榜
@Override
public void run() {
String sourceFilePath = "";
String htmlPathPath = "";
String filePath = "";
File file = null;
List<Map<String,String>> files = null;
InputStream is = null;
String fileName = null;
String fileType = null;
try {
files = new ArrayList<Map<String,String>>();
for(Map<String, Object> fileInfo: fileList) {
filePath = String.("file_path")); // 存储在DB的⽂件路径
fileName = String.("saved_name")); // ⽂件名称
fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); // 获取⽂件后缀
List<String> tranTypeList = Arrays.anFileType.split(","));
if (!ains(fileType)) { // 不属于xlsx,xls,docx,doc,pptx,ppt,pdf类型⽂件不转HTML
continue;
}
sourceFilePath = sourceFileRoot + File.separator  + filePath; // 原⽂件路径
htmlPathPath = htmlPathRoot + filePath + ".html"; // ⽣成的HTML⽂件路径
is = getClass().getResourceAsStream("/Aspose.Total.Java.lic"); // License认证⽂件,如果没有此认证,最多只能转3页    //readContent(is);
if (StringUtils.startsWith(fileType, "doc")) {
com.aspose.words.License wordLicense = new com.aspose.words.License();
wordLicense.setLicense(is);
Document doc = new Document(sourceFilePath);
doc.save(htmlPathPath, SaveFormat.HTML);
}else if (StringUtils.startsWith(fileType, "xls")) {
lls.License excelLicense = new lls.License();
excelLicense.setLicense(is);
Workbook book = new Workbook(sourceFilePath);
book.save(htmlPathPath, lls.SaveFormat.HTML);
风油精的妙用
}else if (StringUtils.startsWith(fileType, "ppt")) {
com.aspose.slides.License pptLicense = new com.aspose.slides.License();
pptLicense.setLicense(is);
Presentation doc = new Presentation(sourceFilePath);
doc.save(htmlPathPath, com.aspose.slides.SaveFormat.Html);
}else if (StringUtils.startsWith(fileType, "pdf")) {
com.aspose.pdf.License pdfLicense = new com.aspose.pdf.License();
pdfLicense.setLicense(is);
com.aspose.pdf.Document.addLocalFontPath(fontPath);
com.aspose.pdf.Document pdfDoc = new com.aspose.pdf.Document(sourceFilePath);
pdfDoc.save(htmlPathPath, com.aspose.pdf.SaveFormat.Html);
}
htmlPathPath = place(htmlPathRoot, ""); // 去掉配置⽂件指定路径
电脑单机
Map<String,String> map = new HashMap<String, String>();
map.put("id", String.("id")));
map.put("tranPath", htmlPathPath);
files.add(map);
}
if (CollectionUtils.isNotEmpty(files)) {
IFileOperateService fileOperateService = (IFileOperateService) Bean("fileOperateService");
fileOperateService.updateTransFilePath(files);//保存转换后的静态资源⽂件路径到DB补水啫喱
}
}catch (Exception e) {
<("Transfer File to Html fail,The message is:", e);
}finally {
try {
if (is != null) {
}
} catch (IOException e) {
}
}
}
}
启异步线程将附件转成HTMl,核⼼逻辑如下
String tranFileType ="txt,xlsx,xls,docx,doc,pptx,ppt,zip,rar,img,jpg,pdf";
String filePath =/usr/mairuan/upload/";//PC端上传附件的存放路径
String tranFilePath ="/usr/mairuan/filepath/tran";//转换后静态资源⽂件的存放路径
List<Map<String,Object>> fileList = fileOperateService.uploadFile(multiFile, filePath);
String fontPath = Session().getServletContext().getRealPath("/fonts/");//PDF转出来在服务端预览时可能会出现乱码,需要⼀些字体⽀持
new Thread(new TransferToHtmlThread(filePath,tranFilePath,fileList,req,tranFileType,fontPath)).start();
部署⼀个空的Tomcat,在Tomcat的l中配置静态资源⽂件存放根路径,⼿机端预览附件时,直接请求这个Tomcat提供的服务即可
<Context docBase="/usr/mairuan/filepath/tran" path="/bile" reloadable="false"/>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。