视项目做相应的调整: /** * 上传文件夹方法 * * @param developer * @param moduleInfo * @param dynamicInfo */ public void upLoadFileAll(JButton developer, String section, ModuleInfo moduleInfo, DynamicInfo dynamicInfo) { JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(true); FileNameExtensionFilter filter = new FileNameExtensionFilter("xml", "war", "ear", "txt", "doc", "docx", "ini", "sql", "xlsx");// 过滤文件类型 chooser.setFileFilter(filter); chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 显示目录和文件 int returnVal = chooser.showOpenDialog(developer); if (returnVal == JFileChooser.APPROVE_OPTION) {// 得到选择的文件 File file = chooser.getSelectedFile(); if (file == null) { return; } else { print(file, section, moduleInfo, dynamicInfo); JOptionPane.showMessageDialog(null,"文件夹上传已完成", "提示",JOptionPane.INFORMATION_MESSAGE); } } } /** * 判断是文件还是文件夹,若是文件夹,递归扫描 * * @param file * @param section * @param moduleInfo * @param dynamicInfo */ public void print(File file, String section, ModuleInfo moduleInfo, DynamicInfo dynamicInfo) { if (file != null) { String path = ManageConstants.FILEPATH_PACKAGE; // 文件存放路径 File[] fileArray = file.listFiles();//获取所有文件 if (fileArray != null && fileArray.length > 0) { for (int i = 0; i < fileArray.length; i++) { if (fileArray[i].isFile()) { // 截取文件名和文件后缀名 String[] strArray = fileArray[i].getName().split("\\."); String name = strArray[0].trim(); String type = strArray[1].trim(); String[] componentName = moduleInfo.getComponentName(); // 需要上传的文件名字 String[] extension = moduleInfo.getExtension(); // 需要上传的文件后缀名 boolean flag = false; int number =0; k: for (int z = 0; z < componentName.length; z++) { if (name.equals(componentName[z])) {// 如果文件名相同 if (type.equals(extension[z])) {// 并且文件后缀相同 flag = true; number = z; break k; } } } if (flag) {// 存在 System.out.println("文件夹:" + file.getAbsolutePath()); System.out.println("文件:" + fileArray[i].getAbsolutePath()); // 判断后缀名 String toPath = ""; if ("war".equals(type) || "ear".equals(type)) { FileIsExists(path + "/" + section + ManageConstants.FILEPATH_APP); toPath = path + "/" + section + ManageConstants.FILEPATH_APP; } else if ("sql".equals(type)) { FileIsExists(path + "/" + section + ManageConstants.FILEPATH_SQL_SCRIPT); toPath = path + "/" + section + ManageConstants.FILEPATH_SQL_SCRIPT; } else { FileIsExists(path + "/" + section + ManageConstants.FILEPATH_DOC); toPath = path + "/" + section + ManageConstants.FILEPATH_DOC; } try { download(fileArray[i].getName(), fileArray[i].getPath(), toPath); if (moduleInfo != null) { ModuleDao moduleDao = new ModuleDao(); String[] typeArr = moduleInfo.getType(); typeArr[number] ="已上传"; moduleDao.saveSection( ManageConstants.FILEPATH, moduleInfo);// 修改ini文件 } if (dynamicInfo != null) { DynamicDao dynamicdao = new DynamicDao(); //操作人 String[] operator = dynamicInfo.getOperator(); operator[number]= moduleInfo.getLeading(); // 获取系统时间 String[] systemArr = dynamicInfo.getSystem(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式 String value = df.format(new Date());// new Date()为获取当前系统时间 systemArr[number] = value; dynamicdao.saveSection( ManageDynamicConstants.FILEPATH, dynamicInfo);// 修改ini文件 } } catch (IOException e) { e.printStackTrace(); } } }else if (fileArray[i].isDirectory()) { //文件夹递归此方法 System.out.println("文件夹:" + file.getAbsolutePath()); // 递归调用 print(fileArray[i], section, moduleInfo, dynamicInfo); } } } } } /** * 单文件复制方法 * * @param from_file_name * 源文件名 * @param path * 源文件路径 * @param to_path * 目标路径 * @throws IOException */ public static void download(String from_file_name, String path, String to_path) throws IOException { OutputStream output = null; FileInputStream input = null; try { input = new FileInputStream(path); byte[] buffer = new byte[1024]; /* * // 获取系统时间 SimpleDateFormat df = new * SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式 String time = * df.format(new Date());// new Date()为获取当前系统时间 time = * time.replaceAll("\\:", "\\-"); time = time.replaceAll("\\s", * "\\-"); String[] strArray = from_file_name.split("\\."); String * name = strArray[0].trim(); String type = strArray[1].trim(); File * des = new File(to_path, name + time + "." + type); */ File des = new File(to_path, from_file_name); output = new FileOutputStream(des); int len = 0; while (-1 != (len = input.read(buffer))) { output.write(buffer, 0, len); } if (output != null) { try { if (input != null) output.close(); input.close(); // JOptionPane.showMessageDialog(null, "成功!", "提示", 2); } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e1) { JOptionPane.showMessageDialog(null, "失败!", "提示", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } catch (IOException e1) { JOptionPane.showMessageDialog(null, "失败!", "提示", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } } ==================================================== package com.iss.iaf.packager.setup.util; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashSet; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.filechooser.FileNameExtensionFilter; import com.iss.iaf.packager.bean.DynamicInfo; import com.iss.iaf.packager.bean.ModuleInfo; import com.iss.iaf.packager.setup.dao.tabledao.DynamicDao; import com.iss.iaf.packager.setup.dao.tabledao.ModuleDao; import com.iss.iaf.packager.util.ManageConstants; import com.iss.iaf.packager.util.ManageDynamicConstants; public class FileUpLoad { /** * 单文件文件上传功能 * * @param developer * 按钮控件名称 * @param section * 节点 * @param fileName * 文件名 * @param fileSign * 文件后缀 * @param ModuleInfo * 类 * @param DynamicInfo * 类 */ public void upLoad(JButton developer, String section, String fileName, String fileSign, ModuleInfo moduleInfo, DynamicInfo dynamicInfo) { JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(true); /** 过滤文件类型 * */ FileNameExtensionFilter filter = new FileNameExtensionFilter("xml", "war", "ear", "txt", "doc", "docx", "ini", "sql", "xlsx"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(developer); if (returnVal == JFileChooser.APPROVE_OPTION) { /** 得到选择的文件* */ File[] arrfiles = chooser.getSelectedFiles(); if (arrfiles == null || arrfiles.length == 0) { return; } FileInputStream input = null; FileOutputStream out = null; // String path = "./src/main/resources"; String path = ManageConstants.FILEPATH_PACKAGE; try { for (File f : arrfiles) { // 获取操作的节点 File dir = new File(path + "//" + section); // 每个模块一个文件夹 if (!dir.exists()) {// 如果不存在就创建 dir.mkdirs(); } /** 目标文件夹 * */ File[] fs = dir.listFiles(); HashSet<String> set = new HashSet<String>(); for (File file : fs) { set.add(file.getName()); } File des = null; // 截取文件名和文件后缀名 String[] strArray = f.getName().split("\\."); String name = strArray[0].trim(); String type = strArray[1].trim(); if (fileName.equals(name) && fileSign.equals(type)) {// 如果文件名跟后缀名与配置文件的一致,才允许上传 if ("war".equals(fileSign) || "ear".equals(fileSign)) { FileIsExists(path + "/" + section + ManageConstants.FILEPATH_APP); des = new File(path + "/" + section + ManageConstants.FILEPATH_APP, f.getName()); } else if ("sql".equals(fileSign)) { FileIsExists(path + "/" + section + ManageConstants.FILEPATH_SQL_SCRIPT); des = new File(path + "/" + section + ManageConstants.FILEPATH_SQL_SCRIPT, f.getName()); } else { FileIsExists(path + "/" + section + ManageConstants.FILEPATH_DOC); des = new File(path + "/" + section + ManageConstants.FILEPATH_DOC, f.getName()); } out = new FileOutputStream(des); input = new FileInputStream(f); byte[] buffer = new byte[1024]; int len = 0; while (-1 != (len = input.read(buffer))) { out.write(buffer, 0, len); } out.close(); input.close(); } else { // 文件名跟后缀名与配置文件不一致,不允许上传 JOptionPane.showMessageDialog(null, "文件名跟后缀名与配置文件不一致!", "提示", JOptionPane.ERROR_MESSAGE); return; } } JOptionPane.showMessageDialog(null, "上传成功!", "提示", JOptionPane.INFORMATION_MESSAGE); if (moduleInfo != null) { ModuleDao moduleDao = new ModuleDao(); moduleDao.saveSection(ManageConstants.FILEPATH, moduleInfo);// 修改ini文件 } if (dynamicInfo != null) { DynamicDao dynamicdao = new DynamicDao(); dynamicdao.saveSection(ManageDynamicConstants.FILEPATH, dynamicInfo);// 修改ini文件 } } catch (FileNotFoundException e1) { JOptionPane.showMessageDialog(null, "上传失败!", "提示", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } catch (IOException e1) { JOptionPane.showMessageDialog(null, "上传失败!", "提示", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } } } /** * 文件夹是否存在?不存在就创建 * * @param dir */ private void FileIsExists(String dir) { File newDir = new File(dir); if (!newDir.exists()) { newDir.mkdirs(); } } /** * 删除目录及目录下的文件 * * @param filePath * 文件路径 */ public void clearFiles(String filePath) { File file = new File(filePath); if (file.exists()) { // 当且仅当此抽象路径名表示的文件或目录存在时,返回 true;否则返回 false clearChildFiles(file); } else { System.out.println("不存在此目录"); JOptionPane.showMessageDialog(null, "不存在此目录!", "提示", JOptionPane.ERROR_MESSAGE); } } /** * 删除子文件 * * @param file * 目录 */ private void clearChildFiles(File file) { if (file.isDirectory()) { // 当且仅当此抽象路径名表示的文件存在且 是一个目录时,返回 true;否则返回 // false File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { clearChildFiles(files[i]); } } file.delete(); } /** * 根据文件夹路径,获取文件个数 * * @param filepath * 文件夹路径 * @return 文件个数 */ int count = 0; public int getFileSize(String filepath) { if (filepath != null && filepath != "") { File file = new File(filepath); File[] listfile = file.listFiles(); if (listfile != null && listfile.length > 0) { for (int i = 0; i < listfile.length; i++) { if (!listfile[i].isDirectory()) { String temp = listfile[i].toString().substring(0, listfile[i].toString().length()); System.out.println("temp==" + temp); count++; System.out.println("文件" + count + "---path=" + listfile[i]); } else { getFileSize(listfile[i].toString()); } } } } return count; } /** * 上传文件夹方法 * * @param developer * @param moduleInfo * @param dynamicInfo */ public void upLoadFileAll(JButton developer, String section, ModuleInfo moduleInfo, DynamicInfo dynamicInfo) { JFileChooser chooser = new JFileChooser(); chooser.setMultiSelectionEnabled(true); FileNameExtensionFilter filter = new FileNameExtensionFilter("xml", "war", "ear", "txt", "doc", "docx", "ini", "sql", "xlsx");// 过滤文件类型 chooser.setFileFilter(filter); chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 显示目录和文件 int returnVal = chooser.showOpenDialog(developer); if (returnVal == JFileChooser.APPROVE_OPTION) {// 得到选择的文件 File file = chooser.getSelectedFile(); if (file == null) { return; } else { print(file, section, moduleInfo, dynamicInfo); JOptionPane.showMessageDialog(null,"文件夹上传已完成", "提示",JOptionPane.INFORMATION_MESSAGE); } } } /** * 复制文件夹下的所有文件 * * @param src * 源文件路径 * @param des * 目标文件路径 */ public static void copy(String src, String des) { File file1 = new File(src); File[] fs = file1.listFiles(); File file2 = new File(des); if (!file2.exists()) { file2.mkdirs(); } for (File f : fs) { if (f.isFile()) { try { download(f.getName(), f.getPath(), des + "\\");// 调用文件拷贝的方法 } catch (IOException e) { e.printStackTrace(); } } else if (f.isDirectory()) { copy(f.getPath(), des + "\\"); } } } /** * 单文件复制方法 * * @param from_file_name * 源文件名 * @param path * 源文件路径 * @param to_path * 目标路径 * @throws IOException */ public static void download(String from_file_name, String path, String to_path) throws IOException { OutputStream output = null; FileInputStream input = null; try { input = new FileInputStream(path); byte[] buffer = new byte[1024]; /* * // 获取系统时间 SimpleDateFormat df = new * SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式 String time = * df.format(new Date());// new Date()为获取当前系统时间 time = * time.replaceAll("\\:", "\\-"); time = time.replaceAll("\\s", * "\\-"); String[] strArray = from_file_name.split("\\."); String * name = strArray[0].trim(); String type = strArray[1].trim(); File * des = new File(to_path, name + time + "." + type); */ File des = new File(to_path, from_file_name); output = new FileOutputStream(des); int len = 0; while (-1 != (len = input.read(buffer))) { output.write(buffer, 0, len); } if (output != null) { try { if (input != null) output.close(); input.close(); // JOptionPane.showMessageDialog(null, "成功!", "提示", 2); } catch (IOException e) { e.printStackTrace(); } } } catch (FileNotFoundException e1) { JOptionPane.showMessageDialog(null, "失败!", "提示", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } catch (IOException e1) { JOptionPane.showMessageDialog(null, "失败!", "提示", JOptionPane.ERROR_MESSAGE); e1.printStackTrace(); } } /** * 判断是文件还是文件夹,若是文件夹,递归扫描 * * @param file * @param section * @param moduleInfo * @param dynamicInfo */ public void print(File file, String section, ModuleInfo moduleInfo, DynamicInfo dynamicInfo) { if (file != null) { String path = ManageConstants.FILEPATH_PACKAGE; // 文件存放路径 File[] fileArray = file.listFiles();//获取所有文件 if (fileArray != null && fileArray.length > 0) { for (int i = 0; i < fileArray.length; i++) { if (fileArray[i].isFile()) { // 截取文件名和文件后缀名 String[] strArray = fileArray[i].getName().split("\\."); String name = strArray[0].trim(); String type = strArray[1].trim(); String[] componentName = moduleInfo.getComponentName(); // 需要上传的文件名字 String[] extension = moduleInfo.getExtension(); // 需要上传的文件后缀名 boolean flag = false; int number =0; k: for (int z = 0; z < componentName.length; z++) { if (name.equals(componentName[z])) {// 如果文件名相同 if (type.equals(extension[z])) {// 并且文件后缀相同 flag = true; number = z; break k; } } } if (flag) {// 存在 System.out.println("文件夹:" + file.getAbsolutePath()); System.out.println("文件:" + fileArray[i].getAbsolutePath()); // 判断后缀名 String toPath = ""; if ("war".equals(type) || "ear".equals(type)) { FileIsExists(path + "/" + section + ManageConstants.FILEPATH_APP); toPath = path + "/" + section + ManageConstants.FILEPATH_APP; } else if ("sql".equals(type)) { FileIsExists(path + "/" + section + ManageConstants.FILEPATH_SQL_SCRIPT); toPath = path + "/" + section + ManageConstants.FILEPATH_SQL_SCRIPT; } else { FileIsExists(path + "/" + section + ManageConstants.FILEPATH_DOC); toPath = path + "/" + section + ManageConstants.FILEPATH_DOC; } try { download(fileArray[i].getName(), fileArray[i].getPath(), toPath); if (moduleInfo != null) { ModuleDao moduleDao = new ModuleDao(); String[] typeArr = moduleInfo.getType(); typeArr[number] ="已上传"; moduleDao.saveSection( ManageConstants.FILEPATH, moduleInfo);// 修改ini文件 } if (dynamicInfo != null) { DynamicDao dynamicdao = new DynamicDao(); //操作人 String[] operator = dynamicInfo.getOperator(); operator[number]= moduleInfo.getLeading(); // 获取系统时间 String[] systemArr = dynamicInfo.getSystem(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式 String value = df.format(new Date());// new Date()为获取当前系统时间 systemArr[number] = value; dynamicdao.saveSection( ManageDynamicConstants.FILEPATH, dynamicInfo);// 修改ini文件 } } catch (IOException e) { e.printStackTrace(); } } }else if (fileArray[i].isDirectory()) { //文件夹递归此方法 System.out.println("文件夹:" + file.getAbsolutePath()); // 递归调用 print(fileArray[i], section, moduleInfo, dynamicInfo); } } } } } }
|