1
头图
In the article on , we used MinIO to implement file storage for storing files in various formats. Sometimes we need not only file storage, but also online preview of files. Here I recommend a SpringBoot-based file preview artifact kkFileView, which basically supports online preview of mainstream files, and is very easy to use. I hope it will help you!

SpringBoot actual combat e-commerce project mall (50k+star) address: https://github.com/macrozheng/mall

Introduction to kkFileView

kkFileView can be used to build an online file preview service, which has 5.7k+Star on Github. The project is built using the popular SpringBoot, easy to use and deploy, and basically supports online preview of mainstream office documents, such as docx, xlsx, pptx, pdf, txt, zip, pictures, video, audio, etc. Refer to the figure below for project characteristics.

Install

kkFileView supports installation under Windows and Linux. Below we introduce its installation, which is basically out of the box!

Windows

  • After the download is successful, unzip it to the specified directory, and then run the startup.bat command in the bin

  • Since it is a SpringBoot project, if you want to modify the configuration, you only need to modify the application.properties file in the config

Linux

  • It is very simple to use Docker installation under Linux, only two commands are required, first download the Docker image of kkFileView;
docker pull keking/kkfileview
  • After the download is complete, run the Docker container of kkfileview, and the service will run on port 8012
docker run -p 8012:8012 --name kkfileview \
-d keking/kkfileview

use

Before used MinIO to build an object storage service , then we will combine kkfileview with MinIO, first store the files that need to be previewed online in MinIO, and then preview the files.
  • We can use S3 Browser to manage the files in MinIO, first create a bucket for preview , and then upload all the files;

  • After the upload is successful, you need to modify the access policy of the bucket so that anonymous users can access;

  • When modifying the strategy, Policy Examples , pay attention to modify version to 2012-10-17 ;

  • After the modification is successful, the file can be previewed online. Let's look at the URL format of the online preview file. Just input the url (the file address that needs to be previewed). It should be noted that this url needs to be encoded base64
http://192.168.7.109:8012/onlinePreview?url=base64Encode($url)
  • Next, let's try to preview a picture, first get the access address of the picture;

  • Then put the url parameter into the access path to preview the picture, isn't it very simple!

  • Let's preview the word document again, we can find that there is a JPG on the right, which can switch to JPG. In fact, kkfileview realizes the document preview by converting the word document to pdf or JPG;

  • Let's preview the ppt document again, and find that the ppt document is also previewed by converting it into pdf and JPG;

  • Let's preview the pdf document again, there is no difference from the above two;

  • Let's preview the excel document again, the preview effect needs to be improved;

  • Let's preview the pure text Markdown document;

  • Finally, let's preview the compressed file. You can view the contents of the compressed file. Click the file to preview it directly.

Configuration

There are many configurations of kkfileview. For details application.properties refer to the config directory. Here is how to customize the configuration in the Docker container.
  • If you want to modify the configuration file of kkfileview, you can first copy the configuration file directory from the Docker container;
docker cp kkfileview:/opt/kkFileView-3.5.1/config /mydata/kkFileView/
  • Then mount the configuration directory, file directory, and log directory to the host and run;
docker run -p 8012:8012 --name kkfileview \
-v /mydata/kkFileView/config:/opt/kkFileView-3.5.1/config \
-v /mydata/kkFileView/file:/opt/kkFileView-3.5.1/file \
-v /mydata/kkFileView/log:/opt/kkFileView-3.5.1/log \
-d keking/kkfileview
  • If we want to add a watermark to the preview file, we can modify application.properties file, for example, I want to add the watermark macrozheng
#水印内容
#如需取消水印,内容设置为空即可,例:watermark.txt = ${WATERMARK_TXT:}
watermark.txt = ${WATERMARK_TXT:macrozheng}
  • After the modification is successful, preview the picture again and you will find that the watermark has been added.

Summarize

Through a wave of practice on kkFileView, we found that kkFileView can meet the online preview requirements of mainstream files such as Office documents, videos, and pictures, and it is also very simple to use and configure. Since it realizes preview by converting word and ppt documents into pdf, if you don't have high requirements for Office document preview, you can use it to build an all-round online file preview service!

Reference

Official document: https://kkfileview.keking.cn/zh-cn/index.html

This article GitHub https://github.com/macrozheng/mall-learning has been included, welcome to Star!

macrozheng
1.1k 声望1.3k 粉丝