blobstore 中有一个xml文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Blobs>
</Blobs>
以下 servlet 将文件上传到blobstore。上传文件后,它获取文件密钥并调用一个更新 xml 的 Bean,通过添加一个以 text-content 为密钥的节点。
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);
}
}
}
以下是上述 servlet 调用以更新/写入xml的Bean。
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 文件进行条目。日志中没有任何错误日志。我不明白这是为什么。问题是什么 ?