java实现查询Word是否包含批注和修订内容
1.业务需求发送邮件前确认附件word内容是否包含批注和修订内容如果包含不可发送邮件
2.前端代码如下
/获取发送邮件的信用评价报告word是否包含批注修订内容/
function findEnterpriseReport(post_Param,callback){
$.ajax({
type: "post",
url: baseURL + "sendmail/getEnterpriseReport",
dataType: "json",
contentType: "application/json",
data: JSON.stringify(post_Param),
success: function(r){
if(r.code === 1){
callback(true);
}else if(r.code === 0){
callback(false);
}
},error: function(m){
callback(false);
}
});
}
3.后端java代码如下
/**
* 获取选定项目的发送评级信用报告进行校验
* qimeng.zhang
* 2020-12-03
*/
@RequestMapping("/getEnterpriseReport")
//@RequiresPermissions("sendmail:ProjectListByuser")
public R getEnterpriseReport(@RequestBody Map<String, Object> params) throws Exception{
Map<String,Object> projectFile = sendmailService.getFasongqiyebaogao(params);
if(projectFile==null){
return R.ok().put("code", 1);
}else{
//获取拼接word全路径
String urlPath = (String) projectFile.get("url_path"); //远程文件路径
//服务器word文件路径
String remoteFilePath = "http://cn:8888/uploadFiles"+urlPath.substring(0,urlPath.lastIndexOf("/")+1)+URLEncoder.encode(urlPath.substring(urlPath.lastIndexOf("/")+1), "UTF-8");
//本地存储下载的word文件路径
String localFilePath = "/usr/local/tomcat/webapps/uploadFiles"+urlPath;
//从服务器下载文件到本地
FileUtil.downloadFile(remoteFilePath,localFilePath);
//加载测试文档
Document doc = new Document();
doc.loadFromFile(localFilePath);
//判断是否有修订
if(doc.hasChanges()){
//校验之后删除word文件夹
File file = new File("/usr/local/tomcat/webapps/uploadFiles");
//调用删除方法
delFiles(file);
return R.ok().put("code", 1);
}
//遍历所有批注
for(int i = 0;i< doc.getComments().getCount();i++){
Comment comment = doc.getComments().get(i);
//遍历所有批注中的段落
for(int j= 0;j < comment.getBody().getParagraphs().getCount();j++) {
Paragraph paragraph = comment.getBody().getParagraphs().get(j);
//遍历段落中的对象
for (Object object : paragraph.getChildObjects()) {
//读取文本
if (object instanceof TextRange) {
//校验之后删除word文件夹
File file = new File("/usr/local/tomcat/webapps/uploadFiles");
//调用删除方法
delFiles(file);
return R.ok().put("code", 1);
}
}
}
}
//校验之后删除word文件夹
File file = new File("/usr/local/tomcat/webapps/uploadFiles");
//调用删除方法
delFiles(file);
}
return R.ok();
}
/**
* 删除校验之后的信用评价报告
* qimeng.zhang
* 2020-12-03
* 递归删除
* 删除某个目录及目录下的所有子目录和文件
* @param file 文件或目录
* @return 删除结果
*/
public static boolean delFiles(File file){
boolean result = false;
//目录
if(file.isDirectory()){
File[] childrenFiles = file.listFiles();
for (File childFile:childrenFiles){
result = delFiles(childFile);
if(!result){
return result;
}
}
}
//删除 文件、空目录
result = file.delete();
return result;
}
4.java操作word参考地址
5.如果觉得对您有帮助请帮忙点点赞…谢谢