4/30/09

File Zip using JAVA

pass your file names in an arry to the method.

---------------------------------------------------


public static void zip(String [] args) throws FileNotFoundException, IOException
{

String outputFile = "new.zip";

int level = 9;
int start = 1;

FileOutputStream fout = new FileOutputStream(outputFile);
ZipOutputStream zout = new ZipOutputStream(fout);

zout.setLevel(level);

for (int i = start; i < args.length; i++)
{

ZipEntry ze = new ZipEntry(args[i]);
FileInputStream fin = new FileInputStream(args[i]);
try {

System.out.println("Compressing " + args[i]);
zout.putNextEntry(ze);

for (int c = fin.read(); c != -1; c = fin.read())
{
zout.write(c);
}

}
finally{
fin.close();
}
}
zout.close();
}

Type 4 Connection in MySQL

Connection Class for My SQL ( With Type 4 Connection )
---------------------------------------------------------


public class Conn {

java.sql.Connection con;

public Conn()
{
try{


Class.forName("com.mysql.jdbc.Driver");

String connectionUrl="jdbc:mysql://localhost/[database name]?user=root&password=123";

con=java.sql.DriverManager.getConnection(connectionUrl);

}
catch(Exception ex){javax.swing.JOptionPane.showMessageDialog(null, ex.toString());}
}

Getting PDF Info using iText.jar

String path="Your PDF Path"; // ----------> like C:\Books\Hello.pdf
String BookTitle = " ";
String Author = " ";
String Description = " ";
String Publisher = " ";
int TotalPage=0;


PdfReader reader=null;
reader = new PdfReader( path);

HashMap map = null;

map = new HashMap();
map = reader.getInfo();
int nums = reader.getNumberOfPages();



if (map.containsKey("Title")) {
BookTitle = map.get("Title").toString().trim();
}

if (map.containsKey("Author")) {
Author = map.get("Author");
}
if (map.containsKey("Subject")) {
Description = map.get("Subject");
}

TotalPage = nums;

if (map.containsKey("Producer")) {
Publisher = map.get("Producer");
}

Getting PDF page as Pic Using JPedal.jar

PdfDecoder ff = new PdfDecoder(true);
ff.openPdfFile( path );

BufferedImage img = ff.getPageAsImage(1); -------------> Number of page

ImageIO.write(img, "png", new File("D:/PDF/" + File Name+ ".png"));
ff.closePdfFile();

JProgressbar

bar is the object of javax.swing.JProgressbar
---------------------------------------------


bar.setMinimum(0);
bar.setMaximum( int size ); ---------> Your Loop size.
bar.setValue(0);

your loop starts {


bar.setValue(bar.getValue()+1);
bar.setStringPainted(true);

Rectangle progressRect = bar.getBounds();
progressRect.x = 0;
progressRect.y = 0;
bar.paintImmediately( progressRect );


}
your loop ends.

Get Page Content Dynamically using url Class

java.net.URL url=new java.net.URL("http://www.mapsofworld.com/lat_long
/"+cntry+"-lat-long.html");

java.net.HttpURLConnection con=(java.net.HttpURLConnection)url.openConnection();

con.connect();

java.io.InputStream in=con.getInputStream();


java.io.BufferedReader br=new java.io.BufferedReader(new
java.io.InputStreamReader(in));


String temp="";

while((temp=br.readLine())!=null)
{
cont+=temp+"\n";
}

System.out.println(cont);

Set Swing Window Look & Feel

UIManager.setLookAndFeel(new WindowsLookAndFeel());

Ajax code for alling a servlets

var req;

function toServlet()
{



url = "Test?hr="+document.getElementById('hr').value+"&min="+document.getElementById('min').value;



if(window.XMLHttpRequest) //non IE
{

req = new XMLHttpRequest();


try
{
req.open("GET",url,true);
req.onreadystatechange=modify;
}
catch(e)
{
alert("cannot connect to server");
}

req.send(null);
}

else if(window.ActiveXobject) //IE
{
req=new ActiveXobject("MicrosoftXMLHTTP");

if(req)
{
req.open("GET",url,true);
req.onreadystatechange=modify;
req.send(null);

}
}


}



the result must be write to out.println(s); in servlet

Receiving the response text from servlet.




function modify_InnerHtml()
{
if(req.readyState == 4)
{
if(req.status==200)
{

var res=req.responseText;


var value="
"+res+"
";

document.getElementById("lin").innerHTML=value;
}
}
}

Link For Loading Animation

http://www.ajaxload.info/

Javascript LightBox CSS Menus Links

http://www.1stwebdesigner.com/resources/57-free-image-gallery-slideshow-and-lightbox-solutions/


http://www.cssdrive.com/index.php/menudesigns/category/C20/P10/


http://www.dynamicdrive.com/dynamicindex4/simplegallery.htm

Javascript Code for Browser Dedection

var s;

var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);

var is_nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav6 = (is_nav && (is_major == 5));
var is_nav6up = (is_nav && (is_major >= 5));
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));


function x()
{
alert(is_ie);
}