Fixing .gitignore
This commit is contained in:
@@ -1,36 +1,36 @@
|
||||
package com;
|
||||
|
||||
import com.peanut.modules.pay.weChatPay.config.WechatPayConfig;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.security.PrivateKey;
|
||||
@SpringBootTest
|
||||
public class VxApiTest {
|
||||
|
||||
|
||||
@Resource
|
||||
private WechatPayConfig wechatPayConfig;
|
||||
|
||||
@Test
|
||||
public void textgetPrivateKey(){
|
||||
//获取私钥路径
|
||||
String keyPemPath = wechatPayConfig.getKeyPemPath();
|
||||
//获取私钥
|
||||
PrivateKey privateKey = wechatPayConfig.getPrivateKey(keyPemPath);
|
||||
//输出很长的字符串 如果有问题转json试试
|
||||
System.out.println(privateKey);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getWxPayConfig(){
|
||||
String mchId =wechatPayConfig.getMchId();
|
||||
System.out.println(mchId);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
package com;
|
||||
|
||||
import com.peanut.modules.pay.weChatPay.config.WechatPayConfig;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.security.PrivateKey;
|
||||
@SpringBootTest
|
||||
public class VxApiTest {
|
||||
|
||||
|
||||
@Resource
|
||||
private WechatPayConfig wechatPayConfig;
|
||||
|
||||
@Test
|
||||
public void textgetPrivateKey(){
|
||||
//获取私钥路径
|
||||
String keyPemPath = wechatPayConfig.getKeyPemPath();
|
||||
//获取私钥
|
||||
PrivateKey privateKey = wechatPayConfig.getPrivateKey(keyPemPath);
|
||||
//输出很长的字符串 如果有问题转json试试
|
||||
System.out.println(privateKey);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getWxPayConfig(){
|
||||
String mchId =wechatPayConfig.getMchId();
|
||||
System.out.println(mchId);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,99 +1,99 @@
|
||||
package com.peanut;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.peanut.common.utils.ConnUtil;
|
||||
import com.peanut.common.utils.TokenHolder;
|
||||
import com.peanut.modules.book.entity.BookChapterEntity;
|
||||
import com.peanut.modules.book.service.BookChapterService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BaiduTest {
|
||||
|
||||
@Autowired
|
||||
private BookChapterService bookChapterService;
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
(new BaiduTest()).run();
|
||||
}
|
||||
|
||||
|
||||
// 填写申请百度语音申请的appkey 申请地址:百度AI开放平台
|
||||
private final String appKey = "eaPPX40oIazH8R4oWsD9U4IT";
|
||||
|
||||
// 填写申请百度语音申请的APP SECRET
|
||||
private final String secretKey = "DTN0ioQywwM23IoT2ZzEBmvfBe63ATEY";
|
||||
|
||||
// text 的内容为"欢迎使用百度语音合成"的urlencode,utf-8 编码
|
||||
private final String text = "百度百科是百度公司推出的一部内容开放、自由的网络百科全书。";
|
||||
// 发音人选择, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声
|
||||
private final int per = 3;
|
||||
// 语速,取值0-9,默认为5中语速
|
||||
private final int spd = 5;
|
||||
// 音调,取值0-9,默认为5中语调
|
||||
private final int pit = 5;
|
||||
// 音量,取值0-9,默认为5中音量
|
||||
private final int vol = 5;
|
||||
// 调用地址
|
||||
public final String url = "http://tsn.baidu.com/text2audio";
|
||||
// 用户唯一标识,用来区分用户,填写机器 MAC 地址或 IMEI 码,长度为60以内
|
||||
private String cuid = "0322java";
|
||||
|
||||
private void run() throws Exception {
|
||||
TokenHolder holder = new TokenHolder(appKey, secretKey, TokenHolder.ASR_SCOPE);
|
||||
holder.resfresh();
|
||||
String token = holder.getToken();
|
||||
|
||||
// List<String> list = new ArrayList<String>();
|
||||
// FileInputStream fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\测试.txt");
|
||||
// InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
|
||||
// BufferedReader br = new BufferedReader(isr);
|
||||
// String line = "";
|
||||
// while ((line = br.readLine()) != null) {
|
||||
// // 如果 t x t文件里的路径 不包含---字符串 这里是对里面的内容进行一个筛选
|
||||
// if (line.lastIndexOf("---") < 0) {
|
||||
// list.add(line);
|
||||
// }
|
||||
// }
|
||||
// System.out.println("line"+list);
|
||||
// for (String a:list) {
|
||||
|
||||
//
|
||||
String content = "第1章 科学边界(1) 汪淼觉得,来找他的这四个人是一个奇怪的组合:两名警察和两名军人,如果那两个军人是武警还算正常,但这是两名陆军军官。";
|
||||
|
||||
String url2 = url + "?tex=" + ConnUtil.urlEncode(content);
|
||||
url2 += "&per=" + per;
|
||||
url2 += "&spd=" + spd;
|
||||
url2 += "&pit=" + pit;
|
||||
url2 += "&vol=" + vol;
|
||||
url2 += "&cuid=" + cuid;
|
||||
url2 += "&tok=" + token;
|
||||
url2 += "&lan=zh&ctp=1";
|
||||
HttpURLConnection conn = (HttpURLConnection) new URL(url2).openConnection();
|
||||
conn.setConnectTimeout(5000);
|
||||
String contentType = conn.getContentType();
|
||||
if (contentType.contains("mp3")) {
|
||||
byte[] bytes = ConnUtil.getResponseBytes(conn);
|
||||
// 存在项目根目录下,去文件夹点击可以播放
|
||||
File file = new File("result1.mp3");
|
||||
FileOutputStream os = new FileOutputStream(file);
|
||||
os.write(bytes);
|
||||
os.close();
|
||||
System.out.println("mp3 file write to " + file.getAbsolutePath());
|
||||
} else {
|
||||
System.err.println("ERROR: content-type= " + contentType);
|
||||
String res = ConnUtil.getResponseString(conn);
|
||||
System.err.println(res);
|
||||
}
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
package com.peanut;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.peanut.common.utils.ConnUtil;
|
||||
import com.peanut.common.utils.TokenHolder;
|
||||
import com.peanut.modules.book.entity.BookChapterEntity;
|
||||
import com.peanut.modules.book.service.BookChapterService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BaiduTest {
|
||||
|
||||
@Autowired
|
||||
private BookChapterService bookChapterService;
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
(new BaiduTest()).run();
|
||||
}
|
||||
|
||||
|
||||
// 填写申请百度语音申请的appkey 申请地址:百度AI开放平台
|
||||
private final String appKey = "eaPPX40oIazH8R4oWsD9U4IT";
|
||||
|
||||
// 填写申请百度语音申请的APP SECRET
|
||||
private final String secretKey = "DTN0ioQywwM23IoT2ZzEBmvfBe63ATEY";
|
||||
|
||||
// text 的内容为"欢迎使用百度语音合成"的urlencode,utf-8 编码
|
||||
private final String text = "百度百科是百度公司推出的一部内容开放、自由的网络百科全书。";
|
||||
// 发音人选择, 0为普通女声,1为普通男生,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女声
|
||||
private final int per = 3;
|
||||
// 语速,取值0-9,默认为5中语速
|
||||
private final int spd = 5;
|
||||
// 音调,取值0-9,默认为5中语调
|
||||
private final int pit = 5;
|
||||
// 音量,取值0-9,默认为5中音量
|
||||
private final int vol = 5;
|
||||
// 调用地址
|
||||
public final String url = "http://tsn.baidu.com/text2audio";
|
||||
// 用户唯一标识,用来区分用户,填写机器 MAC 地址或 IMEI 码,长度为60以内
|
||||
private String cuid = "0322java";
|
||||
|
||||
private void run() throws Exception {
|
||||
TokenHolder holder = new TokenHolder(appKey, secretKey, TokenHolder.ASR_SCOPE);
|
||||
holder.resfresh();
|
||||
String token = holder.getToken();
|
||||
|
||||
// List<String> list = new ArrayList<String>();
|
||||
// FileInputStream fis = new FileInputStream("C:\\Users\\Administrator\\Desktop\\测试.txt");
|
||||
// InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
|
||||
// BufferedReader br = new BufferedReader(isr);
|
||||
// String line = "";
|
||||
// while ((line = br.readLine()) != null) {
|
||||
// // 如果 t x t文件里的路径 不包含---字符串 这里是对里面的内容进行一个筛选
|
||||
// if (line.lastIndexOf("---") < 0) {
|
||||
// list.add(line);
|
||||
// }
|
||||
// }
|
||||
// System.out.println("line"+list);
|
||||
// for (String a:list) {
|
||||
|
||||
//
|
||||
String content = "第1章 科学边界(1) 汪淼觉得,来找他的这四个人是一个奇怪的组合:两名警察和两名军人,如果那两个军人是武警还算正常,但这是两名陆军军官。";
|
||||
|
||||
String url2 = url + "?tex=" + ConnUtil.urlEncode(content);
|
||||
url2 += "&per=" + per;
|
||||
url2 += "&spd=" + spd;
|
||||
url2 += "&pit=" + pit;
|
||||
url2 += "&vol=" + vol;
|
||||
url2 += "&cuid=" + cuid;
|
||||
url2 += "&tok=" + token;
|
||||
url2 += "&lan=zh&ctp=1";
|
||||
HttpURLConnection conn = (HttpURLConnection) new URL(url2).openConnection();
|
||||
conn.setConnectTimeout(5000);
|
||||
String contentType = conn.getContentType();
|
||||
if (contentType.contains("mp3")) {
|
||||
byte[] bytes = ConnUtil.getResponseBytes(conn);
|
||||
// 存在项目根目录下,去文件夹点击可以播放
|
||||
File file = new File("result1.mp3");
|
||||
FileOutputStream os = new FileOutputStream(file);
|
||||
os.write(bytes);
|
||||
os.close();
|
||||
System.out.println("mp3 file write to " + file.getAbsolutePath());
|
||||
} else {
|
||||
System.err.println("ERROR: content-type= " + contentType);
|
||||
String res = ConnUtil.getResponseString(conn);
|
||||
System.err.println(res);
|
||||
}
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,38 +1,38 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut;
|
||||
|
||||
import com.peanut.service.DynamicDataSourceTestService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* 多数据源测试
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class DynamicDataSourceTest {
|
||||
@Autowired
|
||||
private DynamicDataSourceTestService dynamicDataSourceTestService;
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
Long id = 1L;
|
||||
|
||||
dynamicDataSourceTestService.updateUser(id);
|
||||
dynamicDataSourceTestService.updateUserBySlave1(id);
|
||||
dynamicDataSourceTestService.updateUserBySlave2(id);
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut;
|
||||
|
||||
import com.peanut.service.DynamicDataSourceTestService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* 多数据源测试
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class DynamicDataSourceTest {
|
||||
@Autowired
|
||||
private DynamicDataSourceTestService dynamicDataSourceTestService;
|
||||
|
||||
@Test
|
||||
public void test(){
|
||||
Long id = 1L;
|
||||
|
||||
dynamicDataSourceTestService.updateUser(id);
|
||||
dynamicDataSourceTestService.updateUserBySlave1(id);
|
||||
dynamicDataSourceTestService.updateUserBySlave2(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
package com.peanut;
|
||||
|
||||
import com.peanut.modules.app.utils.JwtUtils;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class JwtTest {
|
||||
@Autowired
|
||||
private JwtUtils jwtUtils;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String token = jwtUtils.generateToken(1);
|
||||
|
||||
System.out.println(token);
|
||||
}
|
||||
|
||||
}
|
||||
package com.peanut;
|
||||
|
||||
import com.peanut.modules.app.utils.JwtUtils;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class JwtTest {
|
||||
@Autowired
|
||||
private JwtUtils jwtUtils;
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String token = jwtUtils.generateToken(1);
|
||||
|
||||
System.out.println(token);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
package com.peanut;
|
||||
|
||||
import org.apache.poi.hwpf.HWPFDocument;
|
||||
import org.apache.poi.hwpf.usermodel.CharacterRun;
|
||||
import org.apache.poi.hwpf.usermodel.Paragraph;
|
||||
import org.apache.poi.hwpf.usermodel.Range;
|
||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class POITest {
|
||||
@Test
|
||||
public void readDoc() {
|
||||
|
||||
}
|
||||
}
|
||||
package com.peanut;
|
||||
|
||||
import org.apache.poi.hwpf.HWPFDocument;
|
||||
import org.apache.poi.hwpf.usermodel.CharacterRun;
|
||||
import org.apache.poi.hwpf.usermodel.Paragraph;
|
||||
import org.apache.poi.hwpf.usermodel.Range;
|
||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class POITest {
|
||||
@Test
|
||||
public void readDoc() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,43 @@
|
||||
package com.peanut;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.peanut.config.DelayQueueConfig;
|
||||
import com.peanut.modules.mq.service.RabbitMqService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class Producer {
|
||||
|
||||
@Autowired private RabbitMqService rabbitMqService;
|
||||
|
||||
@Test public void sendTest() {
|
||||
// 手动指定消息过期时间
|
||||
long ttl = 10000;
|
||||
|
||||
|
||||
Map<String, Object> msgMap = Maps.newHashMapWithExpectedSize(3);
|
||||
msgMap.put("msg", "Hello RabbitMq");
|
||||
msgMap.put("time", LocalDateTime.now());
|
||||
msgMap.put("ttl", ttl);
|
||||
|
||||
// 注意这里发送的交换机是 延时交换机
|
||||
rabbitMqService.send(DelayQueueConfig.DELAY_EXCHANGE, DelayQueueConfig.DELAY_QUEUE_ROUTING_KEY, JSONObject.toJSONString(msgMap), ttl);
|
||||
|
||||
log.info("消息发送成功:{}", JSONObject.toJSONString(msgMap));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
package com.peanut;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.peanut.config.DelayQueueConfig;
|
||||
import com.peanut.modules.mq.service.RabbitMqService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class Producer {
|
||||
|
||||
@Autowired private RabbitMqService rabbitMqService;
|
||||
|
||||
@Test public void sendTest() {
|
||||
// 手动指定消息过期时间
|
||||
long ttl = 10000;
|
||||
|
||||
|
||||
Map<String, Object> msgMap = Maps.newHashMapWithExpectedSize(3);
|
||||
msgMap.put("msg", "Hello RabbitMq");
|
||||
msgMap.put("time", LocalDateTime.now());
|
||||
msgMap.put("ttl", ttl);
|
||||
|
||||
// 注意这里发送的交换机是 延时交换机
|
||||
rabbitMqService.send(DelayQueueConfig.DELAY_EXCHANGE, DelayQueueConfig.DELAY_QUEUE_ROUTING_KEY, JSONObject.toJSONString(msgMap), ttl);
|
||||
|
||||
log.info("消息发送成功:{}", JSONObject.toJSONString(msgMap));
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,27 +1,27 @@
|
||||
package com.peanut;
|
||||
|
||||
import com.peanut.common.utils.RedisUtils;
|
||||
import com.peanut.modules.sys.entity.SysUserEntity;
|
||||
import org.apache.commons.lang.builder.ToStringBuilder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class RedisTest {
|
||||
@Autowired
|
||||
private RedisUtils redisUtils;
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
SysUserEntity user = new SysUserEntity();
|
||||
user.setEmail("qqq@qq.com");
|
||||
redisUtils.set("user", user);
|
||||
|
||||
System.out.println(ToStringBuilder.reflectionToString(redisUtils.get("user", SysUserEntity.class)));
|
||||
}
|
||||
|
||||
}
|
||||
package com.peanut;
|
||||
|
||||
import com.peanut.common.utils.RedisUtils;
|
||||
import com.peanut.modules.sys.entity.SysUserEntity;
|
||||
import org.apache.commons.lang.builder.ToStringBuilder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class RedisTest {
|
||||
@Autowired
|
||||
private RedisUtils redisUtils;
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
SysUserEntity user = new SysUserEntity();
|
||||
user.setEmail("qqq@qq.com");
|
||||
redisUtils.set("user", user);
|
||||
|
||||
System.out.println(ToStringBuilder.reflectionToString(redisUtils.get("user", SysUserEntity.class)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,132 +1,132 @@
|
||||
package com.peanut;
|
||||
|
||||
import com.spire.doc.Document;
|
||||
import com.spire.doc.Section;
|
||||
import com.spire.doc.documents.Paragraph;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ZZTest {
|
||||
@Test
|
||||
public void readDoc() throws IOException {
|
||||
Document doc = new Document();
|
||||
// doc.loadFromStream(new FileInputStream(new File("C:\\Users\\13404\\Desktop\\文本\\调研报告.doc")), FileFormat.Auto);
|
||||
doc.loadFromFile("C:\\Users\\Administrator\\Documents\\WeChat Files\\yl328572838\\FileStorage\\File\\2022-10\\桃源探秘.docx");
|
||||
//遍历section,section为文件的小节,通过分节符来识别
|
||||
for (int i = 0; i < doc.getSections().getCount(); i++) {
|
||||
Section section = doc.getSections().get(i);
|
||||
// System.out.println("段落数:" + section.getParagraphs().getCount());
|
||||
//遍历Paragraph,每个小节下有若干段落
|
||||
for (int j = 0; j < section.getParagraphs().getCount() - 1; j++) {
|
||||
Paragraph paragraph = section.getParagraphs().get(j);
|
||||
// System.out.println("section" + i + "——paragraph" + j + ":" + paragraph.getText() + "--" + paragraph.getStyleName() + "\r");
|
||||
// System.out.println(paragraph.getStyleName());
|
||||
//获取标题
|
||||
if (paragraph.getStyleName().equals("Heading1"))//段落样式为“标题1”的内容
|
||||
{
|
||||
//如果 是标题 1 为一级 目录
|
||||
int cj = 0;
|
||||
String level1 = paragraph.getText();
|
||||
System.out.println(level1);
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
do {
|
||||
// builder.append(section.getParagraphs().get(j).getText() + "\n");
|
||||
j++;
|
||||
//存放进数据库
|
||||
if (section.getParagraphs().get(j).getStyleName().equals("Heading2")) {
|
||||
cj = 2;
|
||||
}
|
||||
if (section.getParagraphs().get(j).getStyleName().equals("Heading3")) {
|
||||
cj = 3;
|
||||
}
|
||||
//防止index超出异常发生
|
||||
if (j >= section.getParagraphs().getCount()) {
|
||||
break;
|
||||
}
|
||||
// System.out.println(section.getParagraphs().get(j).getStyleName());
|
||||
} while (!section.getParagraphs().get(j).getStyleName().equals("Heading1"));
|
||||
//获取段落标题内容
|
||||
String text = builder.toString();
|
||||
j--; //由于do..while的特性,这里需要-1
|
||||
System.out.println("标题1和内容: " + i + ":" + text + ":" + (j - 1) + "\r");
|
||||
}
|
||||
}
|
||||
}
|
||||
// Section sec = doc.getSections().get(0);
|
||||
// String text = doc.getText();
|
||||
// System.out.println(sec);
|
||||
// System.out.println(text);
|
||||
doc.close();
|
||||
// 加载Word测试文档
|
||||
}
|
||||
|
||||
// Document doc = new Document();
|
||||
// doc.loadFromFile("C:\\Users\\Administrator\\Documents\\WeChat Files\\yl328572838\\FileStorage\\File\\2022-10\\桃源探秘.docx");
|
||||
//
|
||||
// //保存标题内容到.txt文档
|
||||
// File file = new File("GetTitle.txt");
|
||||
// if (file.exists())
|
||||
// {
|
||||
// file.delete();
|
||||
// }
|
||||
// file.createNewFile();
|
||||
// FileWriter fw = new FileWriter(file, true);
|
||||
// BufferedWriter bw = new BufferedWriter(fw);
|
||||
//
|
||||
// //遍历section
|
||||
// for (int i = 0; i < doc.getSections().getCount(); i++)
|
||||
// {
|
||||
// Section section = doc.getSections().get(i);
|
||||
// //遍历Paragraph
|
||||
// for (int j = 0; j < section.getParagraphs().getCount(); j++)
|
||||
// {
|
||||
// Paragraph paragraph = section.getParagraphs().get(j);
|
||||
//
|
||||
// //获取标题
|
||||
// if ( paragraph.getStyleName().matches("1"))//段落为“标题1”的内容
|
||||
// {
|
||||
// //获取段落标题内容
|
||||
// String text = paragraph.getText();
|
||||
//
|
||||
// //写入文本到txt文档
|
||||
// bw.write("标题1: "+ text + " ");
|
||||
// }
|
||||
// //获取标题
|
||||
// if ( paragraph.getStyleName().matches("2"))//段落为“标题2”的内容
|
||||
// {
|
||||
// //获取段落标题内容
|
||||
// String text = paragraph.getText();
|
||||
//
|
||||
// //写入文本到txt文档
|
||||
// bw.write("标题2: " + text + " ");
|
||||
// }
|
||||
// //获取标题
|
||||
// if ( paragraph.getStyleName().matches("3"))//段落为“标题3”的内容
|
||||
// {
|
||||
// //获取段落标题内容
|
||||
// String text = paragraph.getText();
|
||||
//
|
||||
// //写入文本到txt文档
|
||||
// bw.write("标题3: " + text+" ");
|
||||
// }
|
||||
// //获取标题
|
||||
// if ( paragraph.getStyleName().matches("4"))//段落为“标题4”的内容
|
||||
// {
|
||||
// //获取段落标题内容
|
||||
// String text = paragraph.getText();
|
||||
//
|
||||
// //写入文本到txt文档
|
||||
// bw.write("标题4: " + text+" ");
|
||||
// }
|
||||
//
|
||||
// bw.write(" ");
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// bw.flush();
|
||||
// bw.close();
|
||||
// fw.close();
|
||||
// }
|
||||
}
|
||||
package com.peanut;
|
||||
|
||||
import com.spire.doc.Document;
|
||||
import com.spire.doc.Section;
|
||||
import com.spire.doc.documents.Paragraph;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ZZTest {
|
||||
@Test
|
||||
public void readDoc() throws IOException {
|
||||
Document doc = new Document();
|
||||
// doc.loadFromStream(new FileInputStream(new File("C:\\Users\\13404\\Desktop\\文本\\调研报告.doc")), FileFormat.Auto);
|
||||
doc.loadFromFile("C:\\Users\\Administrator\\Documents\\WeChat Files\\yl328572838\\FileStorage\\File\\2022-10\\桃源探秘.docx");
|
||||
//遍历section,section为文件的小节,通过分节符来识别
|
||||
for (int i = 0; i < doc.getSections().getCount(); i++) {
|
||||
Section section = doc.getSections().get(i);
|
||||
// System.out.println("段落数:" + section.getParagraphs().getCount());
|
||||
//遍历Paragraph,每个小节下有若干段落
|
||||
for (int j = 0; j < section.getParagraphs().getCount() - 1; j++) {
|
||||
Paragraph paragraph = section.getParagraphs().get(j);
|
||||
// System.out.println("section" + i + "——paragraph" + j + ":" + paragraph.getText() + "--" + paragraph.getStyleName() + "\r");
|
||||
// System.out.println(paragraph.getStyleName());
|
||||
//获取标题
|
||||
if (paragraph.getStyleName().equals("Heading1"))//段落样式为“标题1”的内容
|
||||
{
|
||||
//如果 是标题 1 为一级 目录
|
||||
int cj = 0;
|
||||
String level1 = paragraph.getText();
|
||||
System.out.println(level1);
|
||||
|
||||
StringBuilder builder = new StringBuilder();
|
||||
do {
|
||||
// builder.append(section.getParagraphs().get(j).getText() + "\n");
|
||||
j++;
|
||||
//存放进数据库
|
||||
if (section.getParagraphs().get(j).getStyleName().equals("Heading2")) {
|
||||
cj = 2;
|
||||
}
|
||||
if (section.getParagraphs().get(j).getStyleName().equals("Heading3")) {
|
||||
cj = 3;
|
||||
}
|
||||
//防止index超出异常发生
|
||||
if (j >= section.getParagraphs().getCount()) {
|
||||
break;
|
||||
}
|
||||
// System.out.println(section.getParagraphs().get(j).getStyleName());
|
||||
} while (!section.getParagraphs().get(j).getStyleName().equals("Heading1"));
|
||||
//获取段落标题内容
|
||||
String text = builder.toString();
|
||||
j--; //由于do..while的特性,这里需要-1
|
||||
System.out.println("标题1和内容: " + i + ":" + text + ":" + (j - 1) + "\r");
|
||||
}
|
||||
}
|
||||
}
|
||||
// Section sec = doc.getSections().get(0);
|
||||
// String text = doc.getText();
|
||||
// System.out.println(sec);
|
||||
// System.out.println(text);
|
||||
doc.close();
|
||||
// 加载Word测试文档
|
||||
}
|
||||
|
||||
// Document doc = new Document();
|
||||
// doc.loadFromFile("C:\\Users\\Administrator\\Documents\\WeChat Files\\yl328572838\\FileStorage\\File\\2022-10\\桃源探秘.docx");
|
||||
//
|
||||
// //保存标题内容到.txt文档
|
||||
// File file = new File("GetTitle.txt");
|
||||
// if (file.exists())
|
||||
// {
|
||||
// file.delete();
|
||||
// }
|
||||
// file.createNewFile();
|
||||
// FileWriter fw = new FileWriter(file, true);
|
||||
// BufferedWriter bw = new BufferedWriter(fw);
|
||||
//
|
||||
// //遍历section
|
||||
// for (int i = 0; i < doc.getSections().getCount(); i++)
|
||||
// {
|
||||
// Section section = doc.getSections().get(i);
|
||||
// //遍历Paragraph
|
||||
// for (int j = 0; j < section.getParagraphs().getCount(); j++)
|
||||
// {
|
||||
// Paragraph paragraph = section.getParagraphs().get(j);
|
||||
//
|
||||
// //获取标题
|
||||
// if ( paragraph.getStyleName().matches("1"))//段落为“标题1”的内容
|
||||
// {
|
||||
// //获取段落标题内容
|
||||
// String text = paragraph.getText();
|
||||
//
|
||||
// //写入文本到txt文档
|
||||
// bw.write("标题1: "+ text + " ");
|
||||
// }
|
||||
// //获取标题
|
||||
// if ( paragraph.getStyleName().matches("2"))//段落为“标题2”的内容
|
||||
// {
|
||||
// //获取段落标题内容
|
||||
// String text = paragraph.getText();
|
||||
//
|
||||
// //写入文本到txt文档
|
||||
// bw.write("标题2: " + text + " ");
|
||||
// }
|
||||
// //获取标题
|
||||
// if ( paragraph.getStyleName().matches("3"))//段落为“标题3”的内容
|
||||
// {
|
||||
// //获取段落标题内容
|
||||
// String text = paragraph.getText();
|
||||
//
|
||||
// //写入文本到txt文档
|
||||
// bw.write("标题3: " + text+" ");
|
||||
// }
|
||||
// //获取标题
|
||||
// if ( paragraph.getStyleName().matches("4"))//段落为“标题4”的内容
|
||||
// {
|
||||
// //获取段落标题内容
|
||||
// String text = paragraph.getText();
|
||||
//
|
||||
// //写入文本到txt文档
|
||||
// bw.write("标题4: " + text+" ");
|
||||
// }
|
||||
//
|
||||
// bw.write(" ");
|
||||
// }
|
||||
//
|
||||
// }
|
||||
// bw.flush();
|
||||
// bw.close();
|
||||
// fw.close();
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -1,57 +1,57 @@
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.service;
|
||||
|
||||
import com.peanut.datasource.annotation.DataSource;
|
||||
import com.peanut.modules.sys.dao.SysUserDao;
|
||||
import com.peanut.modules.sys.entity.SysUserEntity;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 测试多数据源
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
@Service
|
||||
//@DataSource("slave1")
|
||||
public class DynamicDataSourceTestService {
|
||||
@Autowired
|
||||
private SysUserDao sysUserDao;
|
||||
|
||||
@Transactional
|
||||
public void updateUser(Long id){
|
||||
SysUserEntity user = new SysUserEntity();
|
||||
user.setUserId(id);
|
||||
user.setMobile("13500000000");
|
||||
sysUserDao.updateById(user);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@DataSource("slave1")
|
||||
public void updateUserBySlave1(Long id){
|
||||
SysUserEntity user = new SysUserEntity();
|
||||
user.setUserId(id);
|
||||
user.setMobile("13500000001");
|
||||
sysUserDao.updateById(user);
|
||||
}
|
||||
|
||||
@DataSource("slave2")
|
||||
@Transactional
|
||||
public void updateUserBySlave2(Long id){
|
||||
SysUserEntity user = new SysUserEntity();
|
||||
user.setUserId(id);
|
||||
user.setMobile("13500000002");
|
||||
sysUserDao.updateById(user);
|
||||
|
||||
//测试事物
|
||||
int i = 1/0;
|
||||
}
|
||||
/**
|
||||
* Copyright (c) 2016-2019 人人开源 All rights reserved.
|
||||
*
|
||||
* https://www.renren.io
|
||||
*
|
||||
* 版权所有,侵权必究!
|
||||
*/
|
||||
|
||||
package com.peanut.service;
|
||||
|
||||
import com.peanut.datasource.annotation.DataSource;
|
||||
import com.peanut.modules.sys.dao.SysUserDao;
|
||||
import com.peanut.modules.sys.entity.SysUserEntity;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 测试多数据源
|
||||
*
|
||||
* @author Mark sunlightcs@gmail.com
|
||||
*/
|
||||
@Service
|
||||
//@DataSource("slave1")
|
||||
public class DynamicDataSourceTestService {
|
||||
@Autowired
|
||||
private SysUserDao sysUserDao;
|
||||
|
||||
@Transactional
|
||||
public void updateUser(Long id){
|
||||
SysUserEntity user = new SysUserEntity();
|
||||
user.setUserId(id);
|
||||
user.setMobile("13500000000");
|
||||
sysUserDao.updateById(user);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@DataSource("slave1")
|
||||
public void updateUserBySlave1(Long id){
|
||||
SysUserEntity user = new SysUserEntity();
|
||||
user.setUserId(id);
|
||||
user.setMobile("13500000001");
|
||||
sysUserDao.updateById(user);
|
||||
}
|
||||
|
||||
@DataSource("slave2")
|
||||
@Transactional
|
||||
public void updateUserBySlave2(Long id){
|
||||
SysUserEntity user = new SysUserEntity();
|
||||
user.setUserId(id);
|
||||
user.setMobile("13500000002");
|
||||
sysUserDao.updateById(user);
|
||||
|
||||
//测试事物
|
||||
int i = 1/0;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.peanut.service;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class GetP {
|
||||
@Test
|
||||
public void test1(){
|
||||
|
||||
}
|
||||
}
|
||||
package com.peanut.service;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class GetP {
|
||||
@Test
|
||||
public void test1(){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,224 +1,224 @@
|
||||
package com.peanut.service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
import com.aspose.words.Document;
|
||||
import com.aspose.words.SaveFormat;
|
||||
|
||||
/**
|
||||
* @author zcc 解析word获取对应章节下面的数据
|
||||
*/
|
||||
public class Test
|
||||
{
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// word文件的路径
|
||||
String filePath = "C:\\Users\\Administrator\\Documents\\WeChat Files\\yl328572838\\FileStorage\\File\\2022-10\\桃源探秘.docx";
|
||||
InputStream input = null;
|
||||
|
||||
try
|
||||
{
|
||||
// 先创建一个临时目录文件夹
|
||||
String tempPath = "D:\\chapter\\";
|
||||
File tempFile = new File(tempPath);
|
||||
if (!tempFile.exists())
|
||||
{
|
||||
tempFile.mkdirs();
|
||||
}
|
||||
|
||||
input = new FileInputStream(filePath);
|
||||
Document doc = new Document(input);
|
||||
// 将word文档全量转成html显示的html文件路径
|
||||
String htmlFilePath = tempPath + "test.html";
|
||||
doc.save(htmlFilePath, SaveFormat.HTML);
|
||||
String htmlStr = getHtmlStrFromFile(htmlFilePath);
|
||||
org.jsoup.nodes.Document htmlDoc = Jsoup.parse(htmlStr);
|
||||
changeImageSrc(htmlDoc, tempPath);// 转换图片格式
|
||||
Map<String, String> map = exactContentFromHtml(htmlDoc, 2);
|
||||
String string = map.get("h1_标题名称");
|
||||
System.out.println(string);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtils.closeQuietly(input);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从html中抽取出预规内容,章节对应章节的内容。
|
||||
* @return key=h1_name/h2_name,value=html
|
||||
*/
|
||||
private static Map<String, String> exactContentFromHtml(org.jsoup.nodes.Document htmlDoc, int level)
|
||||
{
|
||||
Map<String, String> value = new LinkedHashMap<String, String>();
|
||||
try
|
||||
{
|
||||
Elements eleList = htmlDoc.getElementsByTag("h1");
|
||||
if (eleList == null || eleList.size() == 0)
|
||||
{
|
||||
throw new Exception("上传的文件中不存在一级标题,请检查!");
|
||||
}
|
||||
Element ele = eleList.get(0);
|
||||
String tempKey = "h1_" + ele.text();
|
||||
StringBuffer tempBuffer = new StringBuffer();
|
||||
while (true)
|
||||
{
|
||||
ele = ele.nextElementSibling();// 获取当前节点的下一个节点
|
||||
if (ele == null)
|
||||
{
|
||||
if (StringUtils.isNotEmpty(tempKey))
|
||||
{
|
||||
value.put(tempKey, tempBuffer.toString());
|
||||
}
|
||||
break;
|
||||
}
|
||||
String eleTagName = ele.tagName();// 标签名称
|
||||
if ("h1".equals(eleTagName))
|
||||
{
|
||||
if (StringUtils.isNotEmpty(tempKey))
|
||||
{
|
||||
value.put(tempKey, tempBuffer.toString());
|
||||
tempBuffer.setLength(0);
|
||||
}
|
||||
tempKey = "h1_" + removeNullChar(ele.text());
|
||||
continue;
|
||||
}
|
||||
if (level == 2)
|
||||
{
|
||||
if ("h2".equals(eleTagName))
|
||||
{
|
||||
if (StringUtils.isNotEmpty(tempKey))
|
||||
{
|
||||
value.put(tempKey, tempBuffer.toString());
|
||||
tempBuffer.setLength(0);
|
||||
}
|
||||
tempKey = "h2_" + removeNullChar(ele.text());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
tempBuffer.append(ele.outerHtml());
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除空字符串和*字符
|
||||
* @param text
|
||||
* @return
|
||||
*/
|
||||
private static String removeNullChar(String text)
|
||||
{
|
||||
if (text == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return text.replaceAll(" ", "").trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改图片的src,改为base64格式
|
||||
* @param htmlDoc org.jsoup.nodes.Document
|
||||
* @param file 文件路径
|
||||
* @throws IOException
|
||||
*/
|
||||
private static void changeImageSrc(org.jsoup.nodes.Document htmlDoc, String file) throws IOException
|
||||
{
|
||||
Elements images = htmlDoc.getElementsByTag("img"); // 获取所有的image,转码。
|
||||
for (int i = 0; i < images.size(); i++)
|
||||
{
|
||||
Element tempImage = images.get(i);
|
||||
String tempSrc = tempImage.attr("src");
|
||||
tempImage.attr("src", imageConvertBase64(file + File.separator + tempSrc));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 把图片转换成base64格式
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
private static String imageConvertBase64(String filePath) throws IOException
|
||||
{
|
||||
InputStream in = null;
|
||||
byte[] data = null;
|
||||
try
|
||||
{
|
||||
String fileExtension = filePath.substring(filePath.lastIndexOf(".") + 1); // 获取文件后缀
|
||||
in = new FileInputStream(new File(filePath));
|
||||
data = new byte[in.available()];
|
||||
in.read(data);
|
||||
in.close();
|
||||
String enCoderContent = new String(Base64.getEncoder().encode(data));
|
||||
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
|
||||
Matcher m = p.matcher(enCoderContent);
|
||||
enCoderContent = m.replaceAll("");
|
||||
return "data:image/" + fileExtension + ";base64," + enCoderContent;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (in != null)
|
||||
{
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从html文件中读取文件信息string
|
||||
* @param filePath
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
private static String getHtmlStrFromFile(String filePath) throws IOException
|
||||
{
|
||||
FileInputStream in = null;
|
||||
File file = new File(filePath);
|
||||
Long filelength = file.length();
|
||||
byte[] filecontent = new byte[filelength.intValue()];
|
||||
try
|
||||
{
|
||||
in = new FileInputStream(file);
|
||||
in.read(filecontent);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtils.closeQuietly(in);
|
||||
}
|
||||
return new String(filecontent, "UTF-8");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
package com.peanut.service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.jsoup.nodes.Element;
|
||||
import org.jsoup.select.Elements;
|
||||
import com.aspose.words.Document;
|
||||
import com.aspose.words.SaveFormat;
|
||||
|
||||
/**
|
||||
* @author zcc 解析word获取对应章节下面的数据
|
||||
*/
|
||||
public class Test
|
||||
{
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// word文件的路径
|
||||
String filePath = "C:\\Users\\Administrator\\Documents\\WeChat Files\\yl328572838\\FileStorage\\File\\2022-10\\桃源探秘.docx";
|
||||
InputStream input = null;
|
||||
|
||||
try
|
||||
{
|
||||
// 先创建一个临时目录文件夹
|
||||
String tempPath = "D:\\chapter\\";
|
||||
File tempFile = new File(tempPath);
|
||||
if (!tempFile.exists())
|
||||
{
|
||||
tempFile.mkdirs();
|
||||
}
|
||||
|
||||
input = new FileInputStream(filePath);
|
||||
Document doc = new Document(input);
|
||||
// 将word文档全量转成html显示的html文件路径
|
||||
String htmlFilePath = tempPath + "test.html";
|
||||
doc.save(htmlFilePath, SaveFormat.HTML);
|
||||
String htmlStr = getHtmlStrFromFile(htmlFilePath);
|
||||
org.jsoup.nodes.Document htmlDoc = Jsoup.parse(htmlStr);
|
||||
changeImageSrc(htmlDoc, tempPath);// 转换图片格式
|
||||
Map<String, String> map = exactContentFromHtml(htmlDoc, 2);
|
||||
String string = map.get("h1_标题名称");
|
||||
System.out.println(string);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtils.closeQuietly(input);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从html中抽取出预规内容,章节对应章节的内容。
|
||||
* @return key=h1_name/h2_name,value=html
|
||||
*/
|
||||
private static Map<String, String> exactContentFromHtml(org.jsoup.nodes.Document htmlDoc, int level)
|
||||
{
|
||||
Map<String, String> value = new LinkedHashMap<String, String>();
|
||||
try
|
||||
{
|
||||
Elements eleList = htmlDoc.getElementsByTag("h1");
|
||||
if (eleList == null || eleList.size() == 0)
|
||||
{
|
||||
throw new Exception("上传的文件中不存在一级标题,请检查!");
|
||||
}
|
||||
Element ele = eleList.get(0);
|
||||
String tempKey = "h1_" + ele.text();
|
||||
StringBuffer tempBuffer = new StringBuffer();
|
||||
while (true)
|
||||
{
|
||||
ele = ele.nextElementSibling();// 获取当前节点的下一个节点
|
||||
if (ele == null)
|
||||
{
|
||||
if (StringUtils.isNotEmpty(tempKey))
|
||||
{
|
||||
value.put(tempKey, tempBuffer.toString());
|
||||
}
|
||||
break;
|
||||
}
|
||||
String eleTagName = ele.tagName();// 标签名称
|
||||
if ("h1".equals(eleTagName))
|
||||
{
|
||||
if (StringUtils.isNotEmpty(tempKey))
|
||||
{
|
||||
value.put(tempKey, tempBuffer.toString());
|
||||
tempBuffer.setLength(0);
|
||||
}
|
||||
tempKey = "h1_" + removeNullChar(ele.text());
|
||||
continue;
|
||||
}
|
||||
if (level == 2)
|
||||
{
|
||||
if ("h2".equals(eleTagName))
|
||||
{
|
||||
if (StringUtils.isNotEmpty(tempKey))
|
||||
{
|
||||
value.put(tempKey, tempBuffer.toString());
|
||||
tempBuffer.setLength(0);
|
||||
}
|
||||
tempKey = "h2_" + removeNullChar(ele.text());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
tempBuffer.append(ele.outerHtml());
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除空字符串和*字符
|
||||
* @param text
|
||||
* @return
|
||||
*/
|
||||
private static String removeNullChar(String text)
|
||||
{
|
||||
if (text == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return text.replaceAll(" ", "").trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改图片的src,改为base64格式
|
||||
* @param htmlDoc org.jsoup.nodes.Document
|
||||
* @param file 文件路径
|
||||
* @throws IOException
|
||||
*/
|
||||
private static void changeImageSrc(org.jsoup.nodes.Document htmlDoc, String file) throws IOException
|
||||
{
|
||||
Elements images = htmlDoc.getElementsByTag("img"); // 获取所有的image,转码。
|
||||
for (int i = 0; i < images.size(); i++)
|
||||
{
|
||||
Element tempImage = images.get(i);
|
||||
String tempSrc = tempImage.attr("src");
|
||||
tempImage.attr("src", imageConvertBase64(file + File.separator + tempSrc));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 把图片转换成base64格式
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
private static String imageConvertBase64(String filePath) throws IOException
|
||||
{
|
||||
InputStream in = null;
|
||||
byte[] data = null;
|
||||
try
|
||||
{
|
||||
String fileExtension = filePath.substring(filePath.lastIndexOf(".") + 1); // 获取文件后缀
|
||||
in = new FileInputStream(new File(filePath));
|
||||
data = new byte[in.available()];
|
||||
in.read(data);
|
||||
in.close();
|
||||
String enCoderContent = new String(Base64.getEncoder().encode(data));
|
||||
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
|
||||
Matcher m = p.matcher(enCoderContent);
|
||||
enCoderContent = m.replaceAll("");
|
||||
return "data:image/" + fileExtension + ";base64," + enCoderContent;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (in != null)
|
||||
{
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从html文件中读取文件信息string
|
||||
* @param filePath
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
private static String getHtmlStrFromFile(String filePath) throws IOException
|
||||
{
|
||||
FileInputStream in = null;
|
||||
File file = new File(filePath);
|
||||
Long filelength = file.length();
|
||||
byte[] filecontent = new byte[filelength.intValue()];
|
||||
try
|
||||
{
|
||||
in = new FileInputStream(file);
|
||||
in.read(filecontent);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtils.closeQuietly(in);
|
||||
}
|
||||
return new String(filecontent, "UTF-8");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package io;
|
||||
|
||||
public class Test {
|
||||
|
||||
@org.junit.Test
|
||||
public void test01(){
|
||||
String a = "dsadasdasdas";
|
||||
String substring = a.substring(5, 1);
|
||||
System.out.println(substring);
|
||||
}
|
||||
|
||||
}
|
||||
package io;
|
||||
|
||||
public class Test {
|
||||
|
||||
@org.junit.Test
|
||||
public void test01(){
|
||||
String a = "dsadasdasdas";
|
||||
String substring = a.substring(5, 1);
|
||||
System.out.println(substring);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user