import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFileChooser;
import oracle.jdbc.driver.OracleConnection;
public class Read
{
public static void main(String[] args)
{
try {
Read rfdb = new Read ();
rfdb.readFromDB();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Connection conn;
Statement stmt;
ResultSet rs;
int bufferSize;
public Read ()throws SQLException, ClassNotFoundException
{
String connectString;
Class.forName("oracle.jdbc.driver.OracleDriver");
connectString = "jdbc:oracle:thin:@192.168.1.134:1521:zhpt";
conn = (OracleConnection)
DriverManager.getConnection(connectString, "dzjc","dzjc");
stmt = conn.createStatement();
}
public void readFromDB()
{
String pathname, name;
int amount = 0;
InputStream in = null;
BufferedOutputStream out = null;
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
pathname = chooser.getSelectedFile().getAbsolutePath();
name = chooser.getSelectedFile().getName();
chooser = null;
}
else
{
System.out.println("没有找到文件!");
System.out.println("程序非正常结束...");
return;
}
try
{
rs = stmt.executeQuery("select img from dzjc_img");
if (rs.next())
{
Blob blob = rs.getBlob(1);
in = blob.getBinaryStream();
bufferSize = ( (oracle.sql.BLOB) blob).getBufferSize();
out = new BufferedOutputStream(new FileOutputStream(pathname),(int) bufferSize);
byte[] b = new byte[ (int) bufferSize];
int count = in.read(b, 0, (int) bufferSize);
while (count != -1)
{
out.write(b, 0, count);
amount += count;
System.out.println("已经处理了" + amount + "字节.");
count = in.read(b, 0, (int) bufferSize);
}
System.out.println("成功处理完毕字节数:" + amount);
out.close();
out = null;
in.close();
in = null;
}
else
{
System.out.println("文档:" + name + "未找到.");
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (out != null)
{
try
{
out.close();
}
catch (Exception ignored) {}
}
if (in != null)
{
try
{
in.close();
}
catch (Exception ignored) {}
}
}
}
}
上篇文章是从硬盘上存到数据库里,这次来个存到数据库里再取出来的存到硬盘上的类,还有一个是,下篇文章再发一个从数据库里取出来再显示在浏览器上的
贴代码先
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFileChooser;
import oracle.jdbc.driver.OracleConnection;
public class Read
{
public static void main(String[] args)
{
try {
Read rfdb = new Read ();
rfdb.readFromDB();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
Connection conn;
Statement stmt;
ResultSet rs;
int bufferSize;
public Read ()throws SQLException, ClassNotFoundException
{
String connectString;
Class.forName("oracle.jdbc.driver.OracleDriver");
connectString = "jdbc:oracle:thin:@192.168.1.134:1521:zhpt";
conn = (OracleConnection)
DriverManager.getConnection(connectString, "dzjc","dzjc");
stmt = conn.createStatement();
}
public void readFromDB()
{
String pathname, name;
int amount = 0;
InputStream in = null;
BufferedOutputStream out = null;
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
pathname = chooser.getSelectedFile().getAbsolutePath();
name = chooser.getSelectedFile().getName();
chooser = null;
}
else
{
System.out.println("没有找到文件!");
System.out.println("程序非正常结束...");
return;
}
try
{
rs = stmt.executeQuery("select img from dzjc_img");
if (rs.next())
{
Blob blob = rs.getBlob(1);
in = blob.getBinaryStream();
bufferSize = ( (oracle.sql.BLOB) blob).getBufferSize();
out = new BufferedOutputStream(new FileOutputStream(pathname),(int) bufferSize);
byte[] b = new byte[ (int) bufferSize];
int count = in.read(b, 0, (int) bufferSize);
while (count != -1)
{
out.write(b, 0, count);
amount += count;
System.out.println("已经处理了" + amount + "字节.");
count = in.read(b, 0, (int) bufferSize);
}
System.out.println("成功处理完毕字节数:" + amount);
out.close();
out = null;
in.close();
in = null;
}
else
{
System.out.println("文档:" + name + "未找到.");
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (out != null)
{
try
{
out.close();
}
catch (Exception ignored) {}
}
if (in != null)
{
try
{
in.close();
}
catch (Exception ignored) {}
}
}
}
}
要保证数据库里有图片,打开的对话框显示为打开而不是保存,没关系,自己找个地方,输个文件名,一点打开,图片就会被保存在那个位置了