ブロブストアには、次のようなxmlファイルがあります。
<?xml version="1.0" encoding="UTF-8"?>
<Blobs>
</Blobs>
次のサーブレットは、ファイルをblobstoreにアップロードします。ファイルをアップロードした後、ファイルキーを取得し、テキスト コンテンツがキーであるノードを追加して xml を更新する Bean を呼び出します。
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);
}
}
}
以下は、 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 ファイルにエントリが作成されません。ログにエラー ログはありません。この理由がわかりません。何が問題ですか ?