... Blob blob = blobStore.getBlob(containerName, fileName); InputStream is = blob.getPayload.openStream(); String contentType = "image/jpeg"; //or whatever content the blob is. AttachmentInputStream ais = new AttachmentInputStream("attachment_id", is, contentType); db.createAttachment("new_document_id", ais); ...
String contentType = "image/png"; AttachmentInputStream data = db.getAttachment("document_id","attachment_id"); response.setContentType(contentType); response.setContentLength(longToInt(data.getContentLength())); OutputStream out = response.getOutputStream(); byte[] buffer = new byte[1024]; int count = 0; while ((count = data.read(buffer)) >= 0) { out.write(buffer, 0, count); } out.close(); data.close();
@Override public String createAttachment(String docId, String revision, AttachmentInputStream data) { assertDocIdHasValue(docId); URI uri = dbURI.append(docId).append(data.getId()); if (revision != null) { uri.param("rev", revision); } return restTemplate.put(uri.toString(), data, data.getContentType(), data.getContentLength(), revisionHandler).getRevision(); }
@RequestMapping(value = "/stream/{language}/{name}", method = RequestMethod.GET) public void getStreamContent(@PathVariable String language, @PathVariable String name, HttpServletResponse response) throws IOException { LOG.info(String.format("Getting resource for : stream:%s:%s", language, name)); OutputStream out = null; AttachmentInputStream contentStream = null; try { out = response.getOutputStream(); contentStream = (AttachmentInputStream) cmsLiteService.getStreamContent(language, name).getInputStream(); response.setContentLength((int) contentStream.getContentLength()); response.setContentType(contentStream.getContentType()); response.setHeader("Accept-Ranges", "bytes"); response.setStatus(HttpServletResponse.SC_OK); IOUtils.copy(contentStream, out); } catch (ContentNotFoundException e) { LOG.error(String.format("Content not found for : stream:%s:%s%n:%s", language, name, Arrays.toString(e.getStackTrace()))); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, NOT_FOUND_RESPONSE); } finally { IOUtils.closeQuietly(contentStream); IOUtils.closeQuietly(out); } }
private void createAttachment(String contentId, String contentRevision, StreamContent content) { BufferedInputStream bufferedInputStream = new BufferedInputStream(content.getInputStream()); AttachmentInputStream attachmentInputStream = new AttachmentInputStream(contentId, bufferedInputStream, content.getContentType()); db.createAttachment(contentId, contentRevision, attachmentInputStream); } }
private AttachmentInputStream getAttachment(String attachmentId, URI uri) { HttpResponse r = restTemplate.get(uri.toString()); return new AttachmentInputStream(attachmentId, r.getContent(), r.getContentType(), r.getContentLength()); }