zoukankan      html  css  js  c++  java
  • 读取jar文件的sha1码,请求maven官方的solrsearch接口查询该jar文件所对应的maven坐标信息

    版权声明:本文为博主原创文章,未经博主允许不得转载。

    import com.google.gson.JsonObject;
    import com.google.gson.JsonParser;
    import org.apache.commons.io.IOUtils;
    
    import java.io.*;
    import java.net.URL;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * 读取jar文件的sha1码,请求maven官方的solrsearch接口查询该jar文件所对应的maven坐标信息
     * Created by Nihaorz on 2017/4/20.
     */
    public class MakeMavenInfo {
    
        public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
            long start = System.currentTimeMillis();
            File dir = new File("C:\Users\Administrator\Desktop\jars");
            for (File jar : dir.listFiles()) {
                String sha1 = getSha1ByFile(jar);
                Map<String, String> map = getMavenInfoBySha1(jar, sha1);
                System.out.println(map);
            }
            long end = System.currentTimeMillis();
            System.out.println("耗时:" + (end-start)/1000 + "秒");
        }
    
        /**
         * 根据jar包输入流获取该文件的sha1码
         * @param file
         * @return
         * @throws IOException
         * @throws NoSuchAlgorithmException
         */
        public static String getSha1ByFile(File file) throws IOException, NoSuchAlgorithmException {
            MessageDigest sha1 = MessageDigest.getInstance("SHA1");
            FileInputStream fis = new FileInputStream(file);
            byte[] data = new byte[1024];
            int read;
            while ((read = fis.read(data)) != -1) {
                sha1.update(data, 0, read);
            }
            byte[] hashBytes = sha1.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < hashBytes.length; i++) {
                sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16).substring(1));
            }
            return sb.toString();
        }
    
        /**
         * 根据sha1码请求接口拿到该jar包对应的maven坐标信息
         * @param jar
         * @param sha1
         * @return
         */
        public static Map<String, String> getMavenInfoBySha1(File jar, String sha1){
            Map<String, String> map = new HashMap<String, String>();
            String url = "http://search.maven.org/solrsearch/select?q=1:"";
            StringBuilder sb = new StringBuilder();
            sb.append(url).append(sha1).append(""&rows=20&wt=json");
            map.put("jarName", jar.getName());
            try {
                String jsonStr = IOUtils.toString(new URL(sb.toString()));
                JsonObject json = new JsonParser().parse(jsonStr).getAsJsonObject();
                JsonObject obj = json.getAsJsonObject("response").getAsJsonArray("docs").get(0).getAsJsonObject();
                map.put("groupId", obj.get("g").getAsString());
                map.put("artifactId", obj.get("a").getAsString());
                map.put("version", obj.get("v").getAsString());
                map.put("packaging", obj.get("p").getAsString());
                map.put("result", "true");
            } catch (Exception e) {
                e.printStackTrace();
                map.put("result", "false");
                System.out.println(sb);
            }
            return map;
        }
    
    }
    

    参考:  http://www.sha1-online.com/sha1-java/

         http://search.maven.org/#api

  • 相关阅读:
    TreeView 控件选中项变色
    遍历repeater
    堆和栈,类型声明实例化过程,string=null;string=”“的区别;
    用Oled操作EXCEL碰到错误
    修改部分代码,让优酷和ckplayer可覆盖,优酷播放的部分API。
    C# 拼接字符串,自实现翻页功能
    总结Vue第二天:自定义子组件、父子组件通信、插槽
    总结Vue第一天:简单介绍、基本常用知识、辅助函数
    总结Vue第三天:模块化和webpack模块化打包:
    PHP之冒泡排序
  • 原文地址:https://www.cnblogs.com/nihaorz/p/6738414.html
Copyright © 2011-2022 走看看