java实现短地址服务的方法(附代码)
作者:王爵 时间:2023-11-15 19:13:41
假设下面是你的视频网站链接列表,如果别人想爬取你的数据十分轻松,看规则就知道数据库是序列自增的
http://www.xxxx.com/video/1
http://www.xxxx.com/video/2
http://www.xxxx.com/video/3
那么解决这一问题,我们可以使用短地址,不对外暴露真实链接,使用对称加密是一个很好的方案。
Hashids是一个很好的选择,它提供了JS/PHP/JAVA/PYTHON等编程语言的实现,这里我使用的就是它。
下面是我基于blade框架搭建的java短地址服务。
CREATE TABLE `t_url` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`url` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
路由
@Path
public class IndexRoute {
// 盐值
private static final Hashids HASHIDS = new Hashids("blade-shorturl");
private UrlModel urlModel = new UrlModel();
@Route("/:key")
public void get(Request req, Response response) {
String key = req.pathParam(":key").replaceAll("[^A-Za-z0-9]", "");
long[] numbers = HASHIDS.decode(key);
if (null == numbers || numbers.length < 1) {
response.text("没有找到");
return;
}
int id = (int) numbers[0];
String result = get(id).getUrl();
if (result == null) {
response.text("没有找到");
return;
}
response.redirect(result);
}
@Route(value = "/", method = HttpMethod.GET)
public String index() {
return "index";
}
@Route(value = "/", method = HttpMethod.POST)
public String save(Request request, Response response) {
String resJsp = "index";
String longUrl = request.query("url");
if (!isURL(longUrl)) {
request.attribute("error", "无效的URL");
return resJsp;
}
Integer id = this.save(longUrl);
if (id == 0) {
request.attribute("error", "保存失败");
return resJsp;
}
String hash = HASHIDS.encode(id);
request.attribute("url_hash", hash);
System.out.println("id = " + id + ",url_hash=" + hash);
return resJsp;
}
private Integer save(String url) {
return urlModel.insert().param("url", url).executeAndCommit();
}
private UrlModel get(int id) {
return urlModel.fetchByPk(id);
}
private final String REGEX = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
private boolean isURL(String url) {
if(StringKit.isNotBlank(url)){
Pattern pattern = Pattern.compile(REGEX);
Matcher matcher = pattern.matcher(url);
if (matcher.find()) {
return true;
}
}
return false;
}
}
实现效果:
代码位置:https://github.com/bladejava/blade-shorturl
标签:java,短地址
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#实现XSL转换的方法
2023-02-02 03:36:41
SpringBoot MongoDB 索引冲突分析及解决方法
2023-01-23 06:28:05
![](https://img.aspxhome.com/file/2023/4/61504_0s.png)
使用SpringBoot 配置Oracle和H2双数据源及问题
2023-10-04 14:06:11
![](https://img.aspxhome.com/file/2023/7/64407_0s.png)
SpringBoot常用注解详细整理
2022-12-15 18:53:01
![](https://img.aspxhome.com/file/2023/3/75763_0s.jpg)
基于springboot实现redis分布式锁的方法
2023-06-16 01:36:56
![](https://img.aspxhome.com/file/2023/5/82365_0s.png)
分享Java多线程实现的四种方式
2022-02-23 06:34:21
SpringBoot自动配置源码深入刨析讲解
2022-04-03 14:38:06
![](https://img.aspxhome.com/file/2023/1/86031_0s.png)
Android通讯录开发之删除功能的实现方法
2021-07-06 10:43:53
![](https://img.aspxhome.com/file/2023/1/116031_0s.png)
C#数组学习相关资料整理
2022-12-03 08:02:54
Java实现简单的五子棋游戏示例代码
2022-03-14 22:35:47
![](https://img.aspxhome.com/file/2023/9/72569_0s.png)
浅谈图片上传利用request.getInputStream()获取文件流时遇到的问题
2023-10-18 10:36:43
![](https://img.aspxhome.com/file/2023/9/64029_0s.jpg)
Android中使用GridView进行应用程序UI布局的教程
2022-03-19 15:18:06
![](https://img.aspxhome.com/file/2023/0/138030_0s.gif)
Android实现简易计算器(可以实现连续计算)
2023-11-30 23:29:47
![](https://img.aspxhome.com/file/2023/0/110680_0s.jpg)
关于mybatis一对一查询一对多查询遇到的问题
2022-01-26 05:25:12
![](https://img.aspxhome.com/file/2023/4/75414_0s.jpg)
解析Spring事件发布与监听机制
2022-09-01 09:52:19
![](https://img.aspxhome.com/file/2023/6/88586_0s.jpg)
.NET(C#):Emit创建异常处理的方法
2023-11-05 04:03:02
Android应用开发中CardView的初步使用指南
2022-12-30 02:04:47
![](https://img.aspxhome.com/file/2023/1/138691_0s.jpg)
java调用oracle分页存储过程示例
2021-06-08 12:44:16
Android权限管理之Permission权限机制及使用详解
2023-04-07 09:12:45
java实现ArrayList根据存储对象排序功能示例
2022-01-24 01:06:05
![](https://img.aspxhome.com/file/2023/4/84324_0s.png)