Office Online Preview-Yongzhong

overview

Official website: http://api.yozocloud.cn/ 

Advantages: very cheap (a domain name is 3000 a year), the ease of use is close to 0, can be deployed on the intranet alone, and can preview office packages: word, excel, ppf, pdf, pictures, txt and many other document types; (use: You only need to provide a file download interface under a domain name, and the registered account has 500 preview experiences).

Disadvantages: 1. The preview of files exceeding 10M on the public network is very slow; 2. Security: Although watermarks and anti-copy parameters are provided, for the safety of files, you still need to develop a file download IP whitelist filter by yourself; (also provide direct request files The preview address interface does not expose the file download address).

Example: technical architecture: springboot2+freemark2.26+tk.mybatis2.0.4+pagehelper1.2.12 file upload and download, paging query ( Spring Boot integrated MyBatis, paging plug-in PageHelper, general Mapper )

 

This blog contains three modules: 1. Introduction to Yongzhong Documentation Technical College; 2. Introduction to code-related technologies and development processes; 3. Introduction to related pom dependencies and configurations

Code download address: https://download.csdn.net/download/qq_26408545/12339351

Code preview address: http://www.javakly.com:8080 , effective within 7 days from 2020-04-19: the effect is as follows

 1: Introduction of Yongzhong Documentation Technical Secondary School Station

 Use 3 steps: 1. Go to the official website to register an account, configure the domain name information, and get the key official website address: https://api.yozocloud.cn

 2. Splicing preview address according to your file download address: 2 ways

  1. The first method: directly according to the download address and key: the preview address of the spliced ​​file:

Such as: http://dcsapi.com/?k=1744232&url=http://58.215.166.234/example/doc/doctest.docx

http://Yongzhong file preview URL+?k=( user Key corresponding to the domain name )+&url=( online document download address ) +&isCopy=( whether anti-copy 1 is 0 no, optional ) +&watermaerk=( watermark text content, optional )

3 optional parameters: 1. watermark: String format. Set the watermark content for a single document. 2. isCopy: Integer format (0 is no, 1 is, the default is 0. Whether to prevent copying for a single document). 3. noCache: Boolean format. Whether to force re-conversion (ignore cache), true is to force re-conversion, false is not to force re-conversion.

The second method: the user obtains the preview address by calling the api

Request address: http://api.yozocloud.cn/getPreview?k=123456&url=http://abc.com/123.doc

Description: http://api URL / getPreview +?k=( user Key corresponding to domain name )+&url=( online document address ) +&isCopy=( whether anti-copy 1 yes 0 no, optional ) +&watermaerk=( watermark text content, optional )

Get json: errorcode=0 means the request is successful, get data.data, you can directly open it through the URL;

{
    "data":{
            "data":"https://p.dcsapi.com/view/preview/xxxxxxxx/"
        },
        "message":"操作成功",
        "errorcode":0
}

 Two: Code-related technology and development process introduction

 

Code template reference: https://github.com/abel533/MyBatis-Spring-Boot : Spring Boot integrates MyBatis, paging plug-in PageHelper, general Mapper 

2.1 The general mapper of tk.mybatis realizes the generation of reverse engineering

1. First create a table in the database.

DROP TABLE IF EXISTS sys_files;
CREATE TABLE sys_files (
  file_id int(11) NOT NULL AUTO_INCREMENT comment '文件主键',
  file_name varchar(255) NOT NULL comment '文件名称',
	file_new_name varchar(255) NOT NULL comment '文件新名字',
	file_type varchar(500) NOT NULL comment '文件类型',
	file_url varchar(500) NOT NULL comment '文件下载地址',
	file_Show_url varchar(255) NOT NULL comment '文件预览地址',
  create_by varchar(64) default '' comment '创建者',
  create_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '创建时间',
	update_by varchar(64) default '' comment '更新者',
  update_time datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP comment '更新时间',
  PRIMARY KEY (file_id),
	INDEX(file_name),
  INDEX(file_type)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 comment '系统文件';

2. Then add mybatis and general mapper dependencies to the pom, as well as generator plug-ins, see 3 for generator configuration.

<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<dependencies>
					<dependency>
						<groupId>org.springframework</groupId>
						<artifactId>springloaded</artifactId>
						<version>1.2.5.RELEASE</version>
					</dependency>
				</dependencies>
			</plugin>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.2</version>
				<configuration>
					<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
					<overwrite>true</overwrite>
					<verbose>true</verbose>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>${mysql.version}</version>
					</dependency>
					<dependency>
						<groupId>tk.mybatis</groupId>
						<artifactId>mapper-generator</artifactId>
						<version>1.0.0</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>

3. You need to inherit the general template in tk.mybatis in MyMapper in utils, and add the table to be reverse generated.

package com.javakly.utils;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
    //TODO
    //FIXME 特别注意,该接口不能被扫描到,否则会出错
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <properties resource="application-dev.properties"/>

    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="com.javakly.utils.MyMapper"/>
        </plugin>

        <jdbcConnection driverClass="${spring.datasource.driver-class-name}"
                        connectionURL="${spring.datasource.url}"
                        userId="${spring.datasource.username}"
                        password="${spring.datasource.password}">
        </jdbcConnection>

        <javaModelGenerator targetPackage="com.javakly.model" targetProject="src/main/java"/>

        <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>

        <javaClientGenerator targetPackage="com.javakly.mapper" targetProject="src/main/java"
                             type="XMLMAPPER"/>

        <table tableName="sys_files">
            <!--mysql 配置-->
            <generatedKey column="file_id" sqlStatement="Mysql" identity="true"/>
            <!--oracle 配置-->
            <!--<generatedKey column="id" sqlStatement="select SEQ_{1}.nextval from dual" identity="false" type="pre"/>-->
        </table>
    </context>
</generatorConfiguration>

 

4. Run the mybatis-generator:generate command in myeclipse (myeclipse comes with the mvn prefix)

 

 

 

2.2 springboot integrates FreeMarker configuration

server.port=8080
#context-path第一次请求的地址
server.context-path=/files
server.session.timeout=600000
server.tomcat.uri-encoding=UTF-8
server.tomcat.max-threads=800
server.tomcat.min-spare-threads=30

#文件大小
spring.servlet.multipart.max-file-size=100000000
spring.servlet.multipart.max-request-size=100000000

# 启用的application文件
spring.profiles.active=dev
# 表示访问该路径时代表请求静态资源,用户可以直接访问该请求路径中的静态资源
spring.mvc.static-path-pattern=/static/**
spring.mvc.async.request-timeout=60000
########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
#本机调试时,配置项template_update_delay=0,这样就关闭了模板缓存。注意线上环境要开启缓存
spring.freemarker.cache=false
spring.freemarker.settings.template_update_delay=0
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
spring.freemarker.prefix=
#若在freemarker获取request对象,在spring boot 在application.properties可以这么配置
spring.freemarker.request-context-attribute=request
#spring.freemarker.settings.*=
spring.freemarker.suffix=.ftl
#template-loader-path表示所有的模板文件都放在该目录下
spring.freemarker.template-loader-path=classpath\:/templates/ 
#spring.freemarker.view-names= #whitelistofviewnamesthatcanberesolved

#static-locations可以自定义静态资源路径,不过会覆盖springboot默认路径
#在这个最末尾的file:${web.upload-path}之所有要加file:是因为指定的是一个具体的硬盘路径,其他的使用classpath指的是系统环境变量
#spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${web.upload-path}
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
spring.freemarker.settings.datetime_format=yyyy-MM-dd
#freemarker空值不报异常
spring.freemarker.settings.classic_compatible=true

2​​​​​​.3PageHelper configuration and query with general mapper

# mybatis 配置
mybatis.type-aliases-package=com.javakly.model
mybatis.mapper-locations=classpath:mapper/*.xml
# tk.mapper配置
mapper.mappers=com.javakly.utils.MyMapper
mapper.not-empty=false
mapper.identity=MYSQL
# pagehelper 分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count\=countSql

 pageHelper uses 2 methods

Method 1: mybatis general query mode: write query conditions by yourself

public List<SysFiles> getAll(SysFiles sysFiles) {
		if (sysFiles.getPage() != null && sysFiles.getRows() != null) {
			PageHelper.startPage(sysFiles.getPage(), sysFiles.getRows());
		}
		Example example = new Example(SysFiles.class);
		Example.Criteria criteria = example.createCriteria();
		if (!StringUtils.isEmpty(sysFiles.getFileName())) {
			criteria.andLike("file_name", "%" + sysFiles.getFileName() + "%");
		}
		if (!StringUtils.isEmpty(sysFiles.getFileType())) {
			criteria.andLike("file_type", "%" + sysFiles.getFileType() + "%");
		}
		return sysFilesMapper.selectByExample(example);
	}

Method 2: Wrap the query through Weekend, and write the query condition through the annotation mapping field (that is, the class name::get method expression)

public List<SysFiles> getAllByWeekend(SysFiles sysFiles) {
		if (sysFiles.getPage() != null && sysFiles.getRows() != null) {
			PageHelper.startPage(sysFiles.getPage(), sysFiles.getRows());
		}
		Weekend<SysFiles> weekend = Weekend.of(SysFiles.class);
		WeekendCriteria<SysFiles, Object> criteria = weekend.weekendCriteria();
		if (!StringUtils.isEmpty(sysFiles.getFileName())) {
			criteria.andLike(SysFiles::getFileName, "%" + sysFiles.getFileName() + "%");
		}
		if (!StringUtils.isEmpty(sysFiles.getFileType())) {
			criteria.andLike(SysFiles::getFileType, "%" + sysFiles.getFileType() + "%");
		}
		return sysFilesMapper.selectByExample(weekend);
	}

 3. The pom dependency of this article | MyBatis and general Mapper and PageHelper jar versions (2020-4-19 are close to the latest version dependencies)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.6.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javakly</groupId>
	<artifactId>officeOnlineYZ</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>officeOnlineYZ</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<mybatis.spring.version>2.1.1</mybatis.spring.version>
		<mapper.starter.version>2.0.4</mapper.starter.version>
		<pagehelper.starter.version>1.2.12</pagehelper.starter.version>
		<druid.version>1.1.10</druid.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<!--freemarker -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>
		<!--jackson -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.datatype</groupId>
			<artifactId>jackson-datatype-joda</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.module</groupId>
			<artifactId>jackson-module-parameter-names</artifactId>
		</dependency>

		<!--mybatis -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis.spring.version}</version>
		</dependency>
		<!--mapper -->
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper-spring-boot-starter</artifactId>
			<version>${mapper.starter.version}</version>
		</dependency>
		<!--pagehelper -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>${pagehelper.starter.version}</version>
			<exclusions>
				<exclusion>
					<groupId>org.mybatis.spring.boot</groupId>
					<artifactId>mybatis-spring-boot-starter</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>${druid.version}</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<dependencies>
					<dependency>
						<groupId>org.springframework</groupId>
						<artifactId>springloaded</artifactId>
						<version>1.2.5.RELEASE</version>
					</dependency>
				</dependencies>
			</plugin>
			<plugin>
				<groupId>org.mybatis.generator</groupId>
				<artifactId>mybatis-generator-maven-plugin</artifactId>
				<version>1.3.2</version>
				<configuration>
					<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
					<overwrite>true</overwrite>
					<verbose>true</verbose>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>mysql</groupId>
						<artifactId>mysql-connector-java</artifactId>
						<version>${mysql.version}</version>
					</dependency>
					<dependency>
						<groupId>tk.mybatis</groupId>
						<artifactId>mapper-generator</artifactId>
						<version>1.0.0</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>

</project>

 

Guess you like

Origin blog.csdn.net/qq_26408545/article/details/105611747