Пример #1
0
  /*
   * (non-Javadoc)
   *
   * @see javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest,
   * javax.servlet.ServletResponse)
   */
  @Override
  public void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    try {
      String messageId = request.getParameter("messageId");
      String attachmentIndex = request.getParameter("attachmentIndex");
      boolean isThumbnail = Boolean.valueOf(request.getParameter("thumbnail")).booleanValue();

      if (messageId != null) {
        IMailbox mailbox = SessionManager.get().getMailbox();
        Message msg = mailbox.getCurrentFolder().getMessageById(Long.parseLong(messageId));

        if (isThumbnail) {
          List<MimePart> attachmentList = MessageUtils.attachmentsFromPart(msg);
          int index = Integer.valueOf(attachmentIndex);

          MimePart retrievePart = attachmentList.get(index);

          ContentType contentType = new ContentType(retrievePart.getContentType());
          response.setContentType(contentType.getBaseType());

          BufferedInputStream bufInputStream =
              new BufferedInputStream(retrievePart.getInputStream());
          OutputStream outputStream = response.getOutputStream();

          writeScaledImage(bufInputStream, outputStream);

          bufInputStream.close();
          outputStream.flush();
          outputStream.close();
        } else {
          Part imagePart = findImagePart(msg);
          if (imagePart != null) {
            ContentType contentType = new ContentType(imagePart.getContentType());
            response.setContentType(contentType.getBaseType());

            BufferedInputStream bufInputStream =
                new BufferedInputStream(imagePart.getInputStream());
            OutputStream outputStream = response.getOutputStream();

            byte[] inBuf = new byte[1024];
            int len = 0;
            int total = 0;
            while ((len = bufInputStream.read(inBuf)) > 0) {
              outputStream.write(inBuf, 0, len);
              total += len;
            }

            bufInputStream.close();
            outputStream.flush();
            outputStream.close();
          }
        }
      }
    } catch (Exception ex) {
      log.error(ex.getMessage(), ex);
    }
  }
Пример #2
0
  /**
   * @param parent
   * @return
   */
  private Part findImagePart(Part parent) {

    try {
      if (MessageUtils.isImagepart(parent)) {
        return parent;
      } else if (parent.isMimeType("multipart/*")) {
        Multipart mp;
        mp = (Multipart) parent.getContent();
        int count = mp.getCount();
        for (int i = 0; i < count; i++) {
          Part subPart = findImagePart(mp.getBodyPart(i));
          if (subPart != null) {
            return subPart;
          }
        }
      }
    } catch (MessagingException e) {
      log.error(e.getMessage(), e);
    } catch (IOException e) {
      log.error(e.getMessage(), e);
    }

    return null;
  }