文件工具类

 

/**
自己写的文件工具类,(以作备用)包括以下功能:
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();
	}
}

 

 

目 录 1 文档介绍.......................................................................................................................................3 1.1 文档概述.............................................................................................................................3 1.2 适用范围.............................................................................................................................3 2 环境描述.......................................................................................................................................3 3 配置 EOS的 WebService服务....................................................................................................3 3.1 配置 WebService 声明.......................................................................................................3 3.1.1 WebLogic8.x环境.....................................................................................................3 3.1.2 JBoss 环境.................................................................................................................4 3.1.3 WebSphere5.x 环境...................................................................................................4 3.2 加入 axis 的 jar包到 CLASSPATH...................................................................................4 3.2.1 WebLogic8.x环境.....................................................................................................4 3.2.2 JBoss 环境.................................................................................................................5 3.2.3 WebSphere5.x 环境...................................................................................................5 3.3 设置调用 WebService 的验证码........................................................................................5 4 验证 EOS的 WebService服务....................................................................................................6 5 如何通过 EOS的 WebService 服务来调用业务逻辑................................................................6 5.1 传统 JAVA 方式.................................................................................................................6 5.1.1 调用方式及说明........................................................................................................6 5.1.2 例子...........................................................................................................................7 5.2 使用 EOS提供的 bizlet 调用方式..................................................................................11 5.2.1 调用方式及说明......................................................................................................11 5.2.2 例子.........................................................................................................................12 5.3 使用 VB 调用方式...........................................................................................................12 5.3.1 例子.........................................................................................................................12 5.4 使用 VC 调用方式...........................................................................................................13 5.4.1 例子.........................................................................................................................13 5.5 使用 delphi调用方式.......................................................................................................15 5.5.1 例子.........................................................................................................................15 附录 1: axis 的 jar包..................................................................................................................16 6 支持............................................................................................................................................18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值