最近在工作中需要上传简历文件,需求是可以上传word文档,但在前端预览时iframe不支持word预览,所以需要把word文档转成pdf,网上有很多种转换的方法,不列举出来,我使用的不需要引入jar包,显示效果接近,但速度慢,如果需求速度快的小伙伴可以选择其他方法
直接贴上代码
import java.io.File;
import java.io.FileInputStream;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Random;
/**
* 描述: word转PDf
*
* @param docUrl
* @return
* @auther
* @date 2018/9/14 11:22
*/
private String getWordToPdf(String docUrl) {
String fileUrl = docUrl;
try {
String suffix = docUrl.substring(docUrl.lastIndexOf("."), docUrl.length()).toLowerCase();
if (StringUtils.equals(suffix, ".doc") || StringUtils.equals(suffix, ".docx")) {
XDocService xDocService = new XDocService();
File file = new File(createFilePath());
xDocService.to(docUrl, file);
FileInputStream inputStream = new FileInputStream(file);
fileUrl = FileUtil.uploadFileForSurffix(inputStream, ".pdf", "resumeAttachment/");
file.delete();
}
} catch (Exception e) {
log.error("转换文件失败", e);
}
return fileUrl;
}
/**
* 描述: 生成文件路径
*
* @param
* @return
* @auther
* @date 2018/8/9 10:42
*/
private static String createFilePath() throws Exception {
//获取当前文件的根路径
File path = new File(ResourceUtils.getURL("classpath:").getPath());
if (!path.exists()) path = new File("");
//路径,盘符路径
StringBuilder codeUrl = new StringBuilder().append(path.getAbsolutePath()).append("/static/attachment");
String filePath = codeUrl.toString();
File filedir = new File(filePath);
if (!filedir.exists())
filedir.mkdirs();
codeUrl.append("/").append(getFileName()).append(".pdf");
return codeUrl.toString();
}
package com.dxyl.util.file;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.annotation.*;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* XDoc服务
*
* @author xdoc
* @version 11.4.2
*/
public class XDocService {
/**
* 默认服务器地址
*/
public static String DEFAULT_URL = "http://www.xdocin.com";
/**
* 默认账号口令
*/
public static String DEFAULT_KEY = "";
private String url;
private String key;
/**
* 服务地址
*
* @return
*/
public String getUrl() {
return url;
}
/**
* 服务地址
*
* @param url
*/
public void setUrl(String url) {
this.url = url;
}
/**
* 账号口令
*
* @return
*/
public String getKey() {
return key;
}
/**
* 账号口令
*
* @param key
*/
public void setKey(String key) {
this.key = key;
}
/**
* 构造器
*/
public XDocService() {
this(DEFAULT_URL, DEFAULT_KEY);
}
/**
* 构造器
*
* @param url 服务地址
*/
public XDocService(String url) {
this(url, DEFAULT_KEY);
}
/**
* 构造器
*
* @param url 服务地址
* @param key 账号
*/
public XDocService(String url, String key) {
this.url = url;
this.key = key;
}
/**
* 转换为其它格式文件
*
* @param xdoc xdoc
* @param file 其它格式文件,如:a.pdf
* @throws IOException
*/
public void to(File xdoc, File file) throws IOException {
to(xdoc.getAbsolutePath(), file);
}
/**
* 转换为其它格式文件
*
* @param xdoc xdoc文本<br>
* URL:文档URL地址,格式支持:xdoc、json、docx、epub、txt、rtf等,支持datauri协议,可传递二进制数据,支持本地文件<br>
* 纯文本:以"text:"开头的文本<br>
* JSON:符合XDOC-JSON规范的JSON文本<br>
* XML:符合XDOC-XML规范的XML文本<br>
* HTML:用html标签括起来的html文本,如:<html><h1>Hello</h1></html>
* @param file 其它格式文件,如:a.pdf
* @throws IOException
*/
public void to(String xdoc, File file) throws IOException {
to(xdoc, new FileOutputStream(file), getFormat(file.getName()));
}
/**
* 转换为其它格式,保存到指定流中
*
* @param xdoc xdoc
* @param out 输出目标,OutputStream或HttpServletResponse
* @param format format
* @throws IOException
*/
public void to(String xdoc, Object out, String format) throws IOException {
Map<String, Object> param = new HashMap<String, Object>();
param.put("_func", "to");
param.put("_xdoc", xdoc);
param.put("_format", format);
invoke(checkParam(param), out);
}
/**
* 转换为其它格式并发送
*
* @param xdoc xdoc
* @param to 目标,支持ftp、http、mail、datauri等
* @param format format
* @throws IOException
*/
public String to(String xdoc, String to, String format) throws IOException {
Map<String, Object> param = new HashMap<String, Object>();
param.put("_func", "to");
param.put("_xdoc", xdoc);
param.put("_to", to);
param.put("_format", format);
ByteArrayOutputStream out = new ByteArrayOutputStream();
invoke(checkParam(param), out);
return new String(out.toByteArray(), "UTF-8");
}
/**
* 运行xdoc
*
* @param xdoc xdoc
* @param param 参数
* @param file 目标文件
* @throws IOException
*/
public void run(File xdoc, Map<String, Object> param, File file) throws IOException {
if (!param.containsKey("_xformat")) {
param.put("_xformat", getFormat(file.getName()));
}
run(xdoc.getAbsolutePath(), param, file);
}
/**
* 运行xdoc
*
* @param xdoc xdoc
* @param param 参数
* @param file 目标文件
* @throws IOException
*/
public void run(String xdoc, Map<String, Object> param, File file) throws IOException {
run(xdoc, param, new FileOutputStream(file), getFormat(file.getName()));
}
/**
* 运行xdoc
*
* @param xdoc xdoc
* @param param 参数
* @param out 输出目标,OutputStream或HttpServletResponse
* @param format 目标格式
* @throws IOException
*/
public void run(String xdoc, Map<String, Object> param, Object out, String format) throws IOException {
param.put("_func", "run");
param.put("_xdoc", xdoc);
param.put("_format", format);
invoke(checkParam(param), out);
}
/**
* 运行xdoc并发送
*
* @param xdoc xdoc
* @param param 参数
* @param to 目标,支持ftp、http、mail、datauri等
* @param format 目标格式
* @throws IOException
*/
public String run(String xdoc, Map<String, Object> param, String to, String format) throws IOException {
param.put("_func", "run");
param.put("_xdoc", xdoc);
param.put("_to", to);
param.put("_format", format);
ByteArrayOutputStream out = new ByteArrayOutputStream();
invoke(checkParam(param), out);
return new String(out.toByteArray(), "UTF-8");
}
/**
* 运行注解XDoc
*
* @param obj
* @param file
* @throws IOException
*/
public void run(Object obj, File file) throws IOException {
run(obj, new FileOutputStream(file), getFormat(file.getName()));
}
/**
* 运行注解XDoc
*
* @param obj
* @param out 目标流
* @param format 目标格式
* @throws IOException
*/
public void run(Object obj, Object out, String format) throws IOException {
run(obj, out, null, format);
}
/**
* 运行注解XDoc
*
* @param obj
* @param to 目标,支持ftp、http、mail、datauri等
* @param format 目标格式
* @throws IOException
*/
public void run(Object obj, String to, String format) throws IOException {
run(obj, null, to, format);
}
private void run(Object obj, Object out, String to, String format) throws IOException {
String xurl = "";
XDoc xdoc = obj.getClass().getAnnotation(XDoc.class);
if (xdoc != null) {
xurl = xdoc.value();
}
if (xurl.length() == 0) {
xurl = "./" + obj.getClass().getSimpleName() + ".xdoc";
}
Field[] fields = obj.getClass().getDeclaredFields();
boolean hasXParam = false;
XParam xParam;
Map<String, Object> param = new HashMap<String, Object>();
String name;
Object value;
for (Field field : fields) {
xParam = field.getAnnotation(XParam.class);
if (xParam != null) {
hasXParam = true;
name = xParam.value();
if (name.length() == 0) {
name = field.getName();
}
try {
field.setAccessible(true);
value = field.get(obj);
if (name.equals("_xdoc")) {
xurl = String.valueOf(value);
} else {
param.put(name, value);
}
} catch (Exception e) {
throw new IOException(e);
}
}
}
if (!hasXParam) {
//没有指定xparam,传入所有属性
for (Field field : fields) {
try {
field.setAccessible(true);
param.put(field.getName(), field.get(obj));
} catch (Exception e) {