java程式: 從kernel.ubuntu.com下載kernel - HttpURLConnection
阿新 • • 發佈:2019-01-03
用java實現從kernel.ubuntu.com下載核心的小工具
現在的最新的LTS版本是4.19,目前已經更新到4.19.13了。
Kernel的更新通知裡,經常有下面的陳述:
作為一個聽勸的人,沒事就刷一刷https://www.kernel.org/,看看有沒有更新。
有更新之後,再刷一刷kernel.ubuntu.com看看Ubuntu有沒有更新核心。
下載deb包,執行下面安裝核心之後在重啟。
sudo dpkg -i *.deb
思路
希望通過下面命令列,可以從kernel.ubuntu.com下載核心的deb檔案。
java -jar getKernel.jar v4.19.13
java程式getkernel.jar先通過命令列引數獲得版本。
將版本拼接成網址https://kernel.ubuntu.com/~kernel-ppa/mainline/v4.19.13/
從網址讀取網頁,HttpURLConnection
並從中解析deb的超連結,java.util.regex.Pattern、Matcher
在把amd64的所有包都儲存成檔案。
估計用指令碼呼叫wget可能更快。
貼程式碼
App.java
1 package getKernel; 2 3 import java.io.BufferedReader;View Code4 import java.io.FileNotFoundException; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.InputStreamReader; 9 import java.net.HttpURLConnection; 10 import java.net.URL; 11 import java.util.regex.Matcher; 12 import java.util.regex.Pattern;13 14 public class App { 15 16 public static void main(String[] args) { 17 String targetIndex = "https://kernel.ubuntu.com/~kernel-ppa/mainline/" + args[0] + "/"; 18 19 String strHeaderAll=null; 20 String strHeaderAmd64=null; 21 String strImageUnsignedAmd64=null; 22 String strModulesAmd64=null; 23 24 boolean foundHeaderAll = false; 25 boolean foundHeaderAmd64 = false; 26 boolean foundImageAmd64 = false; 27 boolean foundModulesAmd64 = false; 28 29 // 1 get file 30 FileOutputStream fs = null; 31 BufferedReader br = null; 32 Pattern pattern = null; 33 Matcher matcher = null; 34 35 try { 36 URL url = new URL(targetIndex); 37 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 38 conn.connect(); 39 if (conn.getResponseCode() == 200) { 40 InputStream inStream = conn.getInputStream(); 41 br = new BufferedReader(new InputStreamReader(inStream)); 42 String str; 43 while ((str = br.readLine()) != null) { 44 45 if (!foundHeaderAll) { 46 pattern = Pattern.compile("href=\"(linux-headers-\\S+all.deb)\"", Pattern.CASE_INSENSITIVE); 47 matcher = pattern.matcher(str); 48 if (matcher.find()) { 49 strHeaderAll = targetIndex + matcher.group(1); 50 System.out.println(strHeaderAll); 51 foundHeaderAll = true; 52 } 53 } 54 if (!foundHeaderAmd64) { 55 pattern = Pattern.compile("href=\"(linux-headers-\\S+amd64.deb)\"", Pattern.CASE_INSENSITIVE); 56 matcher = pattern.matcher(str); 57 if (matcher.find()) { 58 strHeaderAmd64 = targetIndex + matcher.group(1); 59 System.out.println(strHeaderAmd64); 60 foundHeaderAmd64 = true; 61 } 62 } 63 if (!foundImageAmd64) { 64 pattern = Pattern.compile("href=\"(linux-image-unsigned\\S+amd64.deb)\"", 65 Pattern.CASE_INSENSITIVE); 66 matcher = pattern.matcher(str); 67 if (matcher.find()) { 68 strImageUnsignedAmd64 = targetIndex + matcher.group(1); 69 System.out.println(strImageUnsignedAmd64); 70 foundImageAmd64 = true; 71 } 72 73 } 74 if (!foundModulesAmd64) { 75 pattern = Pattern.compile("href=\"(linux-modules\\S+amd64.deb)\"", Pattern.CASE_INSENSITIVE); 76 matcher = pattern.matcher(str); 77 if (matcher.find()) { 78 strModulesAmd64 = targetIndex + matcher.group(1); 79 System.out.println(strModulesAmd64); 80 foundModulesAmd64 = true; 81 } 82 } 83 } 84 85 SaveFile.saveUrl2file(strHeaderAll); 86 SaveFile.saveUrl2file(strHeaderAmd64); 87 SaveFile.saveUrl2file(strImageUnsignedAmd64); 88 SaveFile.saveUrl2file(strModulesAmd64); 89 90 } 91 92 } catch (FileNotFoundException e) { 93 e.printStackTrace(); 94 } catch (IOException e) { 95 e.printStackTrace(); 96 } finally { 97 if (br != null) { 98 try { 99 br.close(); 100 } catch (IOException e1) { 101 e1.printStackTrace(); 102 } 103 } 104 if (fs != null) { 105 try { 106 fs.close(); 107 } catch (IOException e) { 108 e.printStackTrace(); 109 } 110 } 111 } 112 } 113 114 }
SaveFile.java
1 package getKernel; 2 3 import java.io.FileNotFoundException; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.net.HttpURLConnection; 8 import java.net.URL; 9 10 public class SaveFile { 11 12 public static void saveUrl2file(String strPath) { 13 if (strPath == null) 14 return; 15 FileOutputStream fs = null; 16 try { 17 int bytesum = 0; 18 int byteread = 0; 19 20 URL url = new URL(strPath); 21 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 22 conn.connect(); 23 if (conn.getResponseCode() == 200) { 24 InputStream inStream = conn.getInputStream(); 25 int size = conn.getContentLength(); 26 String filename = strPath.substring(strPath.lastIndexOf('/')+1, strPath.length()); 27 System.out.println(filename); 28 fs = new FileOutputStream(filename); 29 30 byte[] buffer = new byte[1024*1024]; 31 while ((byteread = inStream.read(buffer)) != -1) { 32 bytesum += byteread; 33 fs.write(buffer, 0, byteread); 34 String strPgs = "Progress:"+bytesum * 100 / size; 35 System.out.printf(strPgs); 36 for (int k = 0; k < strPgs.length(); k++) 37 System.out.print("\b"); 38 } 39 } 40 41 } catch (FileNotFoundException e) { 42 e.printStackTrace(); 43 } catch (IOException e) { 44 e.printStackTrace(); 45 } finally { 46 47 if (fs != null) { 48 try { 49 fs.close(); 50 } catch (IOException e) { 51 e.printStackTrace(); 52 } 53 } 54 } 55 } 56 }View Code