遇到了一个需要在线预览的问题,在网上搜了搜,五花八门,选择最简单的把所有浏览器不能直接打开的全转成pdf格式,最后选择了aspose(可以再服务器上使用),以下是实现的过程和jar包下载地址,maven依赖:
如果是其他版本,license不通用,请看我的另一个文章(亲测20.6,其他未实验)aspose破解
1前端js,没什么说的,就一个打开
window.open(Feng.ctxPath + '/supervisorfileUp/yulan?supervisorfileUpid='+data.supervisorfileUpid);
2后端java先简单处理一下
@RequestMapping("/yulan")
@ResponseBody
public void downLoad(HttpServletRequest request, HttpServletResponse response) throws Exception {
Documentfile doc = this.documentfileService.getById(request.getParameter("documentfileId"));
String url=quurl(request.getParameter("documentfileId"));//传给quurl方法,获取最后生成的文件地址
if (url.equals("null")){//特殊格式目前不支持
response.sendError(404, "File not found!");
return;
}
response.reset(); // 非常重要
//在线预览,浏览器支持的文件类型都能打开
URL u = new URL("file:///" + url);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + doc.getDocumentfileUpname());
BufferedInputStream br = new BufferedInputStream(new FileInputStream(new File(url)));
byte[] buf = new byte[1024];
int len = 0;
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}
获取转换后的文件路径
public String quurl(String id){
Documentfile doc = this.documentfileService.getById(id);
String filename=doc.getDocumentfileUpname();//获取文件名
String fileurl=doc.getDocumentfileUpurl().replace("\\","");//获取url
String houzhui = filename.substring(filename.lastIndexOf('.')+1);//获取后缀
String wenjianming=filename.substring(0, filename.lastIndexOf("."));//获取文件名不带后缀
String path= ConstantsContext.getyulanhuancunPath();//获取配置文件中的保存转换后文件的路径
if(houzhui.equals("pptx")){
houzhui="ppt";
}
if (houzhui.equals("docx")||houzhui.equals("doc")){
houzhui="word";
}
if (houzhui.equals("xlsx")||houzhui.equals("xls")){
houzhui="excel";
}
System.out.println("houzhui::::::"+houzhui);
String url=doc.getDocumentfileUpurl();
if (houzhui.equals("word")||houzhui.equals("excel")||houzhui.equals("ppt")){
TopdfUtil.trans(fileurl+filename,path+wenjianming+".pdf",houzhui);
return path+wenjianming+".pdf";
}else if(houzhui.equals("pdf")||houzhui.equals("jpg")||houzhui.equals("bmp")||houzhui.equals("txt")||houzhui.equals("img")||houzhui.equals("png")){
return url+"\\"+filename;
}else{
return "null";//其余类型不管,基本够用了
}
}
最主要的转pdf格式
package cn.stylefeng.guns.modular.projectda.controller;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;
import com.aspose.words.*;
public class TopdfUtil {
//校验license
private static boolean judgeLicense() {
boolean result = false;
try {
InputStream is = TopdfUtil.class.getClassLoader().getResourceAsStream("license.xml");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 转换
public static void trans(String filePath, String pdfPath, String type) {
if (!judgeLicense()) {
System.out.println("license错误");
}
try {
System.out.println("as开始:" + filePath);
long old = System.currentTimeMillis();
File file = new File(pdfPath);
toPdf(file, filePath, type);
long now = System.currentTimeMillis();
System.out.println("完成:" + pdfPath);
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
}
private static void toPdf(File file, String filePath, String type) {
if ("word".equals(type) || "txt".equals(type)) {
wordofpdf(file, filePath);
} else if ("excel".equals(type)) {
exceOfPdf(file, filePath);
} else if ("ppt".equals(type)) {
pptofpdf(file, filePath);
}else{
System.out.println("暂不支持该类型:"+type);
}
}
private static void wordofpdf(File file, String filePath) {
FileOutputStream os = null;
Document doc;
try {
os = new FileOutputStream(file);
doc = new Document(filePath);
doc.save(os, com.aspose.words.SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void exceOfPdf(File file, String filePath) {
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
Workbook wb = new Workbook(filePath);
wb.save(os, com.aspose.cells.SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void pptofpdf(File file, String filePath) {
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
Presentation pres = new Presentation(filePath);// 输入pdf路径
pres.save(os, SaveFormat.Pdf);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
转格式的代码在https://www.cnblogs.com/excellencesy/p/11603892.html找到的,作者介绍很详细,文件也很全,不管他看不看得见,先行感谢.
还有很多需要优化的地方,但是没空,毕竟功能实现了,优化是后面的事,忘了就是没这回事了,如果有时间可以做成工具类.
问题:excel文件太宽的话格式会变,毕竟免费的也能接受
ppt和pptx文件转完有水印,license确定是可以用的,可能是jar包的问题
需要一个license和四个jar包
下载地址:https://pan.baidu.com/s/1Npn03RwDYV1TNfULZ3BLEw
提取码:zi6z
maven项目(注意仓库是阿里会下不下来,修改setting.xml):
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.6</version>
<classifier>jdk17</classifier>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>20.6</version>
<classifier>javadoc</classifier>
</dependency>
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
</repositories>