public void handle(FileItem item, Map<String, Object> result) { BlobWriter writer = blobManager.newFile(item.getContentType()); OutputStream out = writer.getOutputStream(); InputStream in = null; try { in = item.getInputStream(); try { int size = 0; while ((size = in.read(buffer)) != -1) { out.write(buffer, 0, size); } } finally { IOTools.close(in, out); } BlobInfo info = writer.getBlobInfo(); result.put("fileId", info.id()); result.put("contentType", info.contentType()); result.put("md5", info.md5()); result.put("sucessful", "true"); blobManager.commit(writer); } catch (Exception ex) { result.put("sucessful", "false"); blobManager.cancel(writer); } }
public void finish(MessageDoc ctx, boolean closeStream) { if (!ctx.getReturnCode().equals("core.ok")) { throw new ServantException(ctx.getReturnCode(), ctx.getReason()); } OutputStream out = null; try { out = ctx.getOutputStream(); } catch (IOException e) { } finally { if (closeStream) { IOTools.close(out); } } }
public int actionProcess(Context ctx) throws Exception { ctx.asMessage(BlobMessage.class); String fileId = getArgument("fileId", ctx); String domain = getArgument("domain", "default", ctx); BlobManager manager = BlobTool.getBlobManager(domain); if (manager == null) { throw new ServantException( "core.blob_not_found", "Can not find a blob manager named " + domain); } BlobReader reader = manager.getFile(fileId); if (reader == null) { throw new ServantException("core.blob_not_found", "Can not find a blob file named " + fileId); } BlobInfo info = reader.getBlobInfo(); ctx.setResponseContentType(info.contentType()); InputStream in = reader.getInputStream(0); OutputStream out = ctx.getOutputStream(); try { int size = 0; while ((size = in.read(buffer)) != -1) { out.write(buffer, 0, size); } } finally { IOTools.close(in); } return 0; }