Pageoffice结合fastdfs在线编辑预览office⽂档
背景:我们项⽬的⽂档是存放在fastdfs服务器上⾯的,现有需求需要实现将fastdfs上⾯的各种office⽂档在⽹站页⾯进⾏预览和编辑保存
⽅案:由于fastdfs⽬前是不⽀持⽂档的更新的,所以只能通过插⼊新⽂档,删除⽼⽂档的⽅式来实现,同时需要将原有数据库表中存储的⽂件uuid给更新掉,然后pageoffice只能⽀持⽂档在本服务器内的预览和编辑,因此需要在应⽤服务器中先下载fastdfs服务器中的⽂件,然后本地保存为临时⽂件,然后给pageoffice预览编辑,保存后再通过删除和插⼊来更新fastdfs的⽂件,从⽽达到⽬的。
补充:需要定时清理由此产⽣的应⽤服务器下载的临时⽂件
⼤部分代码可以参考pageoffice官⽹⽂档中的
PageOffice 4.5 for JAVA (同时⽀持IE、Chrome、Firefox版)
PageOffice 4.5 for Spring Boot[⽰例代码]
香烟爱上火柴dj放代码:
pageoffice初始配置类
@Configuration
public class PageOfficeConfiguration {
/**
* 添加PageOffice的服务器端授权程序Servlet(必须)
*/
public ServletRegistrationBean servletRegistrationBean() {
com.zhuozhengsoft.pageoffice.poserver.Server poserver = new com.zhuozhengsoft.pageoffice.poserver.Server();
刘嘉玲年轻的时候发生了什么
//设置PageOffice注册成功后,license.lic⽂件存放的⽬录
//服务器可以存放在/geelyapp/pageoffice
poserver.setSysPath("d:\\lic\\");
ServletRegistrationBean srb = new ServletRegistrationBean(poserver);
srb.addUrlMappings("/xx/");
srb.addUrlMappings("/xx/");
srb.addUrlMappings("/xx/pageoffice/pageoffice.js");
srb.addUrlMappings("/xx/pageoffice/jquery.min.js");
srb.addUrlMappings("/xx/pageoffice/pobstyle.css");
srb.addUrlMappings("/xx/");
return srb;//
}
}
应⽤controller类
@RequestMapping("/xx/pageoffice")
@Api(value = "⽂档在线编辑接⼝")
public class PageOfficeController {
@Value("${pageoffice.path.doc:'D:/doc/'}")
private String pageofficeDocPath;
@Autowired
private StorageService storageService;//fastdfs封装服务类
@Autowired
private ICommonUtilService commonUtilService;
private static final Logger LOGGER = Logger(PageOfficeController.class);
@RequestMapping(value="/index", method=RequestMethod.GET)
public ModelAndView showIndex(
@ApiParam(name = "busi", required = true, value = "业务代码")
@RequestParam(name = "busi", required = true) String busi,
@ApiParam(name = "filePath", required = true, value = "⽂件远程路径(相对路径,不是包含http的全路径)")
@RequestParam(name = "filePath", required = true) String filePath,
Map<String,Object> map) {
map.put("busi",busi);
map.put("filePath",filePath);
ModelAndView mv = new ModelAndView("Index");
return mv;
}
@RequestMapping(value="/word", method=RequestMethod.GET)
public ModelAndView showWord(HttpServletRequest request, Map<String,Object> map,
kennyrogers@ApiParam(name = "busi", required = false, value = "业务代码")
@RequestParam(name = "busi", required = false) String busi,
@ApiParam(name = "filePath", required = false, value = "⽂件远程路径(相对路径,不是包含http的全路径)")
每一次和你分开 深深的被你打败@RequestParam(name = "filePath", required = false) String filePath){
String[] param = busi.split("%");//这⾥因为使⽤&符合传递多个参数时会导致被编译为&;导致⽆法调⽤成功
busi = param[0];
filePath = param[2];
//        pageofficeDocPath = "D:\\doc\\"; //本地开发环境时可使⽤
String[] filePathArr = filePath.split("/");
String url = pageofficeDocPath+filePathArr[filePathArr.length-1];
LOGGER.info("showWord url:"+url);
File tmpFile = new File(url);
try (FileOutputStream fos = new FileOutputStream(tmpFile)){
fos.write(adFileContent(filePath));
} catch (Exception e) {
<("预览临时⽂件⽣成错误",e);
throw BusinessException.withErrorCode(Errors.System.ILLEAGAL_DATA);
}
//--- PageOffice的调⽤代码 开始 -----
PageOfficeCtrl poCtrl=new PageOfficeCtrl(request);
poCtrl.setServerPage("/xx/");//设置授权程序servlet
poCtrl.addCustomToolButton("保存","Save",1); //添加⾃定义按钮
poCtrl.setSaveFilePage("/xx/pageoffice/save?filePath="+filePath+"&busi="+busi+"&url="+url);//设置保存时的action        String prefix = "file://";//linux服务器查⽂件需要额外前缀
url = prefix + url;
LOGGER.info("showWord pageoffice url:"+url);
dsWith(".doc")||dsWith(".docx")) {
poCtrl.webOpen(url,OpenModeType.SrmUserName());
}else dsWith(".xls")||dsWith(".xlsx")) {
poCtrl.webOpen(url,OpenModeType.SrmUserName());
}else dsWith(".ppt")||dsWith(".pptx")) {
poCtrl.webOpen(url,OpenModeType.SrmUserName());
}
map.put("pageoffice",HtmlCode("PageOfficeCtrl1"));
//--- PageOffice的调⽤代码 结束 -----
ModelAndView mv = new ModelAndView("Word");
return mv;
}
@RequestMapping("/save")
public void saveFile(HttpServletRequest request, HttpServletResponse response,
@ApiParam(name = "busi", required = true, value = "业务代码")
@RequestParam(name = "busi", required = true) String busi,
@ApiParam(name = "url", required = false, value = "临时⽂件url")
@RequestParam(name = "url", required = false) String url,
@ApiParam(name = "filePath", required = true, value = "⽂件远程路径(相对路径,不是包含http的全路径)")        @RequestParam(name = "filePath", required = true) String filePath){
FileSaver fs = new FileSaver(request, response);
//由于fastdfs并不⽀持⽂件更新
//⽬前考虑删除原有的⽂件,重新⽣成⽂件
//TODO 后⾯可以考虑⽀持⽂件的更新,即框架来做删除插⼊,并且新插⼊的uuid需要跟删除的⼀致
String newCode = this.storageService.FileName(), fs.getFileStream());
//将原来业务表中的⽂档uuid更新
commonUtilService.updateFilePath(busi,filePath,newCode);
this.storageService.delete(filePath);
//        fs.saveToFile(url);
fs.close();
File tmpFile = new File(url);
tmpFile.delete();
}
}
Index.ftl
<!DOCTYPE html>
<html xmlns="/1999/xhtml">
<head>
<title>Index</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="pageoffice.js" id="po_js_main"></script>
</head>
<body>
<h1>PageOffice预览准备</h1>
<a href="javascript:POBrowser.openWindowModeless('/xx/pageoffice/word?
busi=${busi}%filePath%${filePath}','width=1200px;height=800px;');">打开⽂件 </a>
</body>
</html>
Word.ftl
<!DOCTYPE html>
<html xmlns="/1999/xhtml">
<head>
<title>Hello World!</title>
<script type="text/javascript">
function Save() {
}
</script>
<script type="text/javascript">
function AddSeal() {
try{
我叫王土地主题曲}
</script>
</head>
<body>
琅琊榜之风起长林结局<div >${pageoffice}</div>
</body>
</html>