- 申请ID:cx22722
- 个人邮箱:2272258775@qq.com
-
原创技术文章:百度网盘群组分享源码
开发工具
IntelliJ IDEA
Mysql5.7
JDK1.8
搭建项目
使用的是SpringBoot2.2
前端页面使用的是thymeleaf
pom.xml
'''
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- thymeleaf 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
'''
application.properties
配置信息
‘’‘
server.port=8843
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://数据库链接地址?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = 数据库用户名
spring.datasource.password = 数据库用密码
thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
热部署文件,页面不产生缓存,及时更新
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
’‘’
使用Mybatis操作mysql
‘’‘
<mapper namespace="com.group.baidu.mapper.LinksMapper">
<insert id="addLinks">
insert into links
(linkname,links,senddate)
values
(#{linkname},#{links},#{sendDate});
</insert>
<select id="getAllLinks" resultType="com.group.baidu.entity.Links">
select * from links order by senddate desc limit 0,20;
</select>
<select id="linksCount" resultType="Integer">
select count(*) from links where links=#{links}
</select>
</mapper>
’‘’
代码块
发布百度云群组链接代码
‘’‘
Map<String,String> maps=new HashMap<>();
String code1 =(String)session.getAttribute("codes");
System.out.println(code1);
// https://pan.baidu.com/mbox/homepage?short=lemgzKb#share/type=session
if(linkname!=null&&!linkname.equals("")&&links!=null&&!links.equals("")&&!randomcode.equals("")&&randomcode!=null){
if(links.indexOf("https://pan.baidu.com/mbox/homepage?short=")==-1||links.length()>70){
maps.put("msg","该链接无法分享,请检查链接");
}else {
if (code1==null||code1.equals("")||!code1.equals(randomcode)){
maps.put("msg","验证码错误");
}else {
Integer row=linksService.linksCount(links);
if(row<3){
Date date = new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
//敏感字去除
String str= MinGan.pingbiStr(linkname);
Integer addRow=linksService.addLinks(str,links,dateFormat.format(date));
if(addRow>0){
maps.put("msg","分享成功");
System.out.println("OK");
}else {
maps.put("msg","分享失败");
System.out.println("nonono");
}
}else {
maps.put("msg","该链接次数过多,请发送新的群组链接");
System.out.println("该链接发送过了");
}
}
}
}else {
maps.put("msg","请填写完整");
System.out.println("参数是空的");
}
’‘’
验证码实现
防止有人批量发送链接
randString 是验证码随机生成的字符串 可以使用数字加字母
stringNum 验证码的位数 不要太长影响用户体验
‘’‘
public class RandomCode {
public static final String RANDOMCODEKEY = "randomcode_key";// 放到session中的key
private Random random = new Random();
private String randString = "0123456789";// 随机产生的字符串
private int width = 80;// 图片宽
private int height = 26;// 图片高
private int lineSize = 40;// 干扰线数量
private int stringNum = 4;// 随机产生字符数量
/**
* 生成随机图片
*/
public void getRandcode(HttpServletRequest request,
HttpServletResponse response) {
HttpSession session = request.getSession();
// BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_BGR);
// 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
Graphics g = image.getGraphics();
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));
g.setColor(getRandColor(160, 200));
// 绘制干扰线
for (int i = 0; i <= lineSize; i++) {
drowLine(g);
}
// 绘制随机字符
String randomString = "";
for (int i = 1; i <= stringNum; i++) {
randomString = drowString(g, randomString, i);
}
session.removeAttribute(RANDOMCODEKEY);
session.setAttribute(RANDOMCODEKEY, randomString);
session.setAttribute("codes", randomString);
g.dispose();
try {
// 将内存中的图片通过流动形式输出到客户端
ImageIO.write(image, "JPEG", response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 获得字体
*/
private Font getFont() {
return new Font("Fixedsys", Font.CENTER_BASELINE, 18);
}
/*
* 获得颜色
*/
private Color getRandColor(int fc, int bc) {
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc - 16);
int g = fc + random.nextInt(bc - fc - 14);
int b = fc + random.nextInt(bc - fc - 18);
return new Color(r, g, b);
}
/*
* 绘制字符串
*/
private String drowString(Graphics g, String randomString, int i) {
g.setFont(getFont());
g.setColor(new Color(random.nextInt(101), random.nextInt(111), random
.nextInt(121)));
String rand = String.valueOf(getRandomString(random.nextInt(randString
.length())));
randomString += rand;
g.translate(random.nextInt(3), random.nextInt(3));
g.drawString(rand, 13 * i, 16);
return randomString;
}
/*
* 绘制干扰线
*/
private void drowLine(Graphics g) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(13);
int yl = random.nextInt(15);
g.drawLine(x, y, x + xl, y + yl);
}
/*
* 获取随机的字符
*/
public String getRandomString(int num) {
String ss=String.valueOf(randString.charAt(random.nextInt(randString.length())));
return ss;
}
}
’‘’
前端页面thymeleaf
页面是使用Bootstrap前端框架写的 比较简洁
一些简单的页面元素 比较简陋
‘’‘
<center>
<div class="jumbotron">
<h1>Simple 群组分享</h1>
<p>欢迎 来到Simple百度云群组分享</p>
<p><a class="btn btn-primary btn-lg" href="#" role="button">Simple</a></p>
<h6>请不要分享违反法律的信息</h6>
<form class="form-inline" id="linkform">
<div class="form-group">
<label for="exampleInputName2">名称</label>
<input type="text" name="linkname" class="form-control" id="exampleInputName2" placeholder="群组名称">
</div>
<div class="form-group">
<label for="exampleInputEmail2">链接</label>
<input type="text" class="form-control" name="links" id="exampleInputEmail2" placeholder="https://pan.baidu.com/mbox/homepage?short">
</div>
<button type="button" class="btn btn-success" id="tijiao">提交</button>
<div class="wrap-input validate-input" data-validate="请输入验证码">
<img src="checkCode" alt="" width="150" height="50" class="passcode" style="height:43px;cursor:pointer;" onclick="this.src=this.src+'?'">
<input class="form-control" type="text" placeholder="请输入验证码" name="randomcode">
</div>
</form>
</div>
</center>
<table class="table table-condensed">
<tr>
<td><center><h3>群组名称</h3></center></td>
<td><center><h3>链接地址</h3></center></td>
<td><center><h3>发布时间</h3></center></td>
</tr>
<tr th:each="linkslist:${linklist}">
<td class="success col-md-3"><center th:text="${linkslist.linkname}">...</center></td>
<td class="info col-md-2"><a th:href="${linkslist.links}" target="_Blank">点我加入群组</a> </td>
<td class="active col-md-1" th:text="${linkslist.senddate}">...</td>
</tr>
</table>
’‘’