最近一直在整理struts2处理与数据库图片存取相关的code,网上也看了不少,结果总是差那么一点点(哈哈),没办法,只有手写个最原始的jdbc到Oracle存取的例子,没想到还有不少收获,顺便将自己的struts2的错误处理也贴上来,存储下。
javaeye的版主们看好了,我这可是存储啊!别再给我评什么茶贴还是什么里。
struts2存储
struts2读取
javaeye的版主们看好了,我这可是存储啊!别再给我评什么茶贴还是什么里。
package com.core.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.OracleResultSet;
import oracle.sql.BLOB;
import com.common.bean.Personnel;
public class TestSQl {
/**
* @param args
*/
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String user = "xx";
String password = "111";
Connection con = DriverManager.getConnection(url, user, password);
Statement stmt = con.createStatement();
String sql = "";
con.setAutoCommit(false);
//***********************IO****************************
File file = new File("C:\\image\\haha.gif") ;
InputStream is = new FileInputStream(file);
ByteArrayOutputStream os = new ByteArrayOutputStream();
FileOutputStream fos = new FileOutputStream("C:\\image\\haha2.gif");
byte[] buffer = new byte[is.available()];
int b = 0;
while((b = is.read(buffer)) != -1){
os.write(buffer);
if(b > 1024) {
System.out.println(b);
}
}
byte[] photo = os.toByteArray();
System.out.println(photo.length);
ByteArrayInputStream bis = new ByteArrayInputStream(photo);
while(( b = bis.read(buffer)) != -1) {
fos.write(buffer);
}
//************************INSERT*******************************
//插入一条空记录
// sql = "insert into personnel(staffcode,staffname,password,deptcode) values('000115','xxoo','aaa','0001')";
// stmt.execute(sql);
// con.commit();
//***********************SQL*****************************
sql = "select * from personnel per where per.staffcode = '000114' for update";//设置更新,需要con.setAutoCommit(false)
sql = "select * from personnel per where per.staffcode = '000101'";
// sql = "select * from personnel " ;
ResultSet rs = stmt.executeQuery(sql);
List<Personnel> personnellist = new ArrayList<Personnel>();
while(rs.next()){
Personnel personnel = new Personnel();
personnel.setStaffcode(rs.getString("staffcode"));
personnel.setStaffname(rs.getString("staffname"));
personnel.setPhoto(rs.getBytes("photo"));
/*for update 将图片写入数据库*/
// BLOB blob = ((OracleResultSet)rs).getBLOB("photo");
// OutputStream out = blob.getBinaryOutputStream();
// InputStream in = new FileInputStream(new File("C:\\image\\haha2.gif"));
// int size = blob.getBufferSize();
// byte [] buf = new byte[size];
// int count = 0;
// while((count = in.read(buf)) != -1) {
// out.write(buf, 0, count);
// }
// in.close();
// out.close();
/*从数据库读取图片*/
BLOB blob2 = ((OracleResultSet)rs).getBLOB("photo");
InputStream in2 = blob2.getBinaryStream();
OutputStream out2 = new FileOutputStream(new File("C:\\image\\haha5.jpg"));
int size2 = blob2.getBufferSize();
byte [] buf2 = new byte[size2];
int count2 = 0;
while((count2 = in2.read(buf2)) != -1) {
out2.write(buf2, 0, count2);
}
in2.close();
out2.close();
personnellist.add(personnel);
}
con.commit();
for(Personnel pp : personnellist){
//注意pp.getPhoto().length
System.out.println(pp.getStaffcode() + ":: " + pp.getStaffname() + " :: " + pp.getPhoto().length );
}
is.close();
os.close();
bis.close();
fos.close();
con.close();
buffer = null;
photo = null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
struts2存储
FileInputStream fis = null;
// OutputStream os = null;
try {
fis = new FileInputStream(file);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
// os = new ByteArrayOutputStream();
// int b;
// while(( b = fis.read()) != -1) {
// os.write(b);
// }
//
System.out.println(buffer.length);
personnel.setPhoto(buffer);
personnelService.addPersonnel(personnel);
// fis.read(personnel.getPhoto());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if( fis != null ) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
struts2读取
HttpServletResponse response = null;
ServletOutputStream out = null;
InputStream in = null;
byte [] bytes = null;
personnel = personnelService.findPersonnel(staffcode);
try {
response = ServletActionContext.getResponse();
response.setContentType("image/jpeg");
out = response.getOutputStream();
in = new ByteArrayInputStream(personnel.getPhoto());
bytes = new byte [ in.available() ];
while ( -1 != in.read( bytes ) ) {
out.write( bytes );
}
out.flush();
// out.close();
// out = null;
// response.flushBuffer();
// out.clear();
// out = pageContext.pushBody();
} catch (IOException e) {
e.printStackTrace();
} catch ( Exception e ) {
e.printStackTrace();
}
finally {
if ( in != null ) {
try {
in.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
if ( out != null ) {
try {
out.close();
} catch ( IOException e ) {
e.printStackTrace();
}
}
bytes = null;
}