java程序: 从kernel.ubuntu.com下载kernel - HttpURLConnection
用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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package getKernel; 2 3 import java.io.BufferedReader; 4 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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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 }
人生苦短,快用python
#!/usr/bin/env python # coding: utf-8 import urllib.request import threading import re import sys patterns = [ r'linux-headers-[_.\-\d]*all.deb', r'linux-headers-[_.\-\d]*\-generic[_.\-\d]*amd64.deb', r'linux-image-unsigned-[_.\-\d]*\-generic[_.\-\d]*amd64.deb', r'linux-modules-[_.\-\d]*\-generic[_.\-\d]*amd64.deb' ] file_type = [ 'header-all', 'header-generic-amd64', 'image-unsigned-amd64', 'modules-generic-amd64', ] class Downloader(threading.Thread): def __init__(self, version, index_str, file_index): threading.Thread.__init__(self) self.file_index = file_index self.version = version try: print('Searching', file_type[self.file_index], 'URL.') self.file = re.search( patterns[self.file_index], index_str).group(0) self.url = 'https://kernel.ubuntu.com/~kernel-ppa/mainline/' + \ self.version + '/' + self.file print('Found', self.url) except Exception as identifier: print('File Not Found!', file_type[self.file_index], identifier) def report_hook(self, count, block_size, total_size): new_progress = int(100.0 * count * block_size / total_size) print('{0}:{1},{2}%'.format( file_type[self.file_index], '['+str(count)+' / ' + str(int(total_size/block_size))+']', new_progress)) def run(self): print('Start to download', self.file) try: urllib.request.urlretrieve( self.url, self.file, reporthook=self.report_hook) except Exception as identifier: print('Cannot download', self.file, identifier) if __name__ == '__main__': if len(sys.argv) != 2: print('Please enter the kernel version.') sys.exit(-1) kernel_version = sys.argv[1] target_index = 'https://kernel.ubuntu.com/~kernel-ppa/mainline/' + kernel_version + '/' # get the file names and urls of the kernel files. try: index_str = '' print('Get', kernel_version, 'index from kernel.ubuntu.com.') with urllib.request.urlopen(target_index) as f: index_str = str(f.read()) print('Got! Try to get kernel files for', kernel_version) threads = [] for i in range(4): threads.append(Downloader(kernel_version, index_str, i)) for t in threads: t.start() for t in threads: t.join() print('All files for', kernel_version, 'downloaded!') except Exception as e: print(e)