ImportJava.io.ByteArrayInputStream;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.ArrayList;Importjava.util.List;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipFile;Importorg.jdom.Document;Importorg.jdom.Element;ImportOrg.jdom.Namespace;ImportOrg.jdom.input.SAXBuilder;Importcn.zsmy.constant.Constant; Public classApkutil {Private Static FinalNamespace NS = Namespace.getnamespace ("Http://schemas.android.com/apk/res/android"); @SuppressWarnings ({"Unchecked", "Rawtypes"}) Public Staticapkinfo getapkinfo (String apkpath) {apkinfo apkinfo=NewApkinfo (); Saxbuilder Builder=NewSaxbuilder (); Document Document=NULL; Try{document=Builder.build (Getxmlinputstream (Apkpath)); }Catch(Exception e) {e.printstacktrace (); } Element Root= Document.getrootelement ();//-->manifest with nodeApkinfo.setversioncode (Root.getattributevalue ("Versioncode", NS)); Apkinfo.setversionname (Root.getattributevalue ("Versionname", NS)); Apkinfo.setapkpackage (Root.getattributevalue ("Package")); Element ELEMUSESDK= Root.getchild ("Uses-sdk");//child node-->uses-sdkApkinfo.setminsdkversion (Elemusesdk.getattributevalue ("Minsdkversion", NS)); List listpermission= Root.getchildren ("uses-permission");//A child node is a collectionList permissions =NewArrayList (); for(Object object:listpermission) {String permission= (Element) object. Getattributevalue ("name"), NS); Permissions.add (permission); } apkinfo.setuses_permission (permissions); Constant.MY_LOG.debug (\ n Version number: "+apkinfo.getversioncode () +" \ n Version Name: "+apkinfo.getversionname () +" \ n Package Name: "+apkinfo.getapkpackage ()); //String str = "\ n version number:" \ +versioncode+ "\ n version name:" +versionname+ "\ n Package Name:" +packagename;Constant.MY_LOG.debug (Root.getattributes (). toString ()); returnApkinfo;//String s = root.getattributes (). toString ();//String c[] = S.split (",");//String versioncode = null;//String versionname = null;//String packagename = null;//For (String a:c) {//if (a.contains ("Versioncode")) {//Versioncode = a.substring (A.indexof ("versioncode=\") +13, A.lastindexof ("\" "));// }//if (a.contains ("Versionname")) {//versionname = a.substring (A.indexof ("versionname=\") +13, A.lastindexof ("\" "));// }//if (a.contains ("package")) {//PackageName = a.substring (A.indexof ("package=\") +9, A.lastindexof ("\" "));// }// } // //Constant.MY_LOG.debug ("\ n version number:" +versioncode+ "\ nthe version name:" +versionname+ "\ n Package Name:" +packagename ");//String str = "\ n version number:" \ +versioncode+ "\ n version name:" +versionname+ "\ n Package Name:" +packagename;////return Root.getattributes (). toString ();//return str;//return "SS"; } Private StaticInputStream Getxmlinputstream (String apkpath) {InputStream InputStream=NULL; InputStream Xmlinputstream=NULL; ZipFile ZipFile=NULL; Try{ZipFile=NewZipFile (Apkpath); ZipEntry ZipEntry=NewZipEntry ("Androidmanifest.xml"); InputStream=Zipfile.getinputstream (ZipEntry); Axmlprinter Xmlprinter=NewAxmlprinter (); Xmlprinter.startprinf (InputStream); Xmlinputstream=NewBytearrayinputstream (Xmlprinter.getbuf (). toString (). GetBytes ("UTF-8")); } Catch(IOException e) {e.printstacktrace (); Try{inputstream.close (); Zipfile.close (); } Catch(IOException E1) {e1.printstacktrace (); } } returnXmlinputstream; }}
Call:
@ResponseBody @RequestMapping (value= "/getapkversion", method =requestmethod.post) PublicString getapkversion (versionform form, Multipartfile apkfile)throwsException {Constant.MY_LOG.debug ("Get the uploaded apk version"); File Apktempfile=NewFile (DictInit.dictMap.get (Constant.Dict.APK_UPLOAD_PATH) + "temp.apk"); //file Apktempfile = new file ("d:\\temp.apk");//for testingApkfile.transferto (Apktempfile); //Get apk InfoApkinfo Apkinfo =NewApkinfo (); Apkinfo=Apkutil.getapkinfo (Apktempfile.getpath ()); returnApkinfo.getversionname (); }
Java to extract information such as APK version by parsing files