⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filecleaner.java

📁 “JSP数据库项目案例导航”一书从第一章到第十一章各章实例的源程序文件以及数据库文件。 注意: 1. 本书中的案例提供的数据库环境不同
💻 JAVA
字号:
/*
 * Created on 2004-7-6
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package oa.main;

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class FileCleaner {

	/**
	 * 
	 */

	final static String root = "D:/hgoa_files";

	final static String recycle = "D:/hgoa_files/recycle";

	//final static String logFile= recycle+ "/recycle.log";

	final static String DepartFilePath = "/cabinet/departfiles";
	final static String PersonFilePath = "/cabinet/personfiles";

	final static String NewsImgPath = "/news/img";
	final static String NewsAppendixPath = "/news/appendix";

	Connection conn = null;
	Statement stm = null;
	PreparedStatement pstm = null;
	ResultSet rs = null;

	boolean connected = false;

	/**生成Oracle SQLServer等的连接*/
	public void createConn(String drv, String url, String usr, String pwd) {
		try {
			if (connected) {
				throw new SQLException("数据库已连接,无须重连!");
			}
			Class.forName(drv).newInstance();
			conn = DriverManager.getConnection(url, usr, pwd);

			connected = true;

		} catch (ClassNotFoundException ec) {
			System.out.println("从自身建立数据库连接时出错;\r\n错误为:" + ec);
		} catch (SQLException e) {
			System.out.println("从自身建立数据库连接时出错;\r\n错误为:" + e);
		} catch (Exception et) {
			System.out.println("从自身建立数据库连接时出错;\r\n错误为:" + et);
		}
	}

	/**释放连接*/
	public void closeConn() {
		try {
			if(conn!=null && !conn.isClosed()) {
				conn.close();
				connected = false;
			}
		} catch (SQLException e) {
			System.out.println("关闭数据库连接时出错;\r\n错误为:" + e);
		}
	}

	public void checkDirs() {
		java.io.File f;

		f = new java.io.File(recycle + "/cabinet");
		if (!f.exists()) {
			f.mkdir();
		}

		f = new java.io.File(recycle + PersonFilePath);
		if (!f.exists()) {
			f.mkdir();
		}

		f = new java.io.File(recycle + DepartFilePath);
		if (!f.exists()) {
			f.mkdir();
		}

		f = new java.io.File(recycle + "/news");
		if (!f.exists()) {
			f.mkdir();
		}

		f = new java.io.File(recycle + NewsImgPath);
		if (!f.exists()) {
			f.mkdir();
		}

		f = new java.io.File(recycle + NewsAppendixPath);
		if (!f.exists()) {
			f.mkdir();
		}
	}

	public Vector getAllFileNames(String path) {
		Vector v = new Vector();
		v.clear();

		java.io.File f = new java.io.File(path);
		if (!f.exists())
			return v;
		java.io.File[] fa = f.listFiles();
		for (int i = 0; i < fa.length; i++) {
			if (fa[i].exists()) {

				if (fa[i].isFile()) {
					v.add(fa[i].getAbsolutePath());
				}
				if (fa[i].isDirectory()) {
					v.addAll(getAllFileNames(fa[i].getAbsolutePath()));
				}
			}

		}
		return v;
	}

	public void CleanPersonFile() throws Exception {
		System.out.println(
			"\n\nStrat the process of cleaning '个人文件柜' ====================");
		String sql = "Select WJLJ from ZZ_GRWJB where WJLJ is not null";
		pstm = conn.prepareStatement(sql);
		rs = pstm.executeQuery();
		//TODO
		//String wjlj=rs.getString(1);
		Vector vect = new Vector();
		Vector v = getAllFileNames(root + PersonFilePath);

		System.out.println("PersonFile Total File(s):" + v.size());
		Vector resultV = new Vector();

		String wjlj = "";
		System.out.print("Reading Data From DataBase...");
		while (rs.next()) {
			wjlj = rs.getString("WJLJ");
			wjlj = root + PersonFilePath + "/" + wjlj;
			java.io.File f = new java.io.File(wjlj);
			if (f.exists())
				vect.add(f.getAbsolutePath());
		}
		System.out.println("OK!");

		for (int i = 0; i < v.size(); i++) {
			System.out.print("Checking...[" + v.get(i) + "]");
			if (vect.contains(v.get(i)))
				System.out.println("...OK!");
			else {
				System.out.println("...Error!");
				resultV.add(v.get(i));
			}

		}

		System.out.println("Error File:");
		for (int i = 0; i < resultV.size(); i++) {
			System.out.println(resultV.get(i));
			java.io.File f = new java.io.File((String) resultV.get(i));
			if (f.exists() && f.isFile()) {
				String recyclePath =
					recycle + PersonFilePath + "/" + f.getName();

				f.renameTo(new java.io.File(recyclePath));

				System.out.println(
					"recycle: ["
						+ recycle
						+ PersonFilePath
						+ "/"
						+ f.getName()
						+ "]");
			}
		}

		System.out.println(resultV.size() + " File(s) have been cleaned!");

		rs.close();
		pstm.close();
	}

	public void CleanDepartFile() throws Exception {
		System.out.println(
			"\n\nStrat the process of cleaning '部门资料' ====================");
		String sql = "Select WJLJ from ZZ_BMWJB where WJLJ is not null";
		pstm = conn.prepareStatement(sql);
		rs = pstm.executeQuery();
		//TODO
		//String wjlj=rs.getString(1);
		Vector vect = new Vector();
		Vector v = getAllFileNames(root + DepartFilePath);

		System.out.println("PersonFile Total File(s):" + v.size());
		Vector resultV = new Vector();

		String wjlj = "";
		System.out.print("Reading Data From DataBase...");
		while (rs.next()) {
			wjlj = rs.getString("WJLJ");
			wjlj = root + DepartFilePath + "/" + wjlj;
			java.io.File f = new java.io.File(wjlj);
			if (f.exists())
				vect.add(f.getAbsolutePath());
		}
		System.out.println("OK!");

		for (int i = 0; i < v.size(); i++) {
			System.out.print("Checking...[" + v.get(i) + "]");
			if (vect.contains(v.get(i)))
				System.out.println("...OK!");
			else {
				System.out.println("...Error!");
				resultV.add(v.get(i));
			}

		}

		System.out.println("Error File:");
		for (int i = 0; i < resultV.size(); i++) {
			System.out.println(resultV.get(i));
			java.io.File f = new java.io.File((String) resultV.get(i));
			if (f.exists() && f.isFile()) {
				String recyclePath =
					recycle + DepartFilePath + "/" + f.getName();
				f.renameTo(new java.io.File(recyclePath));
				System.out.println(
					"recycle: ["
						+ recycle
						+ DepartFilePath
						+ "/"
						+ f.getName()
						+ "]");
			}
		}

		System.out.println(resultV.size() + " File(s) have been cleaned!");

		rs.close();
		pstm.close();

	}

	public void CleanNewsImg() throws Exception {
		System.out.println(
			"\n\nStrat the process of cleaning '文章-图片' ====================");
		String sql = "Select IMG from ARTICLE where IMG is not null";
		pstm = conn.prepareStatement(sql);
		rs = pstm.executeQuery();
		//TODO
		//String wjlj=rs.getString(1);
		Vector vect = new Vector();
		Vector v = getAllFileNames(root + NewsImgPath);

		System.out.println("NewImg Total File(s):" + v.size());
		Vector resultV = new Vector();

		String wjlj = "";
		System.out.print("Reading Data From DataBase...");
		while (rs.next()) {
			wjlj = rs.getString("IMG");
			wjlj = root + NewsImgPath + "/" + wjlj;
			java.io.File f = new java.io.File(wjlj);
			if (f.exists())
				vect.add(f.getAbsolutePath());
		}
		System.out.println("OK!");

		for (int i = 0; i < v.size(); i++) {
			System.out.print("Checking...[" + v.get(i) + "]");
			if (vect.contains(v.get(i)))
				System.out.println("...OK!");
			else {
				System.out.println("...Error!");
				resultV.add(v.get(i));
			}

		}

		System.out.println("Error File:");
		for (int i = 0; i < resultV.size(); i++) {
			System.out.println(resultV.get(i));
			java.io.File f = new java.io.File((String) resultV.get(i));
			if (f.exists() && f.isFile()) {
				String recyclePath = recycle + NewsImgPath + "/" + f.getName();
				f.renameTo(new java.io.File(recyclePath));
				System.out.println(
					"recycle: ["
						+ recycle
						+ NewsImgPath
						+ "/"
						+ f.getName()
						+ "]");
			}
		}

		System.out.println(resultV.size() + " File(s) have been cleaned!");

		rs.close();
		pstm.close();
	}

	public void CleanNewsAppendix() throws Exception {
		System.out.println(
			"\n\nStrat the process of cleaning '文章-附件' ====================");
		String sql = "Select ORGID from ARTICLE where ORGID is not null";
		pstm = conn.prepareStatement(sql);
		rs = pstm.executeQuery();
		//TODO
		//String wjlj=rs.getString(1);
		Vector vect = new Vector();
		Vector v = getAllFileNames(root + NewsAppendixPath);

		System.out.println("NewImg Total File(s):" + v.size());
		Vector resultV = new Vector();

		String wjlj = "";
		System.out.print("Reading Data From DataBase...");
		while (rs.next()) {

			wjlj = rs.getString("ORGID");
			wjlj = wjlj.trim();

			String[] lj = wjlj.split(",");
			for (int i = 0; i < lj.length; i++) {

				wjlj = root + NewsAppendixPath + "/" + lj[i];
				java.io.File f = new java.io.File(wjlj);
				if (f.exists())
					vect.add(f.getAbsolutePath());
			}
		}
		System.out.println("OK!");

		for (int i = 0; i < v.size(); i++) {
			System.out.print("Checking...[" + v.get(i) + "]");
			if (vect.contains(v.get(i)))
				System.out.println("...OK!");
			else {
				System.out.println("...Error!");
				resultV.add(v.get(i));
			}

		}

		System.out.println("Error File:");
		for (int i = 0; i < resultV.size(); i++) {
			System.out.println(resultV.get(i));
			java.io.File f = new java.io.File((String) resultV.get(i));
			if (f.exists() && f.isFile()) {
				String recyclePath =
					recycle + NewsAppendixPath + "/" + f.getName();
				f.renameTo(new java.io.File(recyclePath));
				System.out.println(
					"recycle: ["
						+ recycle
						+ NewsAppendixPath
						+ "/"
						+ f.getName()
						+ "]");
			}
		}

		System.out.println(resultV.size() + " File(s) have been cleaned!");

		rs.close();
		pstm.close();
	}

	public FileCleaner() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void go(javax.servlet.http.HttpServletResponse res) {
		java.io.PrintStream console = System.out;
		try{
		//Check the Dirs for recycle files
			checkDirs();
			
			oa.main.ParentBean p = new oa.main.ParentBean();
			conn=p.getConn();
			
			System.setOut(new java.io.PrintStream(res.getOutputStream()));
			//System.out.println("<table width='90%' align=center><tr width='100%'><td style='font:8pt Verdana,Arial,宋体;'>");
			CleanPersonFile();
			CleanDepartFile();
			System.out.println("Progress successfully terminated.=============");
			//System.out.println("</td></tr></table>");


			System.out.flush();
			System.setOut(console);

			p.closeConn();
		}catch(Exception e){
			System.out.println("A Exception occur during cleaning file(s). The Exception message is :"+e.getMessage());			
		}finally{
			closeConn();
			System.setOut(console);
		}
		
		

	}

	public static void main(String[] args) throws Exception {
		FileCleaner fc = new FileCleaner();

		//Check the Dirs for recycle files
		fc.checkDirs();
		
		oa.main.ParentBean p = new oa.main.ParentBean();
		fc.createConn(
			"oracle.jdbc.driver.OracleDriver",
			"jdbc:oracle:thin:@10.0.0.94:1521:hg",
			"oaadminuser",
			"lancom4454");
		fc.conn=p.getConn();
	
		fc.CleanPersonFile();
		fc.CleanDepartFile();
		//fc.CleanNewsImg();
		//fc.CleanNewsAppendix();

		fc.closeConn();
		System.out.println("Progress successfully terminated.=============");

		//System.out.print(v);

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -