一、工具类
package com.sby.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Locale;
import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import com.aspose.slides.Presentation;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
public class AsposeUtil {
public static boolean getLicense(int type) {
boolean result = false;
try {
InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml");
if (type == 1) {
com.aspose.cells.License aposeLic = new com.aspose.cells.License();
aposeLic.setLicense(is);
result = true;
} else if (type == 2) {
com.aspose.words.License aposeLic = new com.aspose.words.License();
aposeLic.setLicense(is);
result = true;
} else {
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
aposeLic.setLicense(is);
result = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static String Excel2Pdf(String officePath,String OutPutPath,String officeName) {
if (!getLicense(1)) {
return null;
}
try {
File file = new File(OutPutPath);
if (!file.exists()) {
file.mkdirs();
}
Workbook wb = new Workbook(officePath);
File pdfFile = new File(OutPutPath+officeName);
FileOutputStream fileOS = new FileOutputStream(pdfFile);
PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.setAllColumnsInOnePagePerSheet(true);
wb.save(fileOS, pdfSaveOptions);
return pdfFile.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
System.err.println("Excel2Pdf转换pdf错误");
}
return null;
}
public static String Word2Pdf(String officePath,String OutPutPath,String officeName) {
if (!getLicense(2)) {
return null;
}
try {
File file = new File(OutPutPath);
if (!file.exists()) {
file.mkdirs();
}
Document doc = new Document(officePath);
File pdfFile = new File(OutPutPath+officeName);
FileOutputStream fileOS = new FileOutputStream(pdfFile);
doc.save(fileOS, SaveFormat.PDF);
return pdfFile.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
System.err.println("word转换pdf错误");
}
return null;
}
public static String PPT2Pdf(String officePath,String OutPutPath,String officeName) {
if (!getLicense(3)) {
return null;
}
try {
File PathFile = new File(OutPutPath);
if (!PathFile.exists()) {
PathFile.mkdirs();
}
InputStream slides = new FileInputStream(new File(officePath));
Presentation pres = new Presentation(slides);
File file = new File(OutPutPath+officeName);
FileOutputStream fileOS = new FileOutputStream(file);
pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
return file.getAbsolutePath();
} catch (Exception e) {
e.printStackTrace();
System.err.println("ppt转换pdf错误");
}
return null;
}
public static String OfficeToPdf(String officePath) {
String[] split = officePath.split("⌒");
int lastIndex = split[0].lastIndexOf(".");
int lastNameIndex = split[0].lastIndexOf("\\");
String officeType = split[0].substring(lastIndex+1).toLowerCase(Locale.ROOT);
String officeName = split[0].substring(lastNameIndex+1,lastIndex)+".pdf";
String OutPutPath = split[0].substring(0,lastNameIndex+1)+"topdf/";
File file = new File(split[0]);
File pdfFile = new File(OutPutPath+officeName);
if(pdfFile.exists()){
return OutPutPath+officeName;
}
if (file.exists()) {
double bytes = file.length();
double kilobytes = (bytes / 1024);
double megabytes = (kilobytes / 1024);
DecimalFormat df = new DecimalFormat("0.00");
df.setRoundingMode(RoundingMode.HALF_UP);
String MB = df.format(megabytes);
Double Size = Double.parseDouble(MB);
if(Size>30){
return Size+"MB";
}
try {
if(officeType.equals("doc")||officeType.equals("docx")){
return Word2Pdf(split[0],OutPutPath,officeName);
}else if(officeType.equals("xls")||officeType.equals("xlsx")){
return Excel2Pdf(split[0],OutPutPath,officeName);
}else if(officeType.equals("ppt")||officeType.equals("pptx")){
return PPT2Pdf(split[0],OutPutPath,officeName);
}else{
System.err.println("无法识别该文件");
return "Error";
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
return "NotExists";
}
return OutPutPath+officeName;
}
}
相关jar包在我的资源里面下载
调用方法将不同文件类型分类处理后发送到前端预览,不在分类中的直接发送文件下载。
public void previewFile(Integer id, HttpServletResponse response) throws IOException {
FileInfo files = fileMapper.getFilesById(id);
String filePate = dir + "\\" + files.getAliasName();
String fileName = files.getAliasName();
String fileTyle = fileName.substring(fileName.lastIndexOf("."), fileName.length()).toUpperCase();
FileInputStream fileInputStream = new FileInputStream(filePate);
int available = fileInputStream.available();
if (available>(1024*1024*readonline) || fileTyle.equals(".DOCX") || fileTyle.equals(".DOC") || fileTyle.equals(".PPT") || fileTyle.equals(".PPTX") || fileTyle.equals(".XLS") || fileTyle.equals(".XLSX")) {
try{
fileInputStream = new FileInputStream(AsposeUtil.OfficeToPdf(filePate));
IOUtils.copy(fileInputStream, response.getOutputStream());
return;
}catch (Exception e){
log.error("officez转换pdf预览失败"+filePate);
System.err.println("officez转换pdf预览失败");
}
String filename = files.getFileName();
filename = java.net.URLEncoder.encode(filename, "UTF-8").replace("+", "%20");
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + filename);
IOUtils.copy(fileInputStream, response.getOutputStream());
} else if (!fileTyle.equals(".PNG") && !fileTyle.equals(".JPG") && !fileTyle.equals(".JPEG") && !fileTyle.equals(".PDF")) {
BufferedReader br = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(line);
response.getWriter().write("<br/>");
}
} else {
IOUtils.copy(fileInputStream, response.getOutputStream());
}
}
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose.slides</artifactId>
<version>15.9.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/aspose.slides-15.9.0.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose.cells.java</artifactId>
<version>18.11</version>
<scope>system</scope>
<systemPath>${basedir}/lib/aspose-cells-java-18.11.jar</systemPath>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose.words</artifactId>
<version>15.8.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/aspose-words-15.8.0.jar</systemPath>
</dependency>