記錄一下,實現各種文檔在線預覽的功能
阿新 • • 發佈:2019-03-09
mem util oid vat filename sys vax xls trac
首先是通過方法把對應的文檔編譯成為對應的pdf文件,然後再瀏覽器打開對應的pdf文件。從而實現預覽效果;
但要註意的是,預覽打開的url存在中文問題,我這邊始終存在因為轉義,而提示無法找到對應的pdf文件的問題。最後是再文檔編譯的時候使用文檔再數據庫對應的ID作為pdf名稱編譯出來,然後打開。
一下是後臺對應的代碼;
這裏提供兩張編譯方式,我所使用的是編譯成pdf文件。另一種是編譯成html文件。
要註意的是,需要再WEB-INF下面加上classes包,內容在附件中(這些內容必不可少;是代碼在編譯時用到的配置文件);
package ths.project.EnterService.util.attachments.web; import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.aspectj.weaver.patterns.HasMemberTypePattern; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.google.gson.JsonArray; import com.sun.org.apache.bcel.internal.generic.NEW; import ths.jdp.core.dao.base.Paging; import ths.jdp.core.model.FormModel; import ths.jdp.project.web.LoginCache; import ths.project.EnterService.user.service.UserAccountRelationService; import ths.project.EnterService.util.attachments.service.AttachmentsService; import ths.project.EnterService.util.toPdf.Excel2Pdf; import ths.project.EnterService.util.toPdf.PowerPoint2Pdf; import ths.project.EnterService.util.toPdf.WordToPdf; /** * 附件在線預覽 * * @author xwy * @since 2019-3-8 */ @Controller @RequestMapping("/attachments") public class AttachmentsController { @Autowired private AttachmentsService attachmentsService; /** * 在線預覽 查詢表單對應的附件信息 * @param fileName * forcePreview 強制轉換 */ @RequestMapping("/file") public ModelAndView file(@RequestParam String ext,String business_key,Paging<Map<String, Object>> pageInfo, HttpServletRequest request){ ModelAndView result = new ModelAndView("attachments/file"); Map<String,Object> map=new HashMap<String,Object>(); Paging<Map<String, Object>> dateMap = new Paging<Map<String, Object>>(); map.put("BUSINESS_KEY", business_key); map.put("EXT", ext); dateMap=attachmentsService.getBusinesskey(pageInfo,map); result.addObject("pageInfo",dateMap); return result; } /** * 在線預覽 * @param fileName * forcePreview 強制轉 */ @RequestMapping("/previewToPdf") public ModelAndView previewToPdf(@RequestParam String fileid,String forcePreview,HttpServletRequest request){ ModelAndView result = new ModelAndView("attachments/showPdf"); Map<String,Object> map=new HashMap<String,Object>(); map.put("FILE_ID", fileid); map=attachmentsService.getFileid(map); String path=(String)map.get("FILE_NAME"); String name=(String)map.get("FILE_PATH"); // String filePath=request.getRealPath("attachments")+"\\"+path;//獲取文件絕對路徑 File f = new File(request.getRealPath("attachments")+"\\"+fileid+".pdf");//判斷文件是否已經生成 boolean hasToPDF = f.exists(); // result.addObject("path",ajaxToChinse(path.substring(0,path.lastIndexOf("."))+".pdf"));//展示路徑 name+"\\"+ result.addObject("fileid",fileid); if("force".equals(forcePreview)||!hasToPDF){//查看是否已經存在編譯好的;如果是強制轉換 則轉換,或者沒有轉換*/ try{ ToPdf( filePath,filePath,request.getRealPath("attachments")+"\\"+fileid+".pdf"); //這裏進行編譯對應的文檔 }catch(Exception e){ result.addObject("path","");//清空路徑 result.addObject("error", "異常:"+e.getMessage()); } } result.addObject("path",fileid+".pdf"); return result; } /** * 文件轉換 方便實現在線預覽 * @param filePath * @param path */ public void ToPdf(String filePath,String path,String paths) throws Exception{ System.out.println("開始文件類型轉換,文件原始路徑:"+filePath); String fileExt = path.substring(path.lastIndexOf(".") + 1).toLowerCase();//文件後綴 if("doc".equals(fileExt) || "docx".equals(fileExt)){ WordToPdf.wordToOther(filePath,"pdf",paths); } else if("xls".equals(fileExt) || "xlsx".equals(fileExt)){ Excel2Pdf.convertFile(filePath, "pdf"); } else if("ppt".equals(fileExt) || "pptx".equals(fileExt)){ PowerPoint2Pdf.convertFile(filePath, "pdf"); }else{ throw new Exception("不支持在線預覽"); } } //ajax 中文傳參之後解碼 public String ajaxToChinse(String code){ String val1 = null; String val2 = null; if(code!=null && !("").equals(code)){ try { val1= URLDecoder.decode(code, "UTF-8"); val2= URLDecoder.decode(val1, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return val2; } }
記錄一下,實現各種文檔在線預覽的功能