实现Bencode借用github上开源项目 做了一些改造
https://github.com/dampcake/bencode

新增方法encode(object o)
实际上是利用反射将复杂对象转为map
public static Map<String, Object> beanToMapComplate(Object bean) {
if (bean == null) {
return null;
}
Map<String, Object> map = new TreeMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (!key.equals("class")) {
Method getter = property.getReadMeth

本文介绍了如何从零开始编写DHT/磁力爬虫,重点讲解了Bencode编码的实现过程。通过改造github上的开源项目,实现了encode方法,利用反射将复杂对象转化为Map,便于构建后续的消息处理。
最低0.47元/天 解锁文章
2930

被折叠的 条评论
为什么被折叠?



