1 package com.quadcap.app.dbimage; 2 3 40 41 import java.io.ByteArrayOutputStream ; 42 import java.io.File ; 43 import java.io.FileInputStream ; 44 import java.io.IOException ; 45 import java.io.InputStream ; 46 import java.io.OutputStream ; 47 48 import java.util.Enumeration ; 49 import java.util.Properties ; 50 51 import java.sql.Connection ; 52 import java.sql.PreparedStatement ; 53 import java.sql.ResultSet ; 54 import java.sql.Statement ; 55 import java.sql.SQLException ; 56 57 62 public class DbImageLoader { 63 64 public DbImageLoader() { 65 } 66 67 public void loadImages(Connection conn, String root) throws Exception { 68 PreparedStatement pstmt = conn.prepareStatement( 69 "insert into images values(?,-1,?,?)"); 70 File f = new File (root); 71 try { 72 loadImages(pstmt, "", f); 73 } finally { 74 pstmt.close(); 75 } 76 } 77 78 final void loadImages(PreparedStatement pstmt, String path, 79 File dir) 80 throws Exception 81 { 82 File [] files = dir.listFiles(); 83 if (files != null) for (int i = 0; i < files.length; i++) { 84 File f = files[i]; 85 String name = f.getName(); 86 String thispath = path + "/" + name; 87 if (!f.getCanonicalPath().equals(f.getAbsolutePath())) { 88 } else if (f.isDirectory()) { 90 loadImages(pstmt, thispath, f); 91 } else if (name.endsWith(".gif") || 92 name.endsWith(".jpg") || 93 name.endsWith(".GIF") || 94 name.endsWith(".JPG")) { 95 pstmt.clearParameters(); 96 pstmt.setString(1, thispath); 97 FileInputStream in = new FileInputStream (f); 98 99 int len = (int)f.length(); 100 pstmt.setInt(3, len); 101 102 pstmt.setBinaryStream(2, in, len); 103 try { 104 pstmt.execute(); 105 } catch (SQLException e) { 106 String state = e.getSQLState(); 107 if (state != null && state.startsWith("23")) { 108 } else { 111 throw e; 112 } 113 } 114 115 in.close(); 116 } 117 } 118 } 119 } 120 | Popular Tags |