00001
package com.quadcap.app.dbimage;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
import java.io.ByteArrayOutputStream;
00042
import java.io.File;
00043
import java.io.FileInputStream;
00044
import java.io.IOException;
00045
import java.io.InputStream;
00046
import java.io.OutputStream;
00047
00048
import java.util.Enumeration;
00049
import java.util.Properties;
00050
00051
import java.sql.Connection;
00052
import java.sql.PreparedStatement;
00053
import java.sql.ResultSet;
00054
import java.sql.Statement;
00055
import java.sql.SQLException;
00056
00057
00058
00059
00060
00061
00062 public class DbImageLoader {
00063
00064 public DbImageLoader() {
00065 }
00066
00067 public void loadImages(Connection conn, String root)
throws Exception {
00068 PreparedStatement pstmt = conn.prepareStatement(
00069
"insert into images values(?,-1,?,?)");
00070 File f =
new File(root);
00071
try {
00072 loadImages(pstmt,
"", f);
00073 } finally {
00074 pstmt.close();
00075 }
00076 }
00077
00078 final void loadImages(PreparedStatement pstmt, String path,
00079 File dir)
00080
throws Exception
00081 {
00082 File[] files = dir.listFiles();
00083
if (files != null)
for (
int i = 0; i < files.length; i++) {
00084 File f = files[i];
00085 String name = f.getName();
00086 String thispath = path +
"/" + name;
00087
if (!f.getCanonicalPath().equals(f.getAbsolutePath())) {
00088
00089 }
else if (f.isDirectory()) {
00090 loadImages(pstmt, thispath, f);
00091 }
else if (name.endsWith(
".gif") ||
00092 name.endsWith(
".jpg") ||
00093 name.endsWith(
".GIF") ||
00094 name.endsWith(
".JPG")) {
00095 pstmt.clearParameters();
00096 pstmt.setString(1, thispath);
00097 FileInputStream in =
new FileInputStream(f);
00098
00099
int len = (
int)f.length();
00100 pstmt.setInt(3, len);
00101
00102 pstmt.setBinaryStream(2, in, len);
00103
try {
00104 pstmt.execute();
00105 }
catch (SQLException e) {
00106 String state = e.getSQLState();
00107
if (state != null && state.startsWith(
"23")) {
00108
00109
00110 }
else {
00111
throw e;
00112 }
00113 }
00114
00115 in.close();
00116 }
00117 }
00118 }
00119 }