8fd24cb3fb5ae0781ab2ea6d880fe11442da8d86.svn-base 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.toolkit.file;
  2. import java.io.File;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. public class ReadFolder{
  7. /**
  8. * 读取整个目录的文件名
  9. *
  10. * @param path
  11. * 准备读取的目录
  12. * @return
  13. */
  14. public void readFolder(String path, Connection con) {
  15. try {
  16. File a = new File(path);
  17. String[] file = a.list();
  18. File temp = null;
  19. for (int i = 0; i < file.length; i++) {
  20. if (path.endsWith(File.separator)) {
  21. temp = new File(path + file[i]);
  22. } else {
  23. temp = new File(path + File.separator + file[i]);
  24. }
  25. if (temp.isFile()) {
  26. String sql = "insert into imgUrl values('" + path + temp.getName().toString()
  27. + "')";
  28. PreparedStatement ps = con.prepareStatement(sql);
  29. ps.execute();
  30. ps.close();
  31. // WriteToFile(path + temp.getName().toString(),
  32. // "D:/name.txt");
  33. // FileInputStream input = new FileInputStream(temp);
  34. // FileOutputStream output = new FileOutputStream(newPath +
  35. // "/" +
  36. // (temp.getName()).toString());
  37. // byte[] b = new byte[1024 * 5];
  38. // int len;
  39. // while ((len = input.read(b)) != -1) {
  40. // output.write(b, 0, len);
  41. // }
  42. // output.flush();
  43. // output.close();
  44. // input.close();
  45. }
  46. if (temp.isDirectory()) {// 如果是子文件夹
  47. readFolder(path + "/" + file[i], con);
  48. }
  49. }
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. /**
  55. * @param args
  56. */
  57. public static void main(String[] args) {
  58. Connection connection = null;
  59. if (connection == null) {
  60. try {
  61. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  62. connection = DriverManager
  63. .getConnection("jdbc:sqlserver://192.168.100.183;databaseName=TrueMapDB;user=sa;password=sinosoft183");
  64. ReadFolder rf = new ReadFolder();
  65. rf.readFolder("G:/宁波数据发布/Image/NormalImage/000261/2008", connection);
  66. connection.close();
  67. } catch (ClassNotFoundException e) {
  68. e.printStackTrace();
  69. } catch (Exception e) {
  70. e.printStackTrace();
  71. }
  72. }
  73. }
  74. }