This is the code for uplodaing and retreving gis and jpgs ..
if u want text files just replce
res.setContentType()/whatever u want either text/plain or text/application..
import java.io.*;
import java.sql.*;
/* If you are compiling and executing file ImageJDBC.java in c:\
* the file rao.gif should exist in c:\ (same directory)...
*/
public class ImageJDBC {
public static void main(
String args []) throws SQLException, ClassNotFoundException,Exception {
try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
String url = "jdbc
racle:thin:@rao:1521:amica";
Connection conn = DriverManager.getConnection(url,"mail","mail");
// method to insert Image into database
ImageJDBC obj = new ImageJDBC();
obj.insertImage(conn);
// method to retreive Image from database
obj.getImage(conn);
}catch(Exception e){
e.printStackTrace();
}
}
public void insertImage(Connection conn){
try {
Statement stmt = conn.createStatement ();
// execute CREATE for webimages table
boolean flag = stmt.execute("CREATE TABLE webimages (img_ident VARCHAR2 (64), img_fmt CHAR (5), img_data LONG RAW)");
// Insert data into the img_data column
File file = new File ("rao.gif");
InputStream istr = new FileInputStream ("rao.gif");
// prepare the INSERT into webimages using parameters
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO webimages (img_ident, img_fmt, img_data) VALUES (?, ?, ?)");
pstmt.setString (1, "Rao Logo");
pstmt.setString (2, "GIF");
pstmt.setBinaryStream (3, istr, (int)file.length ());
pstmt.execute ();
}catch(Exception e){
e.printStackTrace();
}
}
public void getImage(Connection conn){
try {
Statement stmt = conn.createStatement ();
// retrieve image from row when the ID is 'Rao Logo'
ResultSet rs = stmt.executeQuery ("SELECT img_data FROM webimages WHERE img_ident='Rao Logo'");
// process the ResultSet data
if (rs.next ()) {
// get binary stream data from result set
InputStream web_image = rs.getBinaryStream(1);
// open operating system file for storing the image
FileOutputStream ostr = new FileOutputStream("backlogo.gif");
// fetch-write loop, fetch image data from rs, write to file
int i;
while ((i = web_image.read()) != -1) {
ostr.write (i);
}
ostr.close ();
}
}catch(Exception e) {
e.printStackTrace();
}
}
}