Monday, 13 August 2012

Handle the error Java heap space and PermGen space

In Myeclipse :----
Go to preference server tomcat jdk and paste the following code

-Xms512m -Xmx1024m
XX:PermSize=128m -XX:MaxPermSize=512m

 External Tomcat :----
Go to tomcat configure/java

maximum memory pool=1024
and in java option
-XX:PermSize=128m
-XX:MaxPermSize=512m


Otherwise Go to Catalina.sh and paste the following code

JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m
-Xmx1536m -XX:NewSize=512m -XX:MaxNewSize=1024m -XX:PermSize=512m
-XX:MaxPermSize=1024m -XX:+DisableExplicitGC"





Spring 3 MVC File Upload

In dispatcher-servlet.xml add :-------

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

Add this Code to the Controller Class :-------------------

MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 
MultipartFile multipartFile = multipartRequest.getFile("file"); 
String fileName="";
               
        // image type of file processing...
 System.err.println("-------------------------------------------");
        try {
                InputStream inputStream = null;
                OutputStream outputStream = null;
                if (multipartFile.getSize() > 0) {
        inputStream = multipartFile.getInputStream();
        fileName = request.getRealPath("") + "/images/"+ multipartFile.getOriginalFilename();         
        outputStream = new FileOutputStream(fileName);
        int readBytes = 0;
        byte[] buffer = new byte[10000000];
        while ((readBytes = inputStream.read(buffer, 0, 10000000)) != -1) {
             outputStream.write(buffer, 0, readBytes);
        }
        outputStream.close();
        inputStream.close();
        }       
        }catch (Exception e) {
        e.printStackTrace();
        }

Sunday, 12 August 2012

JavaMail API – Sending email via Gmail SMTP

import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Multipart;

public class SendMailSSL {
    public static void main(String[] args) {
            
        Properties props = new Properties();
                props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.user", "abc@gmail.com");
                props.put("mail.smtp.password", "password");

        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "587");
                
                String[] to = {"xxxx@yahoo.com"}; // added this line

                
        Session session = Session.getDefaultInstance(props,null);
                MimeMessage message = new MimeMessage(session);
                try{
                message.setFrom(new InternetAddress("abc@gmail.com"));
                }catch(Exception e){}
                InternetAddress[] toAddress = new InternetAddress[to.length];
                
                for( int i=0; i < to.length; i++ ) { // changed from a while loop
                    try{
                toAddress[i] = new InternetAddress(to[i]);
                    }catch(Exception e){}
                }System.out.println(Message.RecipientType.TO);
                for( int i=0; i < toAddress.length; i++) { // changed from a while loop
                   try{
        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
                }catch(Exception e){}
                }
               try{
    message.setSubject("sending from gmail");
    message.setText("Hello Its me");
    
     String bodyText = "This is a important message with attachment";
     String filename = "D:/new.txt";
     MimeBodyPart messagePart = new MimeBodyPart();
            messagePart.setText(bodyText);

            //
            // Set the email attachment file
            //
            MimeBodyPart attachmentPart = new MimeBodyPart();
            FileDataSource fileDataSource = new FileDataSource(filename) {
              
               public String getContentType() {
                    return "application/octet-stream";
               }
           };
          attachmentPart.setDataHandler(new DataHandler(fileDataSource));
           attachmentPart.setFileName(filename);

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messagePart);
            multipart.addBodyPart(attachmentPart);

            message.setContent(multipart);
    
    
    Transport transport = session.getTransport("smtp");
    transport.connect("smtp.gmail.com", "abc@gmail.com", "password");
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
    
                }catch(Exception e){}
        System.out.println("Done");
    }
}