java造轮子_java重复造轮子系列篇------File

package org.jeecgframework.core.util;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import org.apache.commons.lang3.StringUtils;

/**

* 文件操作工具类

*

*

*/

public class FileUtils {

/**

* 获取文件扩展名

*

* @param filename

* @return

*/

public static String getExtend(String filename) {

return getExtend(filename, "");

}

/**

* 获取文件扩展名

*

* @param filename

* @return

*/

public static String getExtend(String filename, String defExt) {

if ((filename != null) && (filename.length() > 0)) {

int i = filename.lastIndexOf('.');

if ((i > 0) && (i < (filename.length() - 1))) {

return (filename.substring(i+1)).toLowerCase();

}

}

return defExt.toLowerCase();

}

/**

* 获取文件名称[不含后缀名]

*

* @param

* @return String

*/

public static String getFilePrefix(String fileName) {

int splitIndex = fileName.lastIndexOf(".");

return fileName.substring(0, splitIndex).replaceAll("\\s*", "");

}

/**

* 获取文件名称[不含后缀名]

* 不去掉文件目录的空格

* @param

* @return String

*/

public static String getFilePrefix2(String fileName) {

int splitIndex = fileName.lastIndexOf(".");

return fileName.substring(0, splitIndex);

}

/**

* 文件复制

*方法摘要:这里一句话描述方法的用途

*@param

*@return void

*/

public static void copyFile(String inputFile,String outputFile) throws FileNotFoundException{

File sFile = new File(inputFile);

File tFile = new File(outputFile);

FileInputStream fis = new FileInputStream(sFile);

FileOutputStream fos = new FileOutputStream(tFile);

int temp = 0;

byte[] buf = new byte[10240];

try {

while((temp = fis.read(buf))!=-1){

fos.write(buf, 0, temp);

}

} catch (IOException e) {

e.printStackTrace();

} finally{

try {

fis.close();

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 判断文件是否为图片

*

*

* @param filename

* 文件名

* 判断具体文件类型

* @return 检查后的结果

* @throws Exception

*/

public static boolean isPicture(String filename) {

// 文件名称为空的场合

if (oConvertUtils.isEmpty(filename)) {

// 返回不和合法

return false;

}

// 获得文件后缀名

//String tmpName = getExtend(filename);

String tmpName = filename;

// 声明图片后缀名数组

String imgeArray[][] = { { "bmp", "0" }, { "dib", "1" },

{ "gif", "2" }, { "jfif", "3" }, { "jpe", "4" },

{ "jpeg", "5" }, { "jpg", "6" }, { "png", "7" },

{ "tif", "8" }, { "tiff", "9" }, { "ico", "10" } };

// 遍历名称数组

for (int i = 0; i < imgeArray.length; i++) {

// 判断单个类型文件的场合

if (imgeArray[i][0].equals(tmpName.toLowerCase())) {

return true;

}

}

return false;

}

/**

* 判断文件是否为DWG

*

*

* @param filename

* 文件名

* 判断具体文件类型

* @return 检查后的结果

* @throws Exception

*/

public static boolean isDwg(String filename) {

// 文件名称为空的场合

if (oConvertUtils.isEmpty(filename)) {

// 返回不和合法

return false;

}

// 获得文件后缀名

String tmpName = getExtend(filename);

// 声明图片后缀名数组

if (tmpName.equals("dwg")) {

return true;

}

return false;

}

/**

* 删除指定的文件

*

* @param strFileName

* 指定绝对路径的文件名

* @return 如果删除成功true否则false

*/

public static boolean delete(String strFileName) {

File fileDelete = new File(strFileName);

if (!fileDelete.exists() || !fileDelete.isFile()) {

LogUtil.info("错误: " + strFileName + "不存在!");

return false;

}

LogUtil.info("--------成功删除文件---------"+strFileName);

return fileDelete.delete();

}

/**

*

* @Title: encodingFileName 2015-11-26 huangzq add

* @Description: 防止文件名中文乱码含有空格时%20

* @param @param fileName

* @param @return 设定文件

* @return String 返回类型

* @throws

*/

public static String encodingFileName(String fileName) {

String returnFileName = "";

try {

returnFileName = URLEncoder.encode(fileName, "UTF-8");

returnFileName = StringUtils.replace(returnFileName, "+", "%20");

if (returnFileName.length() > 150) {

returnFileName = new String(fileName.getBytes("GB2312"), "ISO8859-1");

returnFileName = StringUtils.replace(returnFileName, " ", "%20");

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

LogUtil.info("Don't support this encoding ...");

}

return returnFileName;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值