In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people are at a loss about the process of realizing the file online preview function in SpringBoot. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Background
Recently, the company's internal oa system upgrade, need to add file online preview service, the most common file is office document, at the beginning conceive several solutions, such as office software inherent file conversion, openoffice conversion, offce365 service, aspose component conversion, and finally adopted aspose conversion, because the component function is perfect, do not rely on other software installation environment
System design
File type and solution file type preview scheme wordaspsoe-word conversion picture preview (version 21.1) pptaspose-slides convert your picture preview (version 20.4) excelaspose-cell conversion html preview (version 20.4) pdfpdfbox slow picture preview (version 2.0.15) png,jpg,gif integrated viewer.js preview (version 1.5.0) mp4 integrated vedio.js preview (js version 7.10.2) txt read file content preview
Note: due to copyright problems in aspose, the trial version is used in all the sample code of the project, and the watermark will appear in the converted image.
Process design
System realization
Identify file suffix
When URL points to the real path of the file, it is judged by the suffix name.
Public static String getTypeByExtenssion (String linkUrl) {if (linkUrl = = null) return null; linkUrl = linkUrl.toLowerCase (); for (String ext: extensions) {if (linkUrl.endsWith (ext)) {return ext;}} return null;}
When URL is a file output stream
According to the disposition of the file output stream
Private static String getTypeByDisposition (String disposition) {String ext = null; if (! StringUtils.isEmpty (disposition)) {disposition = StringUtils.replace (disposition, "\", "); String [] strs = disposition.split ("; ") For (String string: strs) {if (string.toLowerCase (). IndexOf ("filename=") > = 0) {ext = StringUtils.substring (string, string.lastIndexOf (".")); break;} return ext;}
According to the file output stream content-type
Types = new HashMap (); types.put ("application/pdf", ".pdf"); types.put ("application/msword", ".doc"); types.put ("text/plain", ".txt"); types.put ("application/javascript", ".js"); types.put ("application/x-xls", ".xls") Types.put ("application/-excel", ".xls"); types.put ("text/html", ".html"); types.put ("application/x-rtf", ".rtf"); types.put ("application/x-ppt", ".ppt"); types.put ("image/jpeg", ".jpg") Types.put ("application/vnd.openxmlformats-officedocument.wordprocessingml.template", ".docx"); types.put ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".xlsx"); types.put ("application/vnd.openxmlformats-officedocument.presentationml.presentation", ".pptx"); types.put ("message/rfc822", ".eml"); types.put ("application/xml", ".xml")
Judging by the fixed bytes of stream
FILE_TYPE_MAP.put (".pdf", "255044462D312E"); / / Adobe Acrobat (pdf) FILE_TYPE_MAP.put (".doc", "D0CF11E0"); / / MS Word FILE_TYPE_MAP.put (".xls", "D0CF11E0"); / / MS Excel Note: the headers of word and excel are the same as FILE_TYPE_MAP.put (".jpg", "FFD8FF") / / JPEG (jpg) FILE_TYPE_MAP.put (".png", "89504E47"); / PNG (png) FILE_TYPE_MAP.put (".gif", "47494638"); / / GIF (gif) FILE_TYPE_MAP.put (".tif", "49492A00"); / / TIFF (tif) FILE_TYPE_MAP.put (".bmp", "424D") / / Windows Bitmap (bmp) FILE_TYPE_MAP.put (".dwg", "41433130"); / / CAD (dwg) FILE_TYPE_MAP.put (".html", "68746D6C3E"); / / HTML (html) FILE_TYPE_MAP.put (".rtf", "7B5C727466"); / / Rich Text Format (rtf) FILE_TYPE_MAP.put (".xml", "3C3F786D6C") FILE_TYPE_MAP.put (".zip", "504B0304"); / / the header of docx is the same as that of zip (".rar", "52617221"); FILE_TYPE_MAP.put (".psd", "38425053"); / / Photoshop (psd) FILE_TYPE_MAP.put (".eml", "44656C69766572792D646174653A") / / Email FILE_TYPE_MAP.put (".dbx", "CFAD12FEC5FD746F"); / / Outlook Express (dbx) FILE_TYPE_MAP.put (".pst", "2142444E"); / / Outlook (pst) FILE_TYPE_MAP.put (".mdb", "5374616E64617264204A"); / / MS Access (mdb) FILE_TYPE_MAP.put (".wpd", "FF575043") / / WordPerfect (wpd) FILE_TYPE_MAP.put (".eps", "252150532D41646F6265"); FILE_TYPE_MAP.put (".ps", "252150532D41646F6265"); FILE_TYPE_MAP.put (".qdf", "AC9EBD8F"); / / Quicken (qdf) FILE_TYPE_MAP.put (".pwl", "E3828596") / Windows Password (pwl) FILE_TYPE_MAP.put (".wav", "57415645"); / / Wave (wav) FILE_TYPE_MAP.put (".avi", "41564920"); FILE_TYPE_MAP.put (".ram", "2E7261FD"); / / Real Audio (ram) FILE_TYPE_MAP.put (".rm", "2E524D46") / Real Media (rm) FILE_TYPE_MAP.put (".mpg", "000001BA"); / / FILE_TYPE_MAP.put (".mov", "6D6F6F76"); / / Quicktime (mov) FILE_TYPE_MAP.put (".asf", "3026B2758E66CF11"); / / Windows Media (asf) FILE_TYPE_MAP.put (".mid", "4D546864"); / / MIDI (mid) file parsing
Word paging conversion picture
Document doc = new Document (fileConvertInfo.getFilePath ()); for (int I = 0; I
< doc.getPageCount(); i++) { Document extractedPage = doc.extractPages(i, 1); extractedPage.save(fileConvertInfo.getFileDirPath() + "split_" + (i + 1) + ".jpeg", SaveFormat.JPEG);} ppt分页转换图片 Presentation ppt = new Presentation(fileConvertInfo.getFilePath()); for (int i = 0; i < ppt.getSlides().size(); i++) { ISlide slide = ppt.getSlides().get_Item(i); int height = (int) (ppt.getSlideSize().getSize().getHeight() - 150); int width = (int) (ppt.getSlideSize().getSize().getWidth() - 150); BufferedImage image = slide.getThumbnail(new java.awt.Dimension(width, height)); //每一页输出一张图片 File outImage = new File(fileConvertInfo.getFileDirPath() + "split_" + (i + 1) + ".jpeg"); ImageIO.write(image, "jpeg", outImage);} excel转换html Workbook wb = new Workbook(fileConvertInfo.getFilePath());HtmlSaveOptions opts = new HtmlSaveOptions();opts.setExportWorksheetCSSSeparately(true);opts.setExportSimilarBorderStyle(true);Worksheet ws = wb.getWorksheets().get(0);wb.save(fileConvertInfo.getFileDirPath() + "convert.html", opts); excel分页转换图片(另一种预览方式) Workbook wb = new Workbook(fileConvertInfo.getFilePath());ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();imgOptions.setImageFormat(ImageFormat.getJpeg());for (int i = 0; i < wb.getWorksheets().getCount(); i++) { Worksheet sheet = wb.getWorksheets().get(i); SheetRender sr = new SheetRender(sheet, imgOptions); sr.toImage(i, fileConvertInfo.getFileDirPath() + "split_" + (i + 1) + ".jpeg");} pdf分页转换图片 PDDocument pdf = PDDocument.load(new File((fileConvertInfo.getFilePath())));int pageCount = pdf.getNumberOfPages();PDFRenderer renderer = new PDFRenderer(pdf);for (int i = 0; i < pageCount; i++) { BufferedImage image = renderer.renderImage(i, 1.25f); // 第二个参数越大生成图片分辨率越高,转换时间也就越长 ImageIO.write(image, "JPEG", new File(fileConvertInfo.getFileDirPath() + "split_" + (i + 1) + ".jpeg"));}pdf.close(); 预览图片(function () {$("# image"). Attr ("src", getQueryString ("file")); var image = new Viewer (document.getElementById ('image'), {url:' data-original', button:false, navbar:false, backdrop: false}); document.getElementById ('image'). Click ();})
Preview video
(function () {$("# vedio") .attr ("src", getQueryString ("file"));}) system effect
Usage
Run the project directly and enter the preview address
Http://localhost:8098/fastpreview/plaform/index.html?file= (file address)
File address supports file access path and stream output
Project source address
Gitee.com/code2roc/fa...
Preview interface
Word
Excel
Ppt
Image
Vedio
Txt
After reading the above, have you mastered the process of realizing the file online preview function by SpringBoot? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.