给你一个例子,我先放到一个指定的目录下面,然后读这个文件。
public String getPhoto(String sID) throws CCustomException
{
try
{
CSqlQuery2 sqQuery = new CSqlQuery2("kkk");
DataSource source = sqQuery.getDataSource();
Connection conn = CDataSourceUtil.GetConnection(source); Blob blob = null;
Statement select = conn.createStatement();
String sSQL = "select Photo from Photo where ExamNo = '" + sID + "'";
Statement pstmt = conn.createStatement();
ResultSet rs = pstmt.executeQuery(sSQL);
String sFileName ="";
if(rs.next())
{
//读出流用getBinaryStream()方法。
InputStream inStream = rs.getBinaryStream(1);
//图片的有效长度。
int size = inStream.available();
byte[] pictureData = new byte[size];
//read(byte[] b):读取输入流的数据到指定的数组。只是文件的有效部分。
inStream.read(pictureData);
//将图片文件输出到指定目录。
File f = File.createTempFile("kkk",".jpg",new File(System.getProperty("java.io.tmpdir")));
sFileName = f.getAbsolutePath();
FileOutputStream fileOutStream = new FileOutputStream(f.getAbsolutePath());
//写入数据。
fileOutStream.write(pictureData);
fileOutStream.close();
}
rs.close();
pstmt.close();
return sFileName;
}
catch(CCustomException e)
{
throw e;
}
catch(Exception e)
{
throw new CCustomException("","操作文件时出错!",e.getMessage());
}
}
public String getPhoto(String sID) throws CCustomException
{
try
{
CSqlQuery2 sqQuery = new CSqlQuery2("kkk");
DataSource source = sqQuery.getDataSource();
Connection conn = CDataSourceUtil.GetConnection(source); Blob blob = null;
Statement select = conn.createStatement();
String sSQL = "select Photo from Photo where ExamNo = '" + sID + "'";
Statement pstmt = conn.createStatement();
ResultSet rs = pstmt.executeQuery(sSQL);
String sFileName ="";
if(rs.next())
{
//读出流用getBinaryStream()方法。
InputStream inStream = rs.getBinaryStream(1);
//图片的有效长度。
int size = inStream.available();
byte[] pictureData = new byte[size];
//read(byte[] b):读取输入流的数据到指定的数组。只是文件的有效部分。
inStream.read(pictureData);
//将图片文件输出到指定目录。
File f = File.createTempFile("kkk",".jpg",new File(System.getProperty("java.io.tmpdir")));
sFileName = f.getAbsolutePath();
FileOutputStream fileOutStream = new FileOutputStream(f.getAbsolutePath());
//写入数据。
fileOutStream.write(pictureData);
fileOutStream.close();
}
rs.close();
pstmt.close();
return sFileName;
}
catch(CCustomException e)
{
throw e;
}
catch(Exception e)
{
throw new CCustomException("","操作文件时出错!",e.getMessage());
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货