/**
自己写的文件工具类,(以作备用)包括以下功能:
1)生成UUID(不带“-”);
2)获取文件名;
3)获取文件扩展名;
4)递归删除目录;
5)根据byte数组生成文件;
6)打包zip文件
**/
/*******************************************************************************
* $Header$
* $Revision$
* $Date$
*
*==============================================================================
*
* Copyright (c) 2001-2006 Primeton Technologies, Ltd.
* All rights reserved.
*
* Created on 2015-7-17
*******************************************************************************/
package org.gocome.abframe.feature;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.eos.runtime.core.ApplicationContext;
import com.eos.system.annotation.Bizlet;
/**
* TODO 此处填写 class 信息
* 文件操作工具类
* @author Kevin
* @date 2015-07-17 16:27:21
*/
/*
* 修改历史
* $Log$
*/
@Bizlet("")
public class FileUtil {
/**
* 获取下载路径
*/
@Bizlet
public static String getDownloadPath(){
ApplicationContext context = ApplicationContext.getInstance();
context.getApplicationWorkPath();
return context.getApplicationWorkPath() + File.separator + "download";
}
/**
* 打包目录下所有文件
* @param filePath
*/
@Bizlet
public static Map<String,String> zipFiles(String filePath,String fileName) {
Map<String,String> m = new HashMap<String,String>();
File dir = new File(filePath);
File targetFile = new File(dir.getParentFile().getAbsolutePath()
+ File.separator + getFileName(fileName) + ".zip");
Project pj = new Project();
Zip zip = new Zip();
zip.setProject(pj);
zip.setDestFile(targetFile);// 打包完的目标文件
FileSet fileSet = new FileSet();
fileSet.setProject(pj);
fileSet.setDir(dir);// 需要打包的路径
zip.addFileset(fileSet);
zip.execute();
deleteDir(dir);//打包之后连带目录都删除
m.put("downloadFilePath", targetFile.getAbsolutePath());
m.put("fileName", targetFile.getName());
return m;
}
/**
* 根据byte数组,生成文件
*/
@Bizlet
public static void getFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
file = new File(filePath+File.separator+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
/**
* 创建目录
* @param filePath
* @return
*/
@Bizlet
public static String genFilePath(String filePath){
File dir = new File(filePath);
if(!dir.exists()){
dir.mkdirs();
}else{
if(dir.isFile()){//如果存在,并且是文件就删除,同名文件和目录无法同时存在
dir.delete();
dir.mkdirs();
}
}
filePath = filePath + File.separator + getUUID();
new File(filePath).mkdir();
return filePath;
}
/**
* 递归删除目录下的所有文件及子目录下所有文件
* @param dir 将要删除的文件目录
* @return boolean Returns "true" if all deletions were successful.
* If a deletion fails, the method stops attempting to
* delete and returns "false".
*/
private static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
/**
* 获取文件名(带扩展名和不带扩展名)+uuid
* @param s
* @return
*/
@Bizlet
public static String genFileName(String s){
int i = s.lastIndexOf('.');
if ((i > -1) && (i < (s.length() - 1))) {
return s.substring(0,i) + "_" + getUUID() + "." + s.substring(i + 1);
} else {
return s + "_" + getUUID();
}
}
/**
* 获取不带扩展名的文件名
* @param s
* @return
*/
private static String getFileName(String s){
int i = s.lastIndexOf('.');
if ((i > -1) && (i < (s.length() - 1))) {
return s.substring(0,i);
} else {
return s;
}
}
/**
* 获取文件名的扩展名
* @param s
* @return
*/
@Bizlet
public static String getExtensionName(String s) {
int i = s.lastIndexOf('.');
if ((i > -1) && (i < (s.length() - 1))) {
return s.substring(i + 1);
} else {
return "";
}
}
/**
* 获取UUID
*/
@Bizlet
public static String getUUID(){
String s = UUID.randomUUID().toString();
//去掉“-”符号
return s.substring(0,8)+s.substring(9,13)+s.substring(14,18)+s.substring(19,23)+s.substring(24);
}
//Test
public static void main(String[] args){
System.out.println(getFileName("123.txt"));
genFilePath("c:\\download");
getDownloadPath();
}
}