001: package com.quadcap.app.dbimage;
002:
003: /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.ByteArrayOutputStream;
042: import java.io.File;
043: import java.io.FileInputStream;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.io.OutputStream;
047:
048: import java.util.Enumeration;
049: import java.util.Properties;
050:
051: import java.sql.Connection;
052: import java.sql.PreparedStatement;
053: import java.sql.ResultSet;
054: import java.sql.Statement;
055: import java.sql.SQLException;
056:
057: /**
058: * This class loads images from the file system into a database.
059: *
060: * @author Stan Bailes
061: */
062: public class DbImageLoader {
063:
064: public DbImageLoader() {
065: }
066:
067: public void loadImages(Connection conn, String root)
068: throws Exception {
069: PreparedStatement pstmt = conn
070: .prepareStatement("insert into images values(?,-1,?,?)");
071: File f = new File(root);
072: try {
073: loadImages(pstmt, "", f);
074: } finally {
075: pstmt.close();
076: }
077: }
078:
079: final void loadImages(PreparedStatement pstmt, String path, File dir)
080: throws Exception {
081: File[] files = dir.listFiles();
082: if (files != null)
083: for (int i = 0; i < files.length; i++) {
084: File f = files[i];
085: String name = f.getName();
086: String this path = path + "/" + name;
087: if (!f.getCanonicalPath().equals(f.getAbsolutePath())) {
088: // hack: skip anything that appears to be a symbolic link...
089: } else if (f.isDirectory()) {
090: loadImages(pstmt, this path, f);
091: } else if (name.endsWith(".gif")
092: || name.endsWith(".jpg")
093: || name.endsWith(".GIF")
094: || name.endsWith(".JPG")) {
095: pstmt.clearParameters();
096: pstmt.setString(1, this path);
097: FileInputStream in = new FileInputStream(f);
098:
099: 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: // image already there. We could check timestamps
109: // or something...
110: } else {
111: throw e;
112: }
113: }
114:
115: in.close();
116: }
117: }
118: }
119: }
|