/**
* 判斷是文件還是文件夾,若是文件夾,遞歸掃描
*
* @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();
// 獲取系統(tǒng)時間
String[] systemArr = dynamicInfo.getSystem();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 設(shè)置日期格式
String value = df.format(new Date());// new Date()為獲取當(dāng)前系統(tǒng)時間
systemArr[number] = value;
/**
* 單文件復(fù)制方法
*
* @param from_file_name
* 源文件名
* @param path
* 源文件路徑
* @param to_path
* 目標(biāo)路徑
* @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];
/*
* // 獲取系統(tǒng)時間 SimpleDateFormat df = new
* SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 設(shè)置日期格式 String time =
* df.format(new Date());// new Date()為獲取當(dāng)前系統(tǒng)時間 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);
}
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
* 節(jié)點
* @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) {
// 獲取操作的節(jié)點
File dir = new File(path + "http://" + section); // 每個模塊一個文件夾
if (!dir.exists()) {// 如果不存在就創(chuàng)建
dir.mkdirs();
}
/** 目標(biāo)文件夾 * */
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();
}
}
}
/**
* 文件夾是否存在?不存在就創(chuàng)建
*
* @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()) { // 當(dāng)且僅當(dāng)此抽象路徑名表示的文件或目錄存在時,返回 true;否則返回 false
clearChildFiles(file);
} else {
System.out.println("不存在此目錄");
JOptionPane.showMessageDialog(null, "不存在此目錄!", "提示",
JOptionPane.ERROR_MESSAGE);
}
}
/**
* 刪除子文件
*
* @param file
* 目錄
*/
private void clearChildFiles(File file) {
if (file.isDirectory()) { // 當(dāng)且僅當(dāng)此抽象路徑名表示的文件存在且 是一個目錄時,返回 true;否則返回
// false
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
clearChildFiles(files[i]);
}
}
file.delete();
}
/**
* 根據(jù)文件夾路徑,獲取文件個數(shù)
*
* @param filepath
* 文件夾路徑
* @return 文件個數(shù)
*/
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);
}
}
}
/**
* 復(fù)制文件夾下的所有文件
*
* @param src
* 源文件路徑
* @param des
* 目標(biāo)文件路徑
*/
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 + "\\");// 調(diào)用文件拷貝的方法
} catch (IOException e) {
e.printStackTrace();
}
} else if (f.isDirectory()) {
copy(f.getPath(), des + "\\");
}
}
}
/**
* 單文件復(fù)制方法
*
* @param from_file_name
* 源文件名
* @param path
* 源文件路徑
* @param to_path
* 目標(biāo)路徑
* @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];
/*
* // 獲取系統(tǒng)時間 SimpleDateFormat df = new
* SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 設(shè)置日期格式 String time =
* df.format(new Date());// new Date()為獲取當(dāng)前系統(tǒng)時間 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();
// 獲取系統(tǒng)時間
String[] systemArr = dynamicInfo.getSystem();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 設(shè)置日期格式
String value = df.format(new Date());// new Date()為獲取當(dāng)前系統(tǒng)時間
systemArr[number] = value;
dynamicdao.saveSection(
ManageDynamicConstants.FILEPATH,
dynamicInfo);// 修改ini文件
}
} catch (IOException e) {
e.printStackTrace();
}
}
}else if (fileArray[i].isDirectory()) { //文件夾遞歸此方法
System.out.println("文件夾:" + file.getAbsolutePath());
// 遞歸調(diào)用
print(fileArray[i], section, moduleInfo, dynamicInfo);
}
}
}
}
}
}