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

📄 filedaoimpl.java

📁 网络硬盘
💻 JAVA
字号:
package com.zte.webfile.dao;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import com.zte.webfile.dto.FileDTO;
import com.zte.webfile.tool.UtilZip;

/**
 * 此类实现了接口fileDAOInterface,用于实现对于数据库中关于文件的管理
 * 
 * @author kwatt
 * 
 */
public class fileDAOImpl implements fileDAOInterface {
	// 定义jdbc模版实现对数据库的所有操作
	private JdbcTemplate jdbcTemplate;

	private UtilZip zip;

	Logger logger = Logger.getLogger(fileDAOImpl.class);

	public UtilZip getZip() {
		return zip;
	}

	public void setZip(UtilZip zip) {
		this.zip = zip;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	/**
	 * 文件移动
	 * 
	 * @param fileId
	 * @param fileAddr
	 * @return
	 */
	public boolean move(Integer[] fileId, Integer fileAddr) {
		// 遍历要移动的文件夹
		for (int i = 0; i < fileId.length; i++) {
			Integer id = fileId[i];
			String sql = "update f_file set fileparent=? where id=?";
			Object[] params = new Object[] { fileAddr, id };
			int[] types = new int[] { Types.INTEGER, Types.INTEGER };
			jdbcTemplate.update(sql, params, types);
		}
		return true;
	}

	/**
	 * 文件重命名
	 * 
	 * @param fd
	 *            文件dto
	 * @return
	 */
	public boolean renew(FileDTO fd) {
		boolean bl = false;
		return bl;
	}

	/**
	 * 文件删除操作
	 * 
	 * @param fileId
	 * @return
	 */
	public boolean delete(Integer[] fileId) {

		// 遍历要删除的文件
		for (int i = 0; i < fileId.length; i++) {
			Integer id = fileId[i];
			// 查找到文件的具体位置
			// SQL获得文件实际路径
			
			String sql_select = "select filepath from f_file where id=?";
			String filePath = (String) jdbcTemplate.queryForObject(sql_select,
					new Object[] { id }, String.class);
			// 判断是删除的是文件夹还是文件
			String sql_isFolder = "select isfolder from f_file where id=?";
			String isFolder = (String) jdbcTemplate.queryForObject(
					sql_isFolder, new Object[] { id }, String.class);
			// 判断是删除的是文件夹还是文件
			if (isFolder.equals("0")) { // 当删除的是文件夹的时候
				
				// 先删除次文件夹		
				String sql_deleteFolder = "delete from f_file where id=?";
				jdbcTemplate.update(sql_deleteFolder, new Object[] { id });
				//存放此文件夹下的文件信息
				final List childList=new ArrayList();
				String sql_child="select filepath from f_file where fileparent=?";
				jdbcTemplate.query(sql_child, new Object[]{id}, new RowCallbackHandler(){

					public void processRow(ResultSet rs) throws SQLException {
						childList.add((String)rs.getString(1));
						
					}});
				Iterator it = childList.iterator();
				while(it.hasNext()){
					String pa=(String)it.next();
					File fi=new File(pa);
					if(fi.exists()){
						fi.delete();
					}
				}
				// 再删除此文件夹下的文件信息				
				String sql_deleteAllFile = "delete from f_file where fileparent=?";
				jdbcTemplate.update(sql_deleteAllFile, new Object[] { id });
				
			} else if (isFolder.equals("1")) { // 删除的是文件
				
				// SQL
				String sql = "delete from f_file where id=?";
				jdbcTemplate.update(sql, new Object[] { id });
			}
			// 对物理文件进行删除
			
			File f = new File(filePath);
			if (f.exists()) {
				f.delete();
			} 
		}
		return true;
	}

	public boolean delete1(Integer[] fileId) {

		return true;
	}

	/**
	 * 判断此文件名的文件在此文件夹下是否存在
	 * 
	 * @param fileName
	 *            文件名
	 * @return true or false
	 */
	public boolean isExist(String fileName, Integer parentPath, String userName) {
		// SQL
		String sql = "select id from f_file where fileName=? and fileparent=? and fileowner=?";
		// 参数列表
		Object[] params = new Object[3];
		params = new Object[] { fileName, parentPath, userName };
		// 用于存放查询结果
		final Map idMap = new HashMap();
		jdbcTemplate.query(sql, params, new RowCallbackHandler() {

			public void processRow(ResultSet rs) throws SQLException {
				// TODO 自动生成方法存根
				idMap.put("id", rs.getString("id"));
			}
		});
		if (idMap.get("id") == null) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 文件上传
	 * 
	 * @param fd
	 * @return 文件上传成功或失败信号
	 */
	public boolean up(List list) {
		// 遍历上传文件集合
		Iterator it = list.iterator();
		while (it.hasNext()) {
			FileDTO dto = (FileDTO) it.next();
			// sql语句
			String sql = "insert into F_FILE(id,filename,filepath,filetype,filesize,fileowner,isfolder,fileuploadtime,fshare,fileparent) values(?,?,?,?,?,?,?,sysdate,?,?)";
			// 参数列表
			int fileCount = this.getFileCount() + 1;
			Integer count = Integer.valueOf(fileCount);
			logger.info("当前要插入的文件id为=>" + count);
			Object[] params = new Object[] { count, dto.getFileName(),
					dto.getFilePath(), dto.getFileType(), dto.getFileSize(),
					dto.getFileOwner(), dto.getIsFolder(), dto.getFshare(),
					dto.getFileParent() };
			// 参数类型列表
			int types[] = new int[] { Types.INTEGER, Types.VARCHAR,
					Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR,
					Types.CHAR, Types.CHAR, Types.INTEGER };
			jdbcTemplate.update(sql, params, types);
		}
		return true;
	}

	/**
	 * 通过文件夹名称获得文件夹的id
	 * 
	 * @param fileName
	 * @return
	 */
	public Integer getFileIdByName(String fileName, String userName) {

		return 1;
	}

	/**
	 * 获得现有文件数
	 * 
	 * @return 文件数量
	 */
	public int getFileCount() {
		// 执行sql,取当前最大的id
		int count = jdbcTemplate.queryForInt("select max(id) from f_file");
		logger.info("当前文件数=>>" + count);
		return count;
	}

	/**
	 * 创建新文件夹
	 * 
	 * @param folderName 文件夹名称
	 * @param parentPath 父文件夹id
	 * @param userName fileowner名称
	 * @return 操作信号
	 */
	public boolean createFolder(String folderName, Integer parentPath,
			String userName) {
		String path = "c:/upload/" + userName + "/" + folderName;
		// 获得当前文件数作为id
		int count = this.getFileCount() + 1;
		Integer id = Integer.valueOf(count);
		// sql
		String sql = "insert into F_FILE(id,filename,filepath,filetype,fileowner,isfolder,fileuploadtime,fshare,fileparent,filesize) values(?,?,?,?,?,?,sysdate,?,?,0)";
		// 参数列表
		Object[] params = new Object[8];
		params = new Object[] { id, folderName, path, "folder", userName, "0",
				"1", parentPath };

		// 参数类型列表
		int types[] = new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR,
				Types.VARCHAR, Types.VARCHAR, Types.CHAR, Types.CHAR,
				Types.INTEGER };
		jdbcTemplate.update(sql, params, types);
		return true;
	}

	/**
	 * 通过文件id查找文件信息
	 * 
	 * @param id
	 *            文件id
	 * @return FileDTO
	 */
	public FileDTO getFileById(String id) {
		Integer fileId = Integer.valueOf(id);
		// SQL
		String sql = "select filename,filepath from f_file where id=?";
		// 参数列表
		Object[] params = new Object[] { fileId };
		// 用于返回文件信息
		final FileDTO dto = new FileDTO();
		// 调用查询方法
		jdbcTemplate.query(sql, params, new RowCallbackHandler() {

			public void processRow(ResultSet rs) throws SQLException {
				dto.setFileName(rs.getString(1));
				dto.setFilePath(rs.getString(2));
			}
		});
		return dto;
	}

	/**
	 * 打包一个文件或者文件夹
	 * 
	 * @return boolean
	 */
	public boolean zipOneFile() {
		return true;
	}

	/**
	 * 打包多个文件或者文件夹
	 * 
	 * @return boolean
	 */
	public boolean zipMultiFiles() {
		return true;
	}

	/**
	 * 共享文件,不运行共享文件夹
	 * 
	 * @return
	 */
	public boolean share(Integer[] id) {
		// SQL
		String sql = "update f_file set fshare='0' where id=?";
		// 遍历所有要共享的文件,吧文件共享属性设为“0“
		for (int i = 0; i < id.length; i++) {
			jdbcTemplate.update(sql, new Object[] { id[i] },
					new int[] { Types.INTEGER });
		}
		return true;
	}

	/**
	 * 通过文件id判断此文件是文件夹还是文件
	 * 
	 * @param id
	 * @return 0表明是文件夹,1表明为文件
	 */
	public int isFileOrFolder(String id) {
		int msg = 0;
		Integer fileId = Integer.valueOf(id);
		String sql = "select isfolder from f_file where id=?";
		String isFolder = (String) jdbcTemplate.queryForObject(sql,
				new Object[] { fileId }, String.class);
		if (isFolder.equals("0")) {
			msg = 0;
		} else if (isFolder.equals("1")) {
			msg = 1;
		}
		return msg;
	}

}

⌨️ 快捷键说明

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