序列化并使用数据库中的文档
在处理文档时,您可能需要完成的任务之一是在数据库中存储和检索Document对象。 例如,如果您正在实施任何类型的内容管理系统,这将是必要的。 所有以前版本的文档都必须存储在数据库系统中。 当您的应用程序提供基于web的服务时,在数据库中存储文档的能力也非常有用。
Aspose.Words提供了将文档转换为字节数组的功能,以便在数据库中处理此文档。
将文档转换为字节数组
要将文档存储在数据库中或准备文档以在web上传输,通常需要对文档进行序列化以获得字节数组。
序列化Aspose.Words中的Document对象:
- 使用Document类的Save方法重载将其保存到MemoryStream中。
- 调用ToArray方法,该方法返回以字节形式表示文档的字节数组。
然后可以颠倒上面的步骤,将字节加载回Document对象。
下面的示例演示如何序列化Document对象以获取字节数组,然后如何取消序列化字节数组以再次获取Document对象:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Load the document. | |
Document doc = new Document(dataDir + "Test File (doc).doc"); | |
// Create a new memory stream. | |
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); | |
// Save the document to stream. | |
doc.save(outStream, SaveFormat.DOCX); | |
// Convert the document to byte form. | |
byte[] docBytes = outStream.toByteArray(); | |
// The bytes are now ready to be stored/transmitted. | |
// Now reverse the steps to load the bytes back into a document object. | |
ByteArrayInputStream inStream = new ByteArrayInputStream(docBytes); | |
// Load the stream into a new document object. | |
Document loadDoc = new Document(inStream); |
在数据库中存储、读取和删除文档
本节演示如何将文档保存在数据库中,然后将其加载回Document
对象以进行处理。 为简单起见,文件名是用于存储和从数据库中获取文档的密钥。 数据库包含两列。 第一列"FileName"存储为字符串,用于标识文档。 第二列"FileContent"存储为BLOB
对象,该对象以字节形式存储文档对象。
下面的代码示例演示如何设置与数据库的连接并执行命令:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
String url1 = "jdbc:mysql://localhost:3306/test"; | |
String user = "root"; | |
String password = "123"; | |
// Open a database connection. | |
Connection mConnection = DriverManager.getConnection(url1, user, password); |
下面的代码示例演示如何将文档保存到数据库,然后再次读取同一文档,最后从数据库中删除包含该文档的记录:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Store the document to the database. | |
StoreToDatabase(doc, mConnection); | |
// Read the document from the database and store the file to disk. | |
Document dbDoc = ReadFromDatabase(dataDir + fileName, mConnection); | |
// Save the retrieved document to disk. | |
dbDoc.save(dataDir + fileName); | |
// Delete the document from the database. | |
DeleteFromDatabase(dataDir + fileName, mConnection); | |
// Close the connection to the database. | |
mConnection.close(); |
将文档保存到数据库
要将文档保存在数据库中,请将此文档转换为字节数组,如本文开头所述。 然后,将此字节数组保存到数据库字段中。
下面的代码示例演示如何将文档保存到指定的数据库:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
public static void StoreToDatabase(Document doc, Connection mConnection) throws Exception { | |
// Create an output stream which uses byte array to save data | |
ByteArrayOutputStream aout = new ByteArrayOutputStream(); | |
// Save the document to byte array | |
doc.save(aout, SaveFormat.DOCX); | |
// Get the byte array from output steam | |
// the byte array now contains the document | |
byte[] buffer = aout.toByteArray(); | |
// Get the filename from the document. | |
String fileName = doc.getOriginalFileName(); | |
String filePath = fileName.replace("\\", "\\\\"); | |
// Create the SQL command. | |
String commandString = "INSERT INTO Documents (FileName, FileContent) VALUES('" + filePath + "', '" + buffer | |
+ "')"; | |
Statement statement = mConnection.createStatement(); | |
statement.executeUpdate(commandString); | |
} |
指定commandString,它是执行所有工作的SQL表达式:
- 要将文档保存到数据库中,使用"INSERTINTO"命令并指定一个表以及两个记录字段的值–FileName和FileContent。 为了避免额外的参数,文件名取自Document对象本身。
FileContent
字段值是从内存流中分配的字节,该内存流包含存储文档的二进制表示。 - 剩余的代码行执行将Aspose.Words文档存储在数据库中的命令。
从数据库检索文档
要从数据库中检索文档,请选择包含文档数据的记录作为字节数组。 然后将字节数组从记录加载到MemoryStream中,并创建一个Document对象,该对象将从MemoryStream加载文档。
下面的代码示例演示如何使用文件名作为获取此文档的键从指定的数据库中检索和返回文档:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
private static Document ReadFromDatabase(String fileName, Connection mConnection) throws Exception { | |
// Create the SQL command. | |
String commandString = "SELECT * FROM Documents WHERE FileName=?"; | |
PreparedStatement statement = mConnection.prepareStatement(commandString); | |
statement.setString(1, fileName); | |
Document doc = null; | |
ResultSet result = statement.executeQuery(); | |
if (result.next()) { | |
Blob blob = result.getBlob("FileContent"); | |
InputStream inputStream = blob.getBinaryStream(); | |
doc = new Document(inputStream); | |
inputStream.close(); | |
System.out.println("File saved"); | |
} | |
result.close(); | |
return doc; | |
} |
从数据库中删除文档
要从数据库中删除文档,请使用适当的SQL命令,而不对Document对象进行任何操作。
下面的代码示例演示如何从数据库中删除文档,使用文件名来获取记录:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
private static void DeleteFromDatabase(String fileName, Connection mConnection) throws Exception { | |
// Create the SQL command. | |
String commandString = "DELETE FROM Documents WHERE FileName='" + fileName + "'"; | |
Statement statement = mConnection.createStatement(); | |
// Delete the record. | |
statement.execute(commandString); | |
} |