file operate

	
/**
* 获取随机名称(文件)
*
* @param fileRealName
* @return Created:2010-7-30
* @author:WKF28474
*/
public static String getRandomName(String fileRealName) {
String temp_time = new SimpleDateFormat("yyyyMMddHHmmss")
.format(new Date());
String randomName = null;
String end_point = "";
int point_int = fileRealName.lastIndexOf(".");
if (point_int != -1) {
end_point = fileRealName.substring(point_int + 1,
fileRealName.length()).toLowerCase();
randomName = temp_time + "." + end_point;
} else {
randomName = temp_time;
}
return randomName;
}

/**
* 上传附件
*
* @param path_name 需要将文件上传到什么目录
* @param attachment 需要上传的文件
* @param maxSize 文件的最大长度是多少(bite) 如果为-1表示没有限制
* @return
* @throws ApplicationException
* Created:2010-7-6
* @author:WKF28474
*/
public static String saveFile(String path_name, FormFile attachment,
int maxSize) throws Exception {
try {
if (attachment == null) {
throw new Exception("file is null"); // /file is null
}
// 验证是否内容为空
int attachment_size = attachment.getFileSize();
if (attachment_size == 0) {
throw new Exception("file's size is 0"); // file's size is 0
}
// 验证文件大小,如果maxSise=-1表示没有限制
if (maxSize != -1 && attachment_size > maxSize) {
throw new Exception("file is large"); // 文件大小限制
}

// 生成在服务器上的文件名(上传到服务器后的文件名是什么)
String attachment_name = attachment.getFileName();
// 获取文件名
String file_name = Test.getRandomName(attachment_name);
// 上传附件
uploadFile(attachment.getInputStream(), path_name, file_name);

// 返回生成的文件名即可
return file_name;
} catch (Exception e) {
throw new Exception("file upload faild", e);
}
}


/**
* 用流的方式上传文件 Notes:
*
* @param attachment
* @param path_name
* @param file_name
* @throws ApplicationException
* Created:2010-7-6
* @author:WKF28474
*/
public static void uploadFile(InputStream is, String path_name,
String file_name) throws Exception {
BufferedOutputStream outB = null;
BufferedInputStream inB = null;
// 生成目录和文件
File file = doBefore(path_name, file_name);
try {
outB = new BufferedOutputStream(new FileOutputStream(file));
inB = new BufferedInputStream(is);
// 循环上传数据
int count = 0;
int size = 1024;
byte[] b = new byte[size];
count = inB.read(b, 0, size);
if (count > 0) {
outB.write(b, 0, count);
}
while ((count = inB.read(b, 0, size)) > 0) {
outB.write(b, 0, count);
}
} catch (Exception e) {
throw new Exception("", e);
} finally {
try {
outB.close();
inB.close();
} catch (Exception e) {
throw new Exception("", e);
}
}
}

/**
* 创建文件 Notes:
*
* @param absolutePath
* @param sFileName
* @return
* @throws ApplicationException
* Created:2010-7-6
* @author:WKF28474
* @throws Exception
*/
public static File doBefore(String path_name, String file_name)
throws Exception {
File file = null;
if (!new File(path_name).isDirectory()) {
if (!new File(path_name).mkdirs()) {
throw new Exception("");
}
}
// 创建目标文件
try {
String fullFileName = Test.replaceWithAny(Test.replaceWithAny(
path_name, "\\", "/")
+ "/" + file_name, "//", "/");
file = new File(fullFileName);
if (!file.exists()) {
file.createNewFile();
} else {
throw new Exception("");
}
} catch (Exception e) {
throw new Exception("", e);
}
return file;
}

/**
* 删除文件和子文件
*
* @param dirFile
* @throws Exception
* Created:2010-8-19
* @author:WKF28474
*/
public static void forceDelDir(File dirFile) throws Exception {
if (dirFile.exists()) {
try {
File childFile[] = dirFile.listFiles();
for (int i = 0; i < childFile.length; i++) {
if (childFile[i].isDirectory()) {
forceDelDir(childFile[i]);
} else {
childFile[i].delete();
}
}
// 删除目录
dirFile.delete();
} catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
}
}

/**
* 读取指定目录下的Properties配置文件.
*
* @param resource
* @return Created:2011-1-5
* @author:zKF29923
*/
public static Properties getProperties(String resource) {
Properties properties = new Properties();
try {
properties.load(getStream(resource));
} catch (IOException e) {
throw new RuntimeException("couldn't load properties file '"
+ resource + "'", e);
}
return properties;
}

/**
* 读取指定目录下文件的输入流.
*
* @param resource
* @return
* Created:2011-1-5
* @author:zKF29923
*/
public static InputStream getStream(String resource) {
InputStream inputStream = Test.class.getClassLoader()
.getResourceAsStream(resource);

return inputStream;
}

public static String replaceWithAny(String sourceStr, String sFlag, String sReplace)
throws Exception {
StringBuffer sb = new StringBuffer();
sourceStr = sourceStr.trim();
for (int index = sourceStr.indexOf(sFlag); index != -1; index = sourceStr
.indexOf(sFlag)) {
sb.append(sourceStr.substring(0, index)).append(sReplace);
sourceStr = sourceStr.substring(index + sFlag.length()).trim();
}

sb.append(sourceStr);
return sb.toString();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值