package com.test.dao.file_dir;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Stack;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import com.test.db.DBManage;
import com.test.tools.Tools;
import com.test.ui.FilePanel;
/**
* 文件操作管理类
* @author asus
*
*/
public class FileManager {
// 文件分割
public FileSeperate fileSeperate ;
// 文件合并
public FileCombination fileCombination ;
// 删除文件时用到的栈
private Stack<Integer> deleteFileStack = new Stack<Integer>() ;
// 数据库操作
public DBManage dbmanage ;
public Connection connection ;
public PreparedStatement preparedStatement ;
public ResultSet resultSet ;
public HdfsTools hdfsTool ;
public FileManager(DBManage dbmanage) {
this.dbmanage = dbmanage ;
this.hdfsTool = new HdfsTools() ;
}
public FileManager() {
this.dbmanage = new DBManage() ;
this.hdfsTool = new HdfsTools() ;
}
// 显示文件列表
public void showFile(Icon icon , String fileName) {
Tools.loginSuccess.filePanel = new FilePanel(icon , fileName) ;
Tools.loginSuccess.fileBottomPanel.add(Tools.loginSuccess.filePanel) ;
Tools.loginSuccess.repaint() ;
}
// 显示文件列表
public void showFileList(int parentId , int userId) {
try {
String showFile = "select * from file where parent_id = ? and u_id = ? order by f_type DESC" ;
this.connection = this.dbmanage.getConnection() ;
// this.preparedStatement = this.dbmanage.getPreparedStatement(Tools.SHOW_FILE) ;
this.preparedStatement = this.dbmanage.getPreparedStatement(showFile) ;
this.preparedStatement.setInt(1, parentId) ;
this.preparedStatement.setInt(2, userId) ;
this.resultSet = this.preparedStatement.executeQuery() ;
while(this.resultSet.next()) {
// 取得文件名
String fileName = this.resultSet.getString("f_name") ;
String filetype = this.resultSet.getString("f_type") ;
// 显示文件图标
if(filetype.equals("文件")) {
this.showFile(new ImageIcon("image/file.png") , fileName) ;
}else if(filetype.equals("文件夹")) {
this.showFile(new ImageIcon("image/dir.png") , fileName) ;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
// 关闭连接
if(this.resultSet != null) {
try {
this.resultSet.close() ;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.dbmanage.closeLink() ;
}
}
// 创建文件夹
public void createDir() {
//随机产生1000以内的命名规则
String filename = "新建文件夹"+ (int)(Math.random()*1000);
Date date = new Date();
String type = "文件夹";
long size = 0 ;
//随机产生的文件名若重名,则继续随机产生,知道不重名为止
while (!checkFileName(filename)) {
filename = "新建文件夹"+ (int)(Math.random()*1000);
}
try {
String createDir = "insert into File (u_id , f_name , f_date , f_type , f_size , parent_id) values(? , ? , ? , ? , ? , ?)" ;
this.connection = this.dbmanage.getConnection() ;
// this.preparedStatement = this.dbmanage.getPreparedStatement(Tools.CREATE_DIR) ;
this.preparedStatement = this.dbmanage.getPreparedStatement(createDir) ;
this.preparedStatement.setInt(1, Tools.userId) ;
this.preparedStatement.setString(2, filename) ;
this.preparedStatement.setString(3, date.toString()) ;
this.preparedStatement.setString(4, type) ;
this.preparedStatement.setLong(5, size) ;
this.preparedStatement.setInt(6, Tools.parentIdStack.lastElement()) ;
this.preparedStatement.execute() ;
// 新建的文件夹在界面上显示出来
Tools.loginSuccess.filePanel = new FilePanel(new ImageIcon("image/dir.png") , filename) ;
Tools.loginSuccess.fileBottomPanel.add(Tools.loginSuccess.filePanel) ;
Tools.loginSuccess.repaint();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭连接
if(this.resultSet != null) {
try {
this.resultSet.close() ;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.dbmanage.closeLink() ;
}
}
/**
* 检查文件是否重名,通过文件类型和文件名查询
*/
public boolean checkFileName(String filename) {
try {
String checkFileName = "select * from File where u_id = ? and f_name = ? and parent_id = ?" ;
this.connection = this.dbmanage.getConnection() ;
// this.preparedStatement = this.dbmanage.getPreparedStatement(Tools.CHECK_FILE_NAME) ;
this.preparedStatement = this.dbmanage.getPreparedStatement(checkFileName) ;
this.preparedStatement.setInt(1, Tools.userId) ;
this.preparedStatement.setString(2, filename) ;
this.preparedStatement.setInt(3, Tools.parentIdStack.lastElement()) ;
this.resultSet = this.preparedStatement.executeQuery() ;
while(this.resultSet.next()) {
return false ;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false ;
} finally {
// 关闭连接
if(this.resultSet != null) {
try {
this.resultSet.close() ;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.dbmanage.closeLink() ;
}
return true ;
}
/**
* 检查文件是否重名,通过文件ID号查询
*/
public boolean checkPasteFileName() {
try {
String checkPasteFileName = "select * from File where f_name = (select f_name from File where f_id = ?) and u_id = ? and parent_id = ?" ;
this.connection = this.dbmanage.getConnection() ;
// this.preparedStatement = this.dbmanage.getPreparedStatement(Tools.CHECK_PASTE_FILE_NAME) ;
this.preparedStatement = this.dbmanage.getPreparedStatement(checkPasteFileName) ;
this.preparedStatement.setInt(1, Tools.cutFileId) ;
this.preparedStatement.setInt(2, Tools.userId) ;
this.preparedStatement.setInt(3, Tools.parentIdStack.lastElement()) ;
this.resultSet = this.preparedStatement.executeQuery() ;
if(this.resultSet.next()) {
JOptionPane.showMessageDialog(null, "该文件夹已存在,不能粘贴", "警告信息", 1);
return false ;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false ;
} finally {
// 关闭连接
if(this.resultSet != null) {
try {
this.resultSet.close() ;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.dbmanage.closeLink() ;
}
return true ;
}
/**
* 打开文件夹
*/
public boolean openDir(String filename) {
try {
String queryFileId = "select * from File where u_id = ? and f_name = ? and parent_id = ?" ;
this.connection = this.dbmanage.getConnection() ;
// this.preparedStatement = this.dbmanage.getPreparedStatement(Tools.QUERY_FILE_ID) ;
this.preparedStatement = this.dbmanage.getPreparedStatement(queryFileId) ;
this.preparedStatement.setInt(1, Tools.userId) ;
this.preparedStatement.setString(2, filename) ;
this.preparedStatement.setInt(3, Tools.parentIdStack.lastElement()) ;
this.resultSet = this.preparedStatement.executeQuery() ;
int fileId = -1;
String filetype = null ;
if(this.resultSet.next()) {
fileId = this.resultSet.getInt("f_id") ;
filetype = this.resultSet.getString("f_type") ;
}
// 双击打开文件夹
if(filetype != null && filetype.equals("文件夹")) {
Tools.parentIdStack.add(fileId) ;
Tools.loginSuccess.fileBottomPanel.removeAll() ;
this.showFileList(Tools.parentIdStack.lastElement(), Tools.userId) ;
Tools.loginSuccess.fileBottomPanel.repaint() ;
// 设置文件路径
String context = Tools.loginSuccess.search.getText() ;
Tools.loginSuccess.search.setText(context + "\\" + filename) ;
}else if(filet
机器学习的喵
- 粉丝: 2016
- 资源: 1784
最新资源
- 燃气表摇杆上料压装机sw18全套技术开发资料100%好用.zip
- 美容品、电脑、书检测17-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
- Runtime Editor v4.4.6
- 考虑需求响应的微网优化调度matlab 程序采用粒子群算法,风力发电机、光伏发电机、储能装置、燃气轮机、柴油机组等主体,考虑负荷需求响应、soc约束等,程序模块化编程,注释清楚,有对应资料
- STM32F103C8T6实现用2.4G模块NRF2401传输室内温湿度并显示OLED 源程序
- GENEX-Probe-3.5软件及GENEX-Assistant-3.5软件使用及报告输出指导书
- , 免费,局域网,IP电话, SIP, VOIP, 视频通话,可与手机互通,手机上也安装 yealink
- 西门子S7-200PLC和MCGS组态的燃油锅炉控制系统
- Peer-to-peer LAN chat and file transfer, 局域网点对点消息传输和文件 传输,类似飞秋
- FreeModbus从机基于STM32平台移植源码
- 基于高频方波电压注入的永磁同步电机无感FOC 1.采用方波电压注入,减少了提取dq轴基频电流时LPF的使用,提高了系统的动态性能; 2.可实现带载起动和突加负载运行; 提供算法对应的参考文献和仿真模型
- 免费,局域网,IP电话, SIP, VOIP, 视频通话,可与手机互通,需要安装 手机版本
- 旅游景点数据集.zip
- 基于龙贝格观测器的永磁同步电机无感FOC 1.采用龙伯格观测器提取电机反电势,使用PLL从反电势中获得转子位置和速度信息 2.提供算法对应的参考文献和仿真模型,支持技术解答 仿真模型纯手工搭建
- 中国汽车基础软件发展白皮书4.0
- 背包、炸弹、瓶子、椅子、时钟、键盘、老鼠、人检测1-YOLO(v5至v11)、COCO、CreateML、Paligemma、TFRecord、VOC数据集合集.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈