javax.activation 包是 JavaBeans Activation Framework (JAF) 的核心部分,主要用于处理 MIME 类型的数据,提供数据内容类型识别、命令映射和数据操作等功能。
DataSource: 数据源接口,封装数据访问
DataHandler: 数据处理类,提供统一的数据访问接口
MimetypesFileTypeMap: MIME 类型映射
CommandMap: 命令映射管理
MailcapCommandMap: 邮件能力命令映射
import javax.activation.MimetypesFileTypeMap;
import java.io.File;
public class MimeTypeExample {
public static void main(String[] args) {
MimetypesFileTypeMap mimeMap = new MimetypesFileTypeMap();
// 识别文件MIME类型
File file = new File("example.pdf");
String mimeType = mimeMap.getContentType(file);
System.out.println("File MIME type: " + mimeType);
// 通过文件名识别
String fileNameMime = mimeMap.getContentType("image.jpg");
System.out.println("File name MIME type: " + fileNameMime);
}
}import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import java.io.*;
public class DataHandlerExample {
public static void main(String[] args) {
try {
// 创建文件数据源
DataSource dataSource = new FileDataSource("test.txt");
DataHandler dataHandler = new DataHandler(dataSource);
// 获取MIME类型
System.out.println("Content Type: " + dataHandler.getContentType());
// 读取数据
Object content = dataHandler.getContent();
if (content instanceof InputStream) {
InputStream is = (InputStream) content;
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line;
System.out.println("File content:");
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}import javax.activation.DataSource;
import java.io.*;
public class StringDataSource implements DataSource {
private String data;
private String contentType;
private String name;
public StringDataSource(String data, String contentType, String name) {
this.data = data;
this.contentType = contentType;
this.name = name;
}
@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(data.getBytes("UTF-8"));
}
@Override
public OutputStream getOutputStream() throws IOException {
throw new IOException("Read-only data source");
}
@Override
public String getContentType() {
return contentType;
}
@Override
public String getName() {
return name;
}
}
// 使用自定义DataSource
public class CustomDataSourceExample {
public static void main(String[] args) {
try {
String content = "Hello, this is a test string data source!";
DataSource ds = new StringDataSource(content, "text/plain", "test.txt");
DataHandler handler = new DataHandler(ds);
System.out.println("Content Type: " + handler.getContentType());
System.out.println("Name: " + handler.getName());
// 读取内容
Object obj = handler.getContent();
if (obj instanceof InputStream) {
BufferedReader reader = new BufferedReader(
new InputStreamReader((InputStream) obj));
System.out.println("Content: " + reader.readLine());
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}import javax.activation.CommandInfo;
import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.activation.DataHandler;
import java.awt.*;
import java.io.File;
public class CommandMapExample {
public static void main(String[] args) {
try {
// 添加自定义命令映射
MailcapCommandMap mailcap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mailcap.addMailcap("text/plain;; x-java-view=com.example.TextViewer");
// 创建数据处理器
File file = new File("document.txt");
DataHandler handler = new DataHandler(file.toURI().toURL());
// 获取命令
CommandInfo[] commands = handler.getPreferredCommands();
for (CommandInfo cmd : commands) {
System.out.println("Command: " + cmd.getCommandName());
System.out.println("Class: " + cmd.getCommandClass());
}
// 执行查看命令(如果系统支持)
if (Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(file);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.activation.MimetypesFileTypeMap;
import java.io.*;
public class FileProcessor {
private MimetypesFileTypeMap mimeMap;
public FileProcessor() {
mimeMap = new MimetypesFileTypeMap();
// 添加自定义MIME类型映射
mimeMap.addMimeTypes("application/custom custom");
}
public void processFile(String filePath) {
try {
File file = new File(filePath);
if (!file.exists()) {
System.out.println("File not found: " + filePath);
return;
}
DataSource dataSource = new FileDataSource(file);
DataHandler dataHandler = new DataHandler(dataSource);
// 显示文件信息
System.out.println("File: " + file.getName());
System.out.println("MIME Type: " + dataHandler.getContentType());
System.out.println("Size: " + file.length() + " bytes");
// 根据MIME类型处理文件
String mimeType = dataHandler.getContentType();
if (mimeType.startsWith("text/")) {
processTextFile(dataHandler);
} else if (mimeType.startsWith("image/")) {
processImageFile(dataHandler);
} else {
System.out.println("Unsupported file type: " + mimeType);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void processTextFile(DataHandler handler) {
try {
System.out.println("Processing text file...");
BufferedReader reader = new BufferedReader(
new InputStreamReader(handler.getInputStream()));
String line;
int lineCount = 0;
while ((line = reader.readLine()) != null) {
lineCount++;
if (lineCount <= 5) { // 只显示前5行
System.out.println("Line " + lineCount + ": " + line);
}
}
System.out.println("Total lines: " + lineCount);
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void processImageFile(DataHandler handler) {
System.out.println("Processing image file...");
System.out.println("This would typically involve image processing operations");
// 实际应用中这里会包含图像处理逻辑
}
public static void main(String[] args) {
FileProcessor processor = new FileProcessor();
if (args.length > 0) {
processor.processFile(args[0]);
} else {
processor.processFile("test.txt"); // 默认文件
}
}
}<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>implementation 'javax.activation:activation:1.1.1'1、Java 版本兼容性:从 Java 9 开始,javax.activation 包已被标记为过时
2、替代方案:在较新的 Java 版本中,建议使用 jakarta.activation 包
3、功能限制:主要用于 MIME 类型处理和简单的数据操作