Java可以使用Apache POI和iText库来实现将Word或PDF文件转换成Base64编码,并保存到数据库中。以下是一个示例代码:
```java
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.codec.binary.Base64;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
public class FileToBase64 {
public static void main(String[] args) throws Exception {
String filePath = "test.docx"; // Word或PDF文件路径
String fileType = getFileType(filePath); // 获取文件类型
String base64Str = fileToBase64(filePath, fileType); // 文件转Base64字符串
saveToDatabase(base64Str); // 保存到数据库
}
// 获取文件类型
public static String getFileType(String filePath) {
String[] arr = filePath.split("\\.");
return arr[arr.length - 1];
}
// 文件转Base64字符串
public static String fileToBase64(String filePath, String fileType) throws IOException {
InputStream in = new FileInputStream(filePath);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
in.close();
out.close();
byte[] data = out.toByteArray();
String base64Str = "";
if (fileType.equals("doc") || fileType.equals("docx")) {
// Word文件转Base64字符串
if (fileType.equals("doc")) {
WordExtractor extractor = new WordExtractor(new ByteArrayInputStream(data));
base64Str = Base64.encodeBase64String(extractor.getText().getBytes());
} else if (fileType.equals("docx")) {
XWPFWordExtractor extractor = new XWPFWordExtractor(new ByteArrayInputStream(data));
base64Str = Base64.encodeBase64String(extractor.getText().getBytes());
}
} else if (fileType.equals("pdf