1、点击上传接口文档
2、显示上传提示
3、查看项目对应的所有接口文档,点击文档查看接口详情
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.documents4jgroupId>
<artifactId>documents4j-localartifactId>
<version>1.1.5version>
dependency>
<dependency>
<groupId>com.documents4jgroupId>
<artifactId>documents4j-transformer-msoffice-wordartifactId>
<version>1.1.5version>
dependency>
@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("上传失败");
}
主要核心代码和逻辑 : 前端限制只能上传doc,docx,pdf文档格式,后端生成32位uuid为新的文件名,得到文件的扩展名,拼接生成新的文件。保存在当前项目下的upload目录中。然后判断本地生成文件的后缀,如果为pdf后缀,直接把虚拟路径和后缀保存到数据库中,如果为doc后缀或docx,就转换成pdf。在把新的文件虚拟路径和后缀保存到数据库。
/**
* 上传接口文档
* @param file
* @param req
* @param id
* @return
*/
@Override
public Boolean uploadInterfaceWord(MultipartFile file, HttpServletRequest req, Integer id) {
//使用UUID生成唯一标识文件名
String randomNumber = UUID.randomUUID().toString().replace("-", "");
//获取文件的原始名
String oldFilename = file.getOriginalFilename();
//获取文件后缀 .pdf
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")){
// 上传文件为.pdf后缀的虚拟访问路径
invented_address = req.getScheme()+"://"+req.getServerName()+":"+req.getServerPort() + "/api/file/" + newFileName;
// 添加后缀
fileSuffix = extension.substring(extension.indexOf(".") + 1);
}
// doc docx转pdf在保存地址到数据库中
else if (extension.equals(".doc") || extension.equals(".docx")) {
// 转换之后的pdf文件
String newPdfName = randomNumber+".pdf";
PdfUtil.doc2Pdf(new File(dateDir, newFileName), new File(dateDir, newPdfName), extension);
// 转换之后的pdf文件虚拟路径
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;
}
@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;
}
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Value("${file.staticAccessPath}")
private String staticAccessPath;
@Value("${file.uploadFolder}")
private String uploadFolder;
/**
* 虚拟路径映射
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String property = System.getProperty("user.dir")+"\\upload\\";
//registry.addResourceHandler(staticAccessPath).addResourceLocations("file:"+uploadFolder);
registry.addResourceHandler(staticAccessPath).addResourceLocations("file:"+property);
}
}
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>
如果部署到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");
// windows目录
String windowsPath = System.getProperty("user.dir")+"\\upload\\";
// linux目录
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服务器中转换失败
这里我以为是转换去原因,修改转换器重新打包部署还是报错
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