jsp把图片以二进制形式写入oracle
平台 tomcat,oracle
需要实现的是在提交表单的同时把图片以二进制的形式存入数据库里的一个表。
存取图片的表是单独建立,通过一个id和主表关联。
关于bean和页面如何来做,请大家指导下~~~~非常感谢。
[解决办法]
application的贴一个完整的出来,希望对lz有所帮助。
偶也复习一下,jdbc不能生疏了,呵呵
import java.io.*;import java.sql.*;import javax.swing.*;/*-- 测试表 环境:oracle 9i下的create table TestImage( img_id int primary key, img_content blob)*/ public class BlobTestForOracle { private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver"; private static final String DB_URL = "jdbc:oracle:thin:@localhost:1521:oratest"; private static final String DB_USER = "test"; private static final String DB_PASSWORD = "password"; public BlobTestForOracle(){ try { Class.forName(DB_DRIVER); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 另村为... * * */ public String chooseFileSave(String title,String fileName){ JFileChooser chooser = new JFileChooser(); chooser.setSelectedFile(new File(fileName)); chooser.setDialogTitle(title); int rnt = chooser.showSaveDialog(null); if(rnt == JFileChooser.APPROVE_OPTION){ return chooser.getSelectedFile().getAbsolutePath(); }else{ return ""; } } /** * 打开... * * */ public String chooseFileOpen(String title,String fileName){ JFileChooser chooser = new JFileChooser(); chooser.setSelectedFile(new File(fileName)); chooser.setDialogTitle(title); int rnt = chooser.showOpenDialog(null); if(rnt == JFileChooser.APPROVE_OPTION){ return chooser.getSelectedFile().getAbsolutePath(); }else{ return ""; } } /** * 写入blob * */ public void writeFileToDatabaseByRid(String fileName,int rid){ BufferedInputStream bis = null; Connection conn = null; PreparedStatement pstmt = null; String strSQL = null; File file = null; int size = 0; try{ strSQL = "INSERT INTO TestImage(img_id,img_content) VALUES(?,?)"; conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD); pstmt = conn.prepareStatement(strSQL); fileName = this.chooseFileOpen("请选择一个文件",fileName); file = new File(fileName); if(! file.exists()){ System.out.println(file.getAbsolutePath()); System.out.println("文件貌似不存在!"); return; } else if(file.length() > 50 * 1024 * 1024){ // 限制一下文件大小 System.out.println("文件是不是太大鸟? " + file.length()); return; } size = (int)file.length(); bis = new BufferedInputStream(new FileInputStream(file)); pstmt.setInt(1, rid); pstmt.setBinaryStream(2,bis,size); int effect = pstmt.executeUpdate(); if(effect == 1){ System.out.println("写入ok!" + file.getAbsolutePath()); } else{ System.out.println("写入failed!"); } bis.close(); }catch(IOException ioe){ ioe.printStackTrace(); }catch(SQLException sqle){ sqle.printStackTrace(); }finally{ if(pstmt != null){ try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } } /** * 从blob读取 * */ public void getFileFromDatabaseByRid(String fileName,int rid){ String strSQL = null; Connection conn = null; PreparedStatement pstmt = null; ResultSet rst = null; int size = 0; File file = null; try{ strSQL = "SELECT * FROM TestImage WHERE img_id = ?"; conn = DriverManager.getConnection(DB_URL,DB_USER,DB_PASSWORD); pstmt = conn.prepareStatement(strSQL); pstmt.setInt(1, rid); rst = pstmt.executeQuery(); if(rst.next()){ fileName = this.chooseFileSave("存储为...", fileName); file = new File(fileName); if("".equals(file.getName().trim())){ System.out.println("failed!"); return; } OutputStream os = new BufferedOutputStream(new FileOutputStream(file)); BufferedInputStream bis = new BufferedInputStream(rst.getBinaryStream("img_content")); byte[] b = new byte[10 * 1024]; // 这里是设置了缓存 byte[] b 设置为 10K int len = 0; // 返回read一次实际读取的字节数 while((len = bis.read(b)) != -1){ os.write(b,0,len); size += len; } os.flush(); os.close(); bis.close(); System.out.println("保存文件完毕! " + size + " Byte(s)"); } else{ System.out.println("id : " + rid + " 文件没有找到"); } }catch(IOException ioe){ ioe.printStackTrace(); } catch(SQLException sqle){ sqle.printStackTrace(); }finally{ if(rst != null){ try { rst.close(); } catch (SQLException e) { e.printStackTrace(); } } if(pstmt != null){ try { pstmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if(conn != null){ try{ conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } } public static void main(String[] args){ // test BlobTestForOracle test = new BlobTestForOracle(); test.writeFileToDatabaseByRid("C:/a.jpg",123); test.getFileFromDatabaseByRid("C:/a.jpg", 123); }}