您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Pageoffice如何結(jié)合fastdfs在線編輯及預(yù)覽office文檔,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
背景:我們項(xiàng)目的文檔是存放在fastdfs服務(wù)器上面的,現(xiàn)有需求需要實(shí)現(xiàn)將fastdfs上面的各種office文檔在網(wǎng)站頁面進(jìn)行預(yù)覽和編輯保存
方案:由于fastdfs目前是不支持文檔的更新的,所以只能通過插入新文檔,刪除老文檔的方式來實(shí)現(xiàn),同時(shí)需要將原有數(shù)據(jù)庫表中存儲(chǔ)的文件uuid給更新掉,然后pageoffice只能支持文檔在本服務(wù)器內(nèi)的預(yù)覽和編輯,因此需要在應(yīng)用服務(wù)器中先下載fastdfs服務(wù)器中的文件,然后本地保存為臨時(shí)文件,然后給pageoffice預(yù)覽編輯,保存后再通過刪除和插入來更新fastdfs的文件,從而達(dá)到目的。
補(bǔ)充:需要定時(shí)清理由此產(chǎn)生的應(yīng)用服務(wù)器下載的臨時(shí)文件
大部分代碼可以參考pageoffice官網(wǎng)文檔中的
PageOffice 4.5 for JAVA (同時(shí)支持IE、Chrome、Firefox版)
PageOffice 4.5 for Spring Boot[示例代碼]
放代碼:
pageoffice初始配置類
@Configuration
public class PageOfficeConfiguration {
/**
* 添加PageOffice的服務(wù)器端授權(quán)程序Servlet(必須)
*/
@Bean
public ServletRegistrationBean servletRegistrationBean() {
com.zhuozhengsoft.pageoffice.poserver.Server poserver = new com.zhuozhengsoft.pageoffice.poserver.Server();
//設(shè)置PageOffice注冊(cè)成功后,license.lic文件存放的目錄
//服務(wù)器可以存放在/geelyapp/pageoffice
poserver.setSysPath("d:\\lic\\");
ServletRegistrationBean srb = new ServletRegistrationBean(poserver);
srb.addUrlMappings("/xx/pageoffice/poserver.zz");
srb.addUrlMappings("/xx/pageoffice/posetup.exe");
srb.addUrlMappings("/xx/pageoffice/pageoffice.js");
srb.addUrlMappings("/xx/pageoffice/jquery.min.js");
srb.addUrlMappings("/xx/pageoffice/pobstyle.css");
srb.addUrlMappings("/xx/pageoffice/sealsetup.exe");
return srb;//
}
}
應(yīng)用controller類
@Controller
@RequestMapping("/xx/pageoffice")
@Api(value = "文檔在線編輯接口")
public class PageOfficeController {
@Value("${pageoffice.path.doc:'D:/doc/'}")
private String pageofficeDocPath;
@Autowired
private StorageService storageService;//fastdfs封裝服務(wù)類
@Autowired
private ICommonUtilService commonUtilService;
private static final Logger LOGGER = LoggerFactory.getLogger(PageOfficeController.class);
@RequestMapping(value="/index", method=RequestMethod.GET)
public ModelAndView showIndex(
@ApiParam(name = "busi", required = true, value = "業(yè)務(wù)代碼")
@RequestParam(name = "busi", required = true) String busi,
@ApiParam(name = "filePath", required = true, value = "文件遠(yuǎn)程路徑(相對(duì)路徑,不是包含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,
@ApiParam(name = "busi", required = false, value = "業(yè)務(wù)代碼")
@RequestParam(name = "busi", required = false) String busi,
@ApiParam(name = "filePath", required = false, value = "文件遠(yuǎn)程路徑(相對(duì)路徑,不是包含http的全路徑)")
@RequestParam(name = "filePath", required = false) String filePath){
String[] param = busi.split("%");//這里因?yàn)槭褂?amp;符合傳遞多個(gè)參數(shù)時(shí)會(huì)導(dǎo)致被編譯為&導(dǎo)致無法調(diào)用成功
busi = param[0];
filePath = param[2];
// pageofficeDocPath = "D:\\doc\\"; //本地開發(fā)環(huán)境時(shí)可使用
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(this.storageService.readFileContent(filePath));
} catch (Exception e) {
LOGGER.error("預(yù)覽臨時(shí)文件生成錯(cuò)誤",e);
throw BusinessException.withErrorCode(Errors.System.ILLEAGAL_DATA);
}
//--- PageOffice的調(diào)用代碼 開始 -----
PageOfficeCtrl poCtrl=new PageOfficeCtrl(request);
poCtrl.setServerPage("/xx/pageoffice/poserver.zz");//設(shè)置授權(quán)程序servlet
poCtrl.addCustomToolButton("保存","Save",1); //添加自定義按鈕
poCtrl.setSaveFilePage("/xx/pageoffice/save?filePath="+filePath+"&busi="+busi+"&url="+url);//設(shè)置保存時(shí)的action
String prefix = "file://";//linux服務(wù)器查找文件需要額外前綴
url = prefix + url;
LOGGER.info("showWord pageoffice url:"+url);
if(filePath.endsWith(".doc")||filePath.endsWith(".docx")) {
poCtrl.webOpen(url,OpenModeType.docAdmin,SessionContextUtil.getSrmUserName());
}else if(filePath.endsWith(".xls")||filePath.endsWith(".xlsx")) {
poCtrl.webOpen(url,OpenModeType.xlsNormalEdit,SessionContextUtil.getSrmUserName());
}else if(filePath.endsWith(".ppt")||filePath.endsWith(".pptx")) {
poCtrl.webOpen(url,OpenModeType.pptNormalEdit,SessionContextUtil.getSrmUserName());
}
map.put("pageoffice",poCtrl.getHtmlCode("PageOfficeCtrl1"));
//--- PageOffice的調(diào)用代碼 結(jié)束 -----
ModelAndView mv = new ModelAndView("Word");
return mv;
}
@RequestMapping("/save")
public void saveFile(HttpServletRequest request, HttpServletResponse response,
@ApiParam(name = "busi", required = true, value = "業(yè)務(wù)代碼")
@RequestParam(name = "busi", required = true) String busi,
@ApiParam(name = "url", required = false, value = "臨時(shí)文件url")
@RequestParam(name = "url", required = false) String url,
@ApiParam(name = "filePath", required = true, value = "文件遠(yuǎn)程路徑(相對(duì)路徑,不是包含http的全路徑)")
@RequestParam(name = "filePath", required = true) String filePath){
FileSaver fs = new FileSaver(request, response);
//由于fastdfs并不支持文件更新
//目前考慮刪除原有的文件,重新生成文件
//TODO 后面可以考慮支持文件的更新,即框架來做刪除插入,并且新插入的uuid需要跟刪除的一致
String newCode = this.storageService.store(fs.getFileName(), fs.getFileStream());
//將原來業(yè)務(wù)表中的文檔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="http://www.w3.org/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>
<h2>PageOffice預(yù)覽準(zhǔn)備</h2>
<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="http://www.w3.org/1999/xhtml">
<head>
<title>Hello World!</title>
<script type="text/javascript">
function Save() {
document.getElementById("PageOfficeCtrl1").WebSave();
}
</script>
<script type="text/javascript">
function AddSeal() {
try{
document.getElementById("PageOfficeCtrl1").ZoomSeal.AddSeal();
}catch (e){ };
}
</script>
</head>
<body>
<div >${pageoffice}</div>
</body>
</html>
以上就是Pageoffice如何結(jié)合fastdfs在線編輯及預(yù)覽office文檔,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。