Wednesday, April 27, 2011

Download Books from google book(tested/working)

This is the most powerful and stable way to download Google Book. You can easily download any book from books.google.com using Greasemonkey script. Just follow the simple steps below.


1 This hack only works with firefox browser. Make sure you install firefox browser.

2 Now install Greasemonkey Script
3.restart firefox
3 then install Google book downloader userscript.

4 Install Flashgot to firefox browser
5. restart your firefox browser.
6 Search any book on books.google.com and you’ll notice a download button at the sidebar as shown in screenshot.
7. Click the download button to download the images of each. Select the pages you wish to download and then right click and select FlashGot Selection to download the selected pages.
8. use igame to pdf converter to convert all into single pdf file

9.Njoy..

Sending Automated email in JSP/Java

This summary is not available. Please click here to view the post.

Wednesday, April 20, 2011

Upload files using jsp


<%@ page import="java.io.*" %>
<%
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength)
{
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
//out.print("FileName:" + saveFile.toString());
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.print("FileName:" + saveFile.toString());
//out.print(dataBytes);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
//out.println(boundary);
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
saveFile = "D:\\java\tomcat5\\webapps\\ROOT\\images\\" + saveFile;
FileOutputStream fileOut = new FileOutputStream(saveFile);
//fileOut.write(dataBytes);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
out.println("File saved as " +saveFile);
}
%>
















Select a file to upload :










Read and write file in jsp

<%@ page import="java.io.*" %>


Read write file JSP



<%
String fileName=getServletContext().getRealPath("jsp.txt");

File f=new File(fileName);

InputStream in = new FileInputStream(f);

BufferedInputStream bin = new BufferedInputStream(in);

DataInputStream din = new DataInputStream(bin);
StringBuffer sb=new StringBuffer();
while(din.available()>0)
{
sb.append(din.readLine());
}

try {
PrintWriter pw = new PrintWriter(new FileOutputStream("c:/file.txt"));// save file
pw.println(sb.toString());
pw.close();
} catch(IOException e) {
e.getMessage();
}

in.close();
bin.close();
din.close();
%>
Successfully write file

Tuesday, April 19, 2011

sqlite.JDBC Sample code..

//run by
//java -classpath ".;sqlite-jdbc-3.5.7.jar" Sample

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


class Test
{

String path="jdbc:sqlite:C:/Documents and Settings/Dipankar/Desktop/Dropbox/mq/sample.db";

//=======================logic strat from here add user..===================[DONE]

public void adduser(String username,String filename,String content)
{

// load the sqlite-JDBC driver using the current class loader
Connection connection = null;



//auto calculete id
int uid=0;
try{
connection = DriverManager.getConnection(path);
Statement statement1 = connection.createStatement();
ResultSet rs1=statement1.executeQuery("select count(*) from userinfo");
uid=rs1.getInt(1)+1;
}
catch(Exception e)
{
System.out.println("Error here");
}
finally
{
try
{
if(connection != null)
connection.close();
}

catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}
System.out.println("insert into userinfo values("+uid+",'"+username+"','"+filename+"','"+content+"')");









try
{

connection = DriverManager.getConnection(path);
Statement statement = connection.createStatement();

statement.executeUpdate("insert into userinfo values("+uid+",'"+username+"','"+filename+"','"+content+"')");
}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}

catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}



}
//=======================END==================================================================





//=======================logic strat from here view user..===================[DONE]
public void showinfo(int userid)
{



// load the sqlite-JDBC driver using the current class loader
Connection connection = null;


try
{

connection = DriverManager.getConnection(path);
Statement statement = connection.createStatement();

ResultSet rs = statement.executeQuery("select * from userinfo where userid="+userid);
while(rs.next())
{
// read the result set
System.out.println("id = " + rs.getInt("userid"));
System.out.println("name = " + rs.getString("username"));
System.out.println("finemane = " + rs.getString("filename"));
System.out.println("file conetct = " + rs.getString("filecontent"));

}

}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}

catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}



}
//=======================END===================================================================


//=======================logic strat from here view user..===================[DONE]
public void removeuser(int userid)
{

// load the sqlite-JDBC driver using the current class loader
Connection connection = null;


try
{

connection = DriverManager.getConnection(path);
Statement statement = connection.createStatement();

statement.executeUpdate("delete from userinfo where userid="+userid);

}
catch(SQLException e)
{
// if the error message is "out of memory",
// it probably means no database file is found
System.err.println(e.getMessage());
}
finally
{
try
{
if(connection != null)
connection.close();
}

catch(SQLException e)
{
// connection close failed.
System.err.println(e);
}
}

}

//=======================END===================================================================


}

public class TestDB
{


public static void main(String[] args) throws ClassNotFoundException
{
// load the sqlite-JDBC driver using the current class loader
Class.forName("org.sqlite.JDBC");

Test x= new Test();
x.showinfo(0);
}
}

Monday, April 18, 2011

Java Fille I/0

import java.io.*;

public class FileReader {
public static void main (String[] args) {
System.out.println ("Program to demonstrate reading from a file");
BufferedReader br = null;
String fileName = args[0];
// If an error occurs, go to the "catch" block
try {
// FileInputStream fis = new FileInputStream (fileName);
FileReader fis = new FileReader (fileName);
br = new BufferedReader (fis);
// continue to read lines while there are still some left to read
while ( br.ready() ) {
System.out.println (br.readLine());
}
// Close the input stream
br.close();
} catch (Exception e) {
// Handle any error in opening the file
System.err.println("File input error");
}
} // End of main method
} // End of the class FileReader