数据库图片路径 匹配本地磁盘图片路径

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

...全文
164 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
安特矮油 2012-05-03
  • 打赏
  • 举报
回复
DataBaseProvide dbp = DataBaseProvide.getNewInstance();

DataTable dt= dbp.executeQuery(sql, null);
这个应该是自己封装的查询方法来,不知道其内部的数据结构,所以真的无能为力。

你看看DataTable是如何取值的吧,把查询出来的路径都存储到list里面就行了
huage 2012-05-03
  • 打赏
  • 举报
回复
public class test {
private List<String> fileNames ;

public void setFileName(String path){
File file = new File(path);
String [] fileNames = file.list() ;
for (int i = 0; i < fileNames.length; i++) {
this.fileNames.add(fileNames[i]) ;
}
}
public List<String> getFileNames(){
List<String> list = new ArrayList<String>() ;//这个为你数据库查出来的
fileNames = new ArrayList<String>() ;
setFileName("D:/filedata/upload") ;
boolean b = false ;
for (int i = 0; i < fileNames.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if(fileNames.get(i).equals(list.get(j))){
b = true ;
}
}
if(b){
fileNames.remove(i) ;
i = i -1 ;
}
}
return fileNames ;
}

}
Candylibin 2012-05-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

Java code

public class Test {

/**
* 获取路径下所有文件的名字
*@see
* @param path
* @return List<String>
*/
private static List getFiles(String path) {
List files ……
[/Quote]

public class DocFileReader{
public static void main(String [] args){
String root = DocFileReader.class.getResource("/").getPath();
root = root.substring(0, root.length() - 1);


XMLConfigManager.initConfigManager(root, new String[] {
"/breadthframework.xml"});

String sql = "select filepath from doc";

DataBaseProvide dbp = DataBaseProvide.getNewInstance();

DataTable dt= dbp.executeQuery(sql, null);

System.out.println(dt);

}
}
这个是我读取数据库的代码
安特矮油 2012-05-03
  • 打赏
  • 举报
回复

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;
}
}
安特矮油 2012-05-03
  • 打赏
  • 举报
回复
下面是获取路径下所有文件名的方法,至于获取数据库中的我就不写了,

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

private static void getAllFiles(File file, List<String> files) {
if (file.isFile()) {
files.add(file.getAbsolutePath());
} else {
File[] fs = file.listFiles();
for (File f : fs) {
getAllFiles(f, files);
}
}
}
Candylibin 2012-05-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

把本地磁盘的图片路径都获取出来,存入ListA,然后把数据库中已经存在的路径也取出来存入ListB,然后用ListA.removeAll(ListB);得到的就是数据库中不存在的了
[/Quote]
求代码老哥
安特矮油 2012-05-03
  • 打赏
  • 举报
回复
把本地磁盘的图片路径都获取出来,存入ListA,然后把数据库中已经存在的路径也取出来存入ListB,然后用ListA.removeAll(ListB);得到的就是数据库中不存在的了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧