Sunday, January 30, 2011

Send SMS using Java

In most of the forums i found this question.How to send SMS Using java.
For answering this question i spent few hours for you guys.


For sending SMS you need to have the following files
1) javax.comm.properties
2) comm.jar
3) win32com.dll
In this Link you can download the files.Let me know if the given link is broken.So that i will upload the files and change the link here.

Check the java_home twice before copy and paste the files.Because most of the problems occurred by pasting the files in wrong folder. 
javax.comm.properties:
Should falls under
   %JAVA_HOME%/lib
   %JAVA_HOME%/jre/lib
win32com.dll:
Should falls under
    %JAVA_HOME%/bin
    %JAVA_HOME%/jre/bin
    %windir%System32
comm.jar:
 Should falls under
     %JAVA_HOME%/lib
     %JAVA_HOME%/jre/lib/ext

Okay i believe you placed all files in respective folder correctly.
Wait guys i heard your mind voice,you are asking where is the java program.
How i will forget,here you go for java programs

import java.io.IOException;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;
public class SriramSMS {
public void sendsms()   
{  
 static Enumeration portList;
 static CommPortIdentifier portId;
 static SerialPort serialPort;
 static OutputStream outputStream;
 String messageString = "";
 String userName="";
 String phoneNumber="";
 Thread thread;

  try
  {
     userName = "periodicUpdates.blogspot.com";
     phoneNumber = "9876543210";
     messageString = " Hi." +userName+"welcome to this blog" ;

            String line1 = "AT+CMGF=1\r\n";
     String line2 = "AT+CMGS=" + phoneNumber + "\r\n"+messageString+ "\r\n";
     String line3 = "\u001A";

    // Here initialize the driver class
                  String driverName = "com.sun.comm.Win32Driver";
    CommDriver commdriver = (CommDriver)Class.forName(driverName).newInstance();
    commdriver.initialize();

    portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements())
     {       
      portId = (CommPortIdentifier) portList.nextElement();
      if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL)   {
      System.out.println("SMS Sending........" + portId.getName());
      if((portId.getName().equals("COM1"))) {
       try
        {
         serialPort = (SerialPort) portId.open("SimpleWriteApp",10000);
         System.out.println("sms sending port--->"+serialPort);
         outputStream = serialPort.getOutputStream();
         serialPort.setSerialPortParams(230400,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
         outputStream.write(line1.getBytes());
         outputStream.write(line2.getBytes());
         outputStream.write(line3.getBytes());
         outputStream.flush();
         thread.sleep(5000);
         serialPort.close();
        } catch (PortInUseException portUse) {
         System.out.println("Port In Use " + portUse.toString());
        } catch (UnsupportedCommOperationException unsuppComOperExcep) {
         System.out.println("UnsupportedCommOperationException occured:::"+unsuppComOperExcep.toString());
        }  catch (IOException ioExcep) {
         System.out.println("Error occured while writing to output stream IOException" + ioExcep.toString());
        } catch(Exception excep) {
         System.out.println("Error writing message  with exception while closing " +excep.toString());
        }
     }
    }
   }
  } catch(Exception excep) {
   System.out.println("EXCEPTION raised while writing message@@" +excep.toString());
  }
 }
}
You got the program for sending sms through java program.

Want to Send Mail Using java program then click this Link and for more java program click this Link.
Do post your comments.

Saturday, January 29, 2011

Interview Questions

Like you guys i also surfed and prepared for the interview.Few months before got selected in one MNC Company.I can help you guys by providing set of interview questions.

By clicking this Link you can download the interview questions in rar file format.

why you are waiting just download the files and rock the interview.

All the best guys...

Thursday, January 27, 2011

Struts2 Select Box Error.

I believe you faced this error thats why you are here for the solution.
Instead of wasting our time straightly we will go to the solution.

struts.xml

  /jsp/selectBox.jsp  


SelectList.java
package com.demo.actions;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
public class SelectList extends ActionSupport{
private ArrayList dayList;
public String execute() throws Exception {
//instantiate the Arraylist here
dayList = new ArrayList();
dayList.add("monday");
dayList.add("tuesday");
...
dayList.add("sunday");
return SUCCESS;
}
//Make sure you have created the getter and setter method for ArrayList
public ArrayList getDayList() {
  return dayList;
 }
public void setDayList(ArrayList dayList) {
  this.dayList= dayList;
 }
}
selectBox.jsp
Thats it your code will work like a charm.
Do post your comments and for more java programming click this Link.

Wednesday, January 26, 2011

Maintain Image Aspect Ratio Using Java

In Internet i have searched more about maintaining the aspect ratio of the image using java program.After a long struggle found one example and i changed according to met my requirement.Here you go for the complete solution.
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

class imageAspectRatio {
  public static void main(String args[]) {
  File file = new File("path");//Specify the input image source file location.
    
  try {
   FileInputStream fis = new FileInputStream(file);
   InputStream bis = new BufferedInputStream(fis);
   FileOutputStream fos = null;
   
   Image image = (Image)ImageIO.read(bis);
   
   int thumbWidth = 100;//Specify image width in px
   int thumbHeight = 100;//Specify image height in px
   
   int imageWidth = image.getWidth(null);//get image Widht
   int imageHeight = image.getHeight(null);//get image Height 
   
   double thumbRatio = (double)thumbWidth/(double)thumbHeight;
   double imageRatio = (double)imageWidth/(double)imageHeight;
   
//This calculation is used to convert the image size according to the pixels mentioned above
   if(thumbRatio<imageRatio) {
    thumbHeight = (int) (thumbWidth/imageRatio);
   } else {
    thumbWidth = (int) (thumbHeight*imageRatio);
   }
   
   BufferedImage thumbImage = 
       new BufferedImage(thumbWidth,thumbHeight,BufferedImage.TYPE_INT_RGB);
   
   Graphics2D graphics = thumbImage.createGraphics();
   graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
   graphics.drawImage(image,0, 0, thumbWidth, thumbHeight,null);
   
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   JPEGEncodeParam param  = encoder.getDefaultJPEGEncodeParam(thumbImage);
   
   int quality = 100;
   quality = Math.max(0,Math.min(quality,100));
   param.setQuality((float)quality/100.0f,false);
   
//output image type.
   String format = "jpg";
   
   encoder.setJPEGEncodeParam(param);
   encoder.encode(thumbImage);
   ImageIO.write(thumbImage,format, new File("path"));//Specify the location for output image .
    } catch(IOException ioExcep) {
   ioExcep.printStackTrace();
  }catch(Exception excep) {
   excep.printStackTrace();
  }
  }
}

Run this program you will get the expected result.
Do post your comments and for more java programming click this Link.