日期:2014-05-20  浏览次数:20816 次

数据库图片路径 匹配本地磁盘图片路径
需求:表名:doc
  1,用select将sql中doc表的filepath字段取出。
  2,用返回的数据 和本地磁盘下的文件进行匹配(D:\filedata\upload)"注:我这里面有12个文件夹,存着好多不同的图片" 返回数据库中不存在的数据的字段值:id,projectid,projectname。



------解决方案--------------------
Java code

public class Test {
    
    /**
     * 获取路径下所有文件的名字
     *@see 
     * @param path
     * @return List<String>
     */
    private static List getFiles(String path) {
        List files = new ArrayList();
        File file = new File(path);
        getAllFiles(file, files);
        return files;
    }

    private static void getAllFiles(File file, List files) {
        if (file.isFile()) {
            files.add(file.getAbsolutePath());
        } else {
            File[] fs = file.listFiles();
            for (int i = 0; i < fs.length; i++) {
                File f = fs[i];
                getAllFiles(f, files);
            }
        }
    }
    
    public static void main(String[] args) {
        List pngs = getFiles("D:\\workspace\\test");
        List paths = getPath4JDBC();
        pngs.removeAll(paths);
        for(int i = 0; i < pngs.size(); i++){
            System.out.println((String)pngs.get(i));
        }
    }
    
    public static List getPath4JDBC(){
        List jdbcPaths = new ArrayList();
        Connection conn = null;
        Statement st = null;
        ResultSet result = null;
        try {
            Class.forName("");//加载对应数据库的驱动
            conn = DriverManager.getConnection("url", "user", "password");//根据对应数据库的url,user,password获取数据库连接
            st = conn.createStatement();
            result = st.executeQuery("select path from table");//写入你自己的sql
            while(result.next()){
                jdbcPaths.add(result.getString("path"));//我这里暂且用path来获取
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return jdbcPaths;
    }
}