gpt4 book ai didi

java - xml 文件不更新。这是什么原因呢?

转载 作者:行者123 更新时间:2023-11-30 04:41:22 26 4
gpt4 key购买 nike

blobstore 中有一个 xml 文件,如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<Blobs>

</Blobs>

以下 Servlet 将文件上传到 blobstore。上传文件后,它获取file键并调用一个Bean,通过添加一个其文本内容为键的节点来更新xml。

public class UploadImagesToAisle extends HttpServlet {

/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getParameter("Data");
PrintWriter writer = response.getWriter();
try {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if( !isMultipart ) {
writer.println("File cannot be uploaded !");
} else {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
List list = null;
while(iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
String fileName = item.getName();
String mimeType = new MimeType().getMimeType(fileName);
if(mimeType == null) {
response.sendRedirect("error.jsp");
}
InputStream stream = item.openStream();
if(item.isFormField()) {
// Process regular form field (input type="text|radio|checkbox|etc", select, etc).
} else {
// Get a file service
FileService fileService = FileServiceFactory.getFileService();
// Create a new Blob file with mime-type "text/plain"
AppEngineFile file = fileService.createNewBlobFile(mimeType, fileName);
// Open a channel to write to it
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
// copy byte stream from request to channel
byte[] buffer = new byte[10000];
int len;
while ((len = stream.read(buffer)) > 0) {
writeChannel.write(ByteBuffer.wrap(buffer, 0, len));
}

writeChannel.closeFinally();

if(MimeType.isImage) {
String key = fileService.getBlobKey(file).getKeyString();
// update the xml file with the key
new UpdateXml().appendKey(key); // BEAN CALLED
}

RequestDispatcher dispatcher = request.getRequestDispatcher("private/cpanel/PcPanel.jsp");
dispatcher.forward(request, response);
}
}
}
}catch(Exception exc) {
writer.println(exc);
}
}
}

下面是一个 Bean,由上面的 servlet 调用来更新/写入 xml

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();
}
}

}

文件上传正常,但未在 xml 文件中添加条目。日志中没有任何错误日志。我不明白这是为什么。有什么问题吗?

最佳答案

我认为该文件未保留,因为您忘记了最终确定:

append to it and when all done writing, you must finalize the file for it to persist

Source

尝试在 transformer.transform(source, result); 之后添加: writeChannel.closeFinally();writeChannel.close()/p><小时/>

编辑

你永远不会写入writableFile:

FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);

但尝试写入以读取方式打开并锁定的文件。

关于java - xml 文件不更新。这是什么原因呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12212424/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com