Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize online document Preview in springboot2

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Springboot2 how to achieve online document preview, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

In the first step, you need to introduce the corresponding jar:

Org.jodconverter jodconverter-core 4.2.2 org.jodconverter jodconverter-spring-boot-starter 4.2.2 org.jodconverter jodconverter-local 4.2.2

Second, add key configurations to the configuration file:

Step 3: core class

Package com.yunji.kwxt.document;import com.yunji.kwxt.common.enums.ResultEnum;import com.yunji.kwxt.common.model.ResultJson;import org.apache.commons.io.IOUtils;import org.jodconverter.DocumentConverter;import org.jodconverter.office.OfficeException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource Import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletResponse;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream / * * @ author: LX * creation time: 2019-11-4. 17:42 * place: Guangzhou * purpose: online document preview * * generally speaking, this part of the code can be used, but the effect is not as good as expected. First, the Chinese garbled problem has not been solved, and second, the format is not as good as expected. * Business logic is not recommended. It is recommended to control the upload of files from the source, and then preview the operation. * * if you need to use it later, 1 release the jodconverter package in maven * 2 release the configuration of the corresponding jodconverter in the application-config.properties file * 3 release the view method under this class, and call view. * * remarks: * / @ Controller@RequestMapping ("/ doc") public class DocumentController {private static Logger log = LoggerFactory.getLogger (DocumentController.class); @ Resource private DocumentConverter documentConverter / * * online Preview * @ param response * @ return * / @ RequestMapping (value = "/ view", method = RequestMethod.GET) @ ResponseBody public ResultJson view (HttpServletResponse response) {/ / File to be converted File file = new File ("E:\\ download\\ kd.xlsx") / / the translated address File toFile = new File ("E:\\ temp"); if (! toFile.exists ()) {toFile.mkdirs ();} ServletOutputStream outputStream = null; InputStream in = null / / key method, convert to PDF try {documentConverter.convert (file) .to (new File ("E:/temp/1.pdf")) .execute (); outputStream = response.getOutputStream (); in = new FileInputStream (new File ("E:/temp/1.pdf")); / / copy files to stream IOUtils.copy (in, outputStream) } catch (OfficeException e) {e.printStackTrace (); log.error ("failed to convert file");} catch (IOException e) {e.printStackTrace (); log.error ("failed to get stream") } finally {if (in! = null) {try {in.close ();} catch (IOException e) {e.printStackTrace () }} if (outputStream! = null) {try {outputStream.close ();} catch (IOException e) {e.printStackTrace () } return new ResultJson (null, ResultEnum.SUCCESS.getStatus (), "success", null);}} after reading the above, have you mastered how to implement online document preview in springboot2? 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report