Saturday, September 29, 2012

Upload file example using apache common in servlet and jsp

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
      
       
        <form method="POST" action="UploadServlet" enctype="multipart/form-data">
         Upload File   <input type="file" name="file"/>
            <input type="submit" value="Upload"/>
        </form>
           
    </body>
</html>

Reading Email and Attachment Using Java Mail API

Properties properties = System.getProperties();
            Session session = Session.getDefaultInstance(properties, null);
            store = session.getStore(PROTOCOL);
            store.connect(HOST, USERNAME, USERPASSWORD);
            folder = store.getFolder(PROCESSFOLDER);
            if (folder == null && !folder.exists())
            {
                System.out.println("Invalid folder ");
            }
            folder.open(Folder.READ_WRITE);
            Message[] messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));

for (int i = 0; i < messages.length; i++)
            {
                Message msg = messages[i];
                //
                from = InternetAddress.toString(msg.getFrom());
                subject = msg.getSubject();
                to = InternetAddress.toString(msg.getRecipients(Message.RecipientType.TO));
                //
                String contentType = msg.getContentType();
                String textMsg = "";
                if (contentType.contains("text/html") || contentType.contains("text/plain"))
                {
                    textMsg = msg.getContent().toString();
                   
                } else if (contentType.contains("multipart"))
                {
                    Multipart multiPart = (Multipart) msg.getContent();
                    int partCount = multiPart.getCount();
                   
                    for (int j = 0; j < partCount; j++)
                    {
                        BodyPart part = multiPart.getBodyPart(j);
                        String disposition = part.getDisposition();
                        InputStream inputStream = null;
                        if (disposition == null)
                        {
                            // Check if plain
                            MimeBodyPart mbp = (MimeBodyPart) part;
                            if(mbp.getContent() instanceof MimeMultipart)
                            {
                                MimeMultipart mmp = (MimeMultipart) mbp.getContent();
                                System.out.println(mmp.getBodyPart(0).getContent().toString());
                                System.out.println("bodyContent " + bodyContent);
                            }
                            else
                            {
                                String msgText=  multiPart.getBodyPart(0).getContent().toString();
                                System.out.println("Message::::::::::: "+msgText);
                                inputStream=part.getInputStream();
                           
                                return;
                            }
                           
                        } else if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition()))
                        {
                            System.out.println("Attachment data::::::: " + part.getContent().toString());
                            MailUtility.saveMail(part.getInputStream(), part.getFileName());
                        } else
                        {
                            textMsg = part.getContent() != null ? part.getContent().toString() : "";
                            System.out.println("Text Msg::::::::: " + textMsg);
                        }
                    }
                }
                   
           
            }
        } catch (Exception ex)
        {
            ex.printStackTrace();
        }

Spring Boot Config Server and Config Client.

 In Spring cloud config we can externalise our configuration files to some repository like GIT HUT, Amazon S3 etc. Benefit of externalising ...