springboot 实现上传doc&docx文件格式转pdf在线预览
实现效果
1、点击上传接口文档 2、显示上传提示 3、查看项目对应的所有接口文档,点击文档查看接口详情
pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-local</artifactId>
<version>1.1.5</version>
</dependency>
<dependency>
<groupId>com.documents4j</groupId>
<artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.1.5</version>
</dependency>
controller
@PostMapping("/project/upload/{id}")
public ResultVO<?> fileupload(MultipartFile file, HttpServletRequest req,@PathVariable Integer id) throws FileNotFoundException {
Boolean bool = projectService.uploadInterfaceWord(file, req, id);
return bool ? ResultVO.ok("上传成功") : ResultVO.fail("上传失败");
}
service
主要核心代码和逻辑 : 前端限制只能上传doc,docx,pdf文档格式,后端生成32位uuid为新的文件名,得到文件的扩展名,拼接生成新的文件。保存在当前项目下的upload目录中。然后判断本地生成文件的后缀,如果为pdf后缀,直接把虚拟路径和后缀保存到数据库中,如果为doc后缀或docx,就转换成pdf。在把新的文件虚拟路径和后缀保存到数据库。
@Override
public Boolean uploadInterfaceWord(MultipartFile file, HttpServletRequest req, Integer id) {
String randomNumber = UUID.randomUUID().toString().replace("-", "");
String oldFilename = file.getOriginalFilename();
String extension = oldFilename.substring(oldFilename.lastIndexOf("."));
String newFileName = randomNumber + extension;
String property = System.getProperty("user.dir")+"\\upload";
File dateDir = new File(property);
if (!dateDir.exists()) {
dateDir.mkdirs();
}
try {
file.transferTo(new File(dateDir, newFileName));
} catch (IOException e) {
e.printStackTrace();
return false;
}
String invented_address = null;
String fileSuffix = null;
if (extension.equals(".pdf")){
invented_address = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort() + "/api/file/" + newFileName;
fileSuffix = extension.substring(extension.indexOf(".") + 1);
}
else if (extension.equals(".doc") || extension.equals(".docx")) {
String newPdfName = randomNumber+".pdf";
PdfUtil.doc2Pdf(new File(dateDir, newFileName), new File(dateDir, newPdfName), extension);
invented_address = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort() + "/api/file/" + newPdfName;
fileSuffix = extension.substring(extension.indexOf(".") + 1);
}
InterfaceDoc interfaceDoc = new InterfaceDoc();
interfaceDoc.setProjectId(id);
interfaceDoc.setUrlAddress(invented_address);
interfaceDoc.setSuffix(fileSuffix);
interfaceDoc.setDocName(oldFilename);
interfaceDocMapper.insert(interfaceDoc);
return true;
}
domain
@Data
@TableName("tb_interface_doc")
public class InterfaceDoc extends SuperEntity{
@TableField("url_address")
private String urlAddress;
@TableField("doc_name")
private String docName;
@TableField("suffix")
private String suffix;
@TableField("project_id")
private Integer projectId;
}
SpringMvc虚拟路径映射
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Value("${file.staticAccessPath}")
private String staticAccessPath;
@Value("${file.uploadFolder}")
private String uploadFolder;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String property = System.getProperty("user.dir")+"\\upload\\";
registry.addResourceHandler(staticAccessPath).addResourceLocations("file:"+property);
}
}
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>查看接口文档</title>
<link rel="stylesheet" th:href="@{/component/pear/css/pear.css}" />
</head>
<body class="pear-container">
<div>
<div class="layui-row layui-col-space10">
<div class="layui-col-xs6 layui-col-md3" th:each="item:${InterfaceDocList}">
<div class="layui-card top-panel">
<div class="layui-card-header">[[${item.docName}]]</div>
<div class="layui-card-body">
<div class="layui-row layui-col-space5">
<div class="layui-col-xs8 layui-col-md8 top-panel-number" style="color: #28333E;">
<a th:href="@{${item.urlAddress}}" target="_blank">
<img th:src="@{'/admin/images/'+${item.suffix}+'.png'}" alt="查看详情" style="width: 100px;height: 100px;border-radius: 5px;" />
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script th:src="@{/component/layui/layui.js}"></script>
<script th:src="@{/component/pear/pear.js}"></script>
</html>
Bug
部署到Linux访问失败
如果部署到linux服务器中,会出现问题,文件上传到了cms\upload/ 目录下,访问是读取不到的,因为我们的目录为windos的\ 。 Linux的目录层级方式/usr/local/cms 而windos的目录层级方式F:\springboot\cms ,他们的目录斜杠方式相反。
解决: 创建UploadPathUtil 工具类根据不同系统生成不同的目录
@Slf4j
public class UploadPathUtil {
public static String getSystemRoot() {
String osName = System.getProperty("os.name");
String windowsPath = System.getProperty("user.dir")+"\\upload\\";
String linuxPath = System.getProperty("user.dir")+"/upload/";
log.info("当前系统为: "+osName);
if (osName.toLowerCase().startsWith("linux")) {
return linuxPath;
} else if (osName.toLowerCase().startsWith("windows")) {
return windowsPath;
} else {
throw new RuntimeException("错误目录");
}
}
public static void main(String[] args) {
String systemRoot = UploadPathUtil.getSystemRoot();
System.out.println(systemRoot);
}
}
修改虚拟路径配置类
修改上传文件目录不存在新建目录逻辑
部署到Linux服务器中doc转pdf失败
在本机windows转换成功
打包部署到linux服务器中转换失败 这里我以为是转换去原因,修改转换器重新打包部署还是报错
IConverter converter = LocalConverter.builder().baseFolder(new File(systemRoot))
.workerPool(20,25,1, TimeUnit.SECONDS)
.processTimeout(5,TimeUnit.SECONDS)
.build();
无法运行脚本:/usr/local/cms/upload/word_start1775189097.vbs
查找原因https://github.com/documents4j/documents4j/issues/41 貌似再说使用documents4j获得正确的转换。需要windows服务器(远程API)和一个office安装。
具体解决方案查看我的另一篇文章springboot实现上传doc&docx文件格式转html在线预览v2.0
|