查看Java class文件版本的方法
- 格式:docx
- 大小:15.22 KB
- 文档页数:3
JavaScrit工具函数之获取浏览器类型和版本号获取浏览器类型和版本号是在Web开发过程中常见的需求之一。
JavaScript作为一门强大的编程语言,也提供了一些工具函数来满足这个需求。
本文将介绍几种常用的JavaScript工具函数,以及它们的使用方法和效果。
一、erAgenterAgent是JavaScript内置的一个属性,用于返回客户端浏览器的User-Agent字符串。
这个字符串中包含了浏览器名称、版本号以及操作系统等信息。
我们可以使用正则表达式来获取其中的浏览器信息。
代码示例:```javascriptvar userAgent = erAgent;var browser = {name: '',version: ''};if (/Opera/.test(userAgent)) { = 'Opera';browser.version =userAgent.match(/Opera\/[\d\.]+/)[0].replace('Opera/', '');} else if (/MSIE/.test(userAgent)) { = 'IE';browser.version = userAgent.match(/MSIE [\d\.]+/)[0].replace('MSIE ', '');} else if (/Firefox/.test(userAgent)) { = 'Firefox';browser.version =userAgent.match(/Firefox\/[\d\.]+/)[0].replace('Firefox/', '');} else if (/Chrome/.test(userAgent)) { = 'Chrome';browser.version =userAgent.match(/Chrome\/[\d\.]+/)[0].replace('Chrome/', '');} else if (/Safari/.test(userAgent)) { = 'Safari';browser.version =userAgent.match(/Version\/[\d\.]+/)[0].replace('Version/', '');} else { = 'Unknown';browser.version = 'Unknown';}console.log('Browser Name:', );console.log('Browser Version:', browser.version);```上述代码中,我们首先获取了userAgent字符串,然后通过一系列的正则表达式判断来确定浏览器的名称和版本号。
javatika文件类型判断方法及其实现Apache Tika是一个开源的Java工具包,用于从各种文档格式中提取内容,如文本、元数据和结构化数据等。
在Tika中,可以使用多种方式判断文件类型,包括文件扩展名、魔术字节和MIME类型等。
一、文件扩展名判断文件的扩展名是文件名的一部分,用于标识文件的类型。
在Tika中,可以通过文件的扩展名判断文件的类型。
Tika提供了一个方法用于获取文件的扩展名,即FilenameUtils.getExtension(方法。
以下是一个示例代码:```javapublic class FileUtilpublic static void main(String[] args)String fileName = "example.docx";String extension = FilenameUtils.getExtension(fileName);System.out.println("File extension: " + extension);//判断文件类型if (extension.equalsIgnoreCase("docx"))System.out.println("File type: Microsoft Word document");} else if (extension.equalsIgnoreCase("pdf"))System.out.println("File type: Portable Document Format");} else if (extension.equalsIgnoreCase("xlsx"))System.out.println("File type: Microsoft Excel spreadsheet");} elseSystem.out.println("File type: Unknown");}}```二、魔术字节判断文件的魔术字节是文件开头几个字节的特定值,用于标识文件的类型。
【黑马程序员】Java 如何判断文件的真实类型在我们实现一些需求的时候,有时候需要让用户上传文件,例如做论坛、社交类的应用和服务。
这时候可能就会出现一个需求:上传固定类型、固定大小的文件,也就是说,只允许用户上传图片或者文本、压缩包等。
其他的格式禁止上传,以防止用户恶意操作。
文件的大小非常容易判断,主要是文件类型如何确定。
有一种方式是按照扩展名去判断,例如图片就是.jpg 、.png 等,但是这种方式不准确,因为如果真是恶意上传,exe 文件也可以修改为xxx.jpg 上传成功。
所以,必须要用一种方式获取文件的真实类型。
即使文件后缀名修改了,也能找出来它的原形。
这个地方我们需要先了解一些基本知识,就是所有的文件的开头都有一段头信息,用来承担一定的任务,而每个不同类型的文件的头信息都是不同的,所以我们可以按照这种方式来进行判断。
首先,我们需要获取到文件头里这几个字节的数据,很简单,使用输入流来读取即可。
代码如下: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 public static String getFileHeader (String filePath ) {FileInputStream is = null;String value = null;try {is = new FileInputStream (filePath );byte[] b = new byte[4];is .read (b , 0, b.length );value = bytesToHexString (b );} catch (Exception e ) {} finally {if (null != is ) {try {is .close ();} catch (IOException e ) {}}}return value ;19 }这个地方很容易理解,就是把文件的前4个byte 取出来了,但是取出来的数据我们需要转换为16进制的字符串来表示,才能判断出来究竟是什么内容。
recognizes class file version全文共四篇示例,供读者参考第一篇示例:Java虚拟机(Java Virtual Machine)是一种能够执行Java字节码的虚拟机,是Java程序运行的基础。
Java虚拟机可以在任何支持Java的平台上运行,这为跨平台开发提供了便利。
在Java编程中,我们通常会编写Java源代码,然后通过编译器将其编译成字节码文件。
而Java虚拟机会加载并执行这些字节码文件。
在加载字节码文件时,Java虚拟机会检查字节码文件的版本号,以确保其能够正确解析和执行。
Java虚拟机对于不同版本的Java字节码文件都有对应的支持,因此当我们编写Java程序时,需要根据所使用的Java开发工具版本来选择合适的Java字节码版本。
Java虚拟机能够识别和支持的Java字节码版本可以通过java -version命令查看。
Java字节码文件的版本号主要由主版本号和次版本号组成,例如45.0、46.0、47.0等。
主版本号用于表明Java编译器的主要版本,而次版本号则表示该版本的修订号。
Java虚拟机会根据主版本号来确定其是否支持该版本的字节码文件,如果不支持,则会抛出UnsupportedClassVersionError异常。
在Java 8之后,主要的Java版本发布如下:- Java SE 8对应的主版本号是52,是Java 8发布的版本。
- Java SE 9对应的主版本号是53,是Java 9发布的版本。
- Java SE 10对应的主版本号是54,是Java 10发布的版本。
- Java SE 11对应的主版本号是55,是Java 11发布的版本。
- Java SE 12对应的主版本号是56,是Java 12发布的版本。
- Java SE 13对应的主版本号是57,是Java 13发布的版本。
- Java SE 14对应的主版本号是58,是Java 14发布的版本。
java获取文件类型的方法There are several methods to get the file type in Java. One commonly used method is to use the Java NIO package to read the file and extract the file type from the file's header. By reading the file header and identifying the file signature, it is possible to determine the file type. This method is reliable and widely used in many applications.在Java中有几种获取文件类型的方法。
一种常用的方法是使用Java NIO 包来读取文件,并从文件头中提取文件类型。
通过读取文件头并识别文件标识,可以确定文件类型。
这种方法在许多应用程序中是可靠和广泛使用的。
Another method to get the file type in Java is to use the URLConnection class to get the content type of the file from a URL. This method is useful when working with files that are hosted online and accessed through a URL. By using the URLConnection class, it is possible to retrieve the content type of the file and determine its type.在Java中获取文件类型的另一种方法是使用URLConnection类从URL获取文件的内容类型。
在Linux系统中,获取文件版本号的方法有多种,以下是一些常见的方法:
1.使用lsb_release命令:该命令是Linux标准库支持项目(Linux Standard
Base)的一部分,用于提供有关Linux发行版的详细信息。
通过在终端中输入
lsb_release -a命令,可以查看发行版的名称、版本号和其他相关信息。
2.查看/etc/os-release文件:大多数Linux发行版在/etc目录下包含一个名为
os-release的文件,其中包含有关操作系统的详细信息。
您可以使用cat命令或其他文本编辑器查看该文件的内容,其中包含发行版的名称和版本号等详细信息。
3.查看/proc/version文件:该文件包含了Linux内核的版本信息。
使用cat命令
可以查看该文件的内容,其中包括Linux内核的版本号和其他相关信息。
4.使用uname命令:该命令用于显示有关系统的详细信息,包括内核版本。
要查看
发行版的名称和版本号,可以在终端中运行以下命令:uname -a。
5.查看文件名或文件内容:有些可执行文件或共享库在其文件名中包含了版本号
信息,例如以“.so”为扩展名的共享库文件通常会在文件名中包含版本号。
另外,一些
可执行文件或共享库会在其头部或特定位置包含版本号信息,可以使用诸如strings命令的工具来提取这些信息。
需要注意的是,这些方法可能适用于不同的Linux发行版和文件类型。
对于某些特定的软件或库,您可能需要查阅其文档或使用特定的工具来获取版本号信息。
java获取当然JDK版本信息使⽤spring下的JdkVersion.java⽂件获取当然jdk版本信息。
JdkVersion.java代码:package org.springframework.core; /** * Internal helper class used to find the Java/JVM version * that Spring is operating on, to allow for automatically * adapting to the present platform's capabilities. * * <p>Note that Spring requires JVM 1.5 or higher, as of Spring 3.0. * * @author Rod Johnson * @author Juergen Hoeller * @author Rick Evans */ public abstract class JdkVersion { /** * Constant identifying the 1.3.x JVM (JDK 1.3). */ public static final int JAVA_13 = 0; /** * Constant identifying the 1.4.x JVM (J2SE 1.4). */ public static final int JAVA_14 = 1; /** * Constant identifying the 1.5 JVM (Java 5). */ public static final int JAVA_15 = 2; /** * Constant identifying the 1.6 JVM (Java 6). */ public static final int JAVA_16 = 3; /** * Constant identifying the 1.7 JVM (Java 7). */ public static final int JAVA_17 = 4; private static final String javaVersion; private static final int majorJavaVersion; static { javaVersion = System.getProperty("java.version"); // version String should look like "1.4.2_10" if (javaVersion.contains("1.7.")) { majorJavaVersion = JAVA_17; } else if (javaVersion.contains("1.6.")) { majorJavaVersion = JAVA_16; } else { // else leave 1.5 as default (it's either 1.5 or unknown) majorJavaVersion = JAVA_15; } } /** * Return the full Java version string, as returned by * <code>System.getProperty("java.version")</code>. * @return the full Java version string * @see System#getProperty(String) */ public static String getJavaVersion() { return javaVersion; } /** * Get the major version code. This means we can do things like * <code>if (getMajorJavaVersion() < JAVA_14)</code>. *@return a code comparable to the JAVA_XX codes in this class * @see #JAVA_13 * @see #JAVA_14 * @see #JAVA_15 *@see #JAVA_16 * @see #JAVA_17 */ public static int getMajorJavaVersion() { return majorJavaVersion; } /** * Convenience method to determine if the current JVM is at least Java 1.4. * @return <code>true</code> if the current JVM is at least Java 1.4 * @deprecated as of Spring 3.0 which requires Java 1.5+ * @see #getMajorJavaVersion() * @see #JAVA_14 * @see#JAVA_15 * @see #JAVA_16 * @see #JAVA_17 */ @Deprecated public static boolean isAtLeastJava14() { return true; } /** * Convenience method to determine if the current JVM is at least * Java 1.5 (Java 5). * @return <code>true</code> if the current JVM is at least Java 1.5 * @deprecated as of Spring 3.0 which requires Java 1.5+ * @see #getMajorJavaVersion() * @see #JAVA_15 * @see #JAVA_16 * @see #JAVA_17 */ @Deprecated public static boolean isAtLeastJava15() { return true; } /** * Convenience method to determine if the current JVM is at least * Java 1.6 (Java 6). * @return <code>true</code> if the current JVM is at least Java 1.6 * @deprecated as of Spring 3.0, in favor of reflective checks for * the specific Java 1.6 classes of interest * @see #getMajorJavaVersion() * @see #JAVA_16 * @see #JAVA_17 */ @Deprecated public static boolean isAtLeastJava16() { return (majorJavaVersion >= JAVA_16); } }GetJdkVersion.java代码:public class GetJdkVersion { @SuppressWarnings("static-access") public static void main(String[] args) { JdkVersion jdkVersion = new JdkVersion() { }; System.out.println(jdkVersion.getJavaVersion()); } }测试信息:1.6.0_23。
查看Java class文件版本的方法
查看的方法:
1.先装一个DJ java Decompiler 这个工具网上到处都是,主要就是反编译
Java class文件。
2.在DJ Java Decompiler的菜单中View->class version info,就会弹出对话框如:
Major Version: 49
Minor Version: 0
参照下面的引用,可知class由jdk1.5编译而来。
jackpk已经亲测,以上方法非常实用。
关于JDK版本,参见:
《unsupported major.minor version 解决方法》
(/jackpk/article/details/19755119)
引用
/javadetails/java-0544.html
The first 4 bytes are a magic number, 0xCAFEBABe, to identify a valid class file then the next 2 bytes identify the class format version (major and minor).
Possible major/minor value :
major minor Java platform version
45 3 1.0
45 3 1.1
46 0 1.2
47 0 1.3
48 0 1.4
49 0 1.5
50 0 1.6
import java.io.*;
public class ClassVersionChecker {
public static void main(String[] args) throws IOException {
for (int i = 0; i < args.length; i++)
checkClassVersion(args[i]);
}
private static void checkClassVersion(String filename)
throws IOException
{
DataInputStream in = new DataInputStream
(new FileInputStream(filename));
int magic = in.readInt();
if(magic != 0xcafebabe) {
System.out.println(filename + " is not a valid class!");;
}
int minor = in.readUnsignedShort();
int major = in.readUnsignedShort();
System.out.println(filename + ": " + major + " . " + minor);
in.close();
}
}
> java ClassVersionChecker ClassVersionChecker.class ClassVersionChecker.class: 49 . 0
from The Java Virtual Machine Specification
magic
The magic item supplies the magic number identifying the class file format; it has
the value 0xCAFEBABE.
minor_version, major_version
The values of the minor_version and major_version items are the minor and major version numbers of this class file.Together, a major and a minor version number determine the version of the class file format. If a class file has major version number M and minor version number m, we denote the version of its class file format as M.m. Thus, class file format versions may be ordered lexicographically, for example, 1.5 < 2.0 < 2.1.
A Java virtual machine implementation can support a class file format of version v if and only if v lies in some contiguous range Mi.0 v Mj.m. Only Sun can specify what range of versions a Java virtual machine implementation conforming to a certain release level of the Java platform may support.。