Java xml文件没有';t更新。这是什么原因?
blobstore中有一个xml文件,如下所示:Java xml文件没有';t更新。这是什么原因?,java,xml,google-app-engine,jakarta-ee,blobstore,Java,Xml,Google App Engine,Jakarta Ee,Blobstore,blobstore中有一个xml文件,如下所示: <?xml version="1.0" encoding="UTF-8"?> <Blobs> </Blobs> 下面是一个Bean,上面的servlet调用它来更新/写入xml } 文件上载正常,但没有对xml文件创建条目。日志中没有任何错误日志。我不明白为什么会这样。有什么问题吗?我认为该文件没有持久化,因为您忘记了完成: 附加到它之后,当所有的操作都完成后,您必须完成该文件才能使其持久化 尝试添加:
<?xml version="1.0" encoding="UTF-8"?>
<Blobs>
</Blobs>
下面是一个Bean,上面的servlet调用它来更新/写入xml
}
文件上载正常,但没有对xml文件创建条目。日志中没有任何错误日志。我不明白为什么会这样。有什么问题吗?我认为该文件没有持久化,因为您忘记了完成: 附加到它之后,当所有的操作都完成后,您必须完成该文件才能使其持久化 尝试添加:
writeChannel.closeFinally()
或writeChannel.close()
在transformer.transform(源、结果)之后代码>
编辑
您从未写入可写文件
:
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
但是试着写入一个以读取和锁定方式打开的文件。试着为if(mimeType.isImage()){
(注意:我假设if需要更正)@RC添加一些日志和else
。我已经验证了if
块每次都在工作。我尝试了相同的老方法。当我看到xml时(从blobstore)它没有添加任何节点。从你在回答中引用的链接中,它说,要查看该文件,互联网上的任何人都可以访问http://commondatastorage.googleapis.com/mybucket/myfile
我的链接是这样的,但是当我访问页面时,它说没有这样的键。为什么会这样?你能对这个问题发表评论吗?所以我需要替换fi在语句fileService.openWriteChannel(…)
?“关闭”文件,并写入writeablefile
,如果文件可以被覆盖,它应该可以工作(我无法测试,因为我没有这台计算机上的appengine测试环境)
public class UpdateXml {
private final String xmlKey = "AMIfv95NSB_FKs2v6o0dIKkSdmSIJVtE0oq9X4hyTbxOTglDi6XoDaTUBlXxa3OsPfDo8ZQZF8kFb059zg4kJ3lK2MlgudC_nkQdwCpx1kYP9Rwb40s0HClFzzIIUv-UtMbuycxixVtfw-fYyhC3MXvFc1Gh-Cv9mQ";
public void appendKey(String key) {
try {
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.getBlobFile( new BlobKey( xmlKey ) );
FileReadChannel readChannel = fileService.openReadChannel(file, true);
InputStream stream = Channels.newInputStream(readChannel);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
Document doc = documentBuilder.parse(stream);
Element root = doc.getDocumentElement();
Element blobKey = doc.createElement("blob-key");
blobKey.setTextContent(key);
root.appendChild(blobKey);
GSFileOptionsBuilder optionsBuilder = new GSFileOptionsBuilder();
optionsBuilder.setBucket("my-bucket");
optionsBuilder.setKey(xmlKey);
optionsBuilder.setMimeType("text/html");
optionsBuilder.setAcl("public_read");
optionsBuilder.addUserMetadata("suhail", "gupta");
AppEngineFile writableFile = fileService.createNewGSFile(optionsBuilder.build());
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
OutputStream output = Channels.newOutputStream(writeChannel);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(doc);
transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
StreamResult result = new StreamResult(output);
transformer.transform(source, result);
}catch(Exception exc) {
exc.printStackTrace();
}
}
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);