struts2实现文件上传的工具类

FileTool.java


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;

/**
 * 文件操作工具類
 * 
 * @author fengyapeng
 * @time: 2011-4-11 下午03:40:41
 * @Company:oddtech
 */
public class FileTool {

	private static int i;

	public static boolean copyFile(String resourceFimeName,
			String targetFileName) throws IOException {

		return copyFile(new File(resourceFimeName), new File(targetFileName));
	}

	/**
	 * 文件拷貝
	 * 
	 * @param resourceFimeName
	 *            源文件的路徑名稱
	 * @param targetFile
	 *            目的文件
	 * @return
	 * @throws IOException
	 */
	public static boolean copyFile(String resourceFimeName, File targetFile)
			throws IOException {
		return copyFile(new File(resourceFimeName), targetFile);
	}

	/**
	 * 文件拷貝
	 * 
	 * @param resourceFile
	 *            源文件
	 * @param targetFileName
	 *            目的文件的路徑名稱
	 * @return
	 * @throws IOException
	 */
	public static boolean copyFile(File resourceFile, String targetFileName)
			throws IOException {
		return copyFile(resourceFile, new File(targetFileName));
	}

	/**
	 * 文件拷貝
	 * 
	 * @param resourceFile
	 *            源文件
	 * @param targetFile
	 *            目的文件
	 * @return
	 * @throws IOException
	 */
	public static boolean copyFile(File resourceFile, File targetFile)
			throws IOException {
		if (resourceFile == null || targetFile == null)
			return false;

		if (resourceFile.exists()) {
			if (!targetFile.exists()) {
				File parentFile = targetFile.getParentFile();
				if (!parentFile.exists())
					parentFile.mkdirs();
				targetFile.createNewFile();
			}
			FileInputStream in = new FileInputStream(resourceFile);
			FileOutputStream out = new FileOutputStream(targetFile);
			byte[] buffer = new byte[1024 * 8];
			int i = 0;

			while ((i = in.read(buffer)) != -1) {
				out.write(buffer, 0, i);
			}
			out.flush();
			return true;

		} else {
			return false;
		}
	}

	/**
	 * 文件刪除,
	 * 
	 * @param fileName
	 *            文件的路徑
	 */
	public static void deleteFile(String fileName) {
		if (fileName != null) {
			deleteFile(new File(fileName));
		}
	}

	/**
	 * 文件刪除
	 * 
	 * @param file
	 *            文件
	 */
	public static void deleteFile(File file) {
		if (file != null && file.exists()) {
			System.out.println(file.delete());
		}
	}

	/**
	 * 按照當前時間命名上傳的文件名
	 */
	public static String getFileNameByTime() {
		Calendar date = Calendar.getInstance(Locale.CHINESE);
		StringBuffer fileName = new StringBuffer();
		if (i == 100)
			i = 0;
		if (i <= 9) {

			fileName.append(date.get(Calendar.YEAR))
					.append(date.get(Calendar.MONTH) + 1)
					.append(date.get(Calendar.DAY_OF_MONTH))
					.append(date.get(Calendar.HOUR_OF_DAY))
					.append(date.get(Calendar.MINUTE))
					.append(date.get(Calendar.MILLISECOND)).append("00")
					.append(i);
		} else {
			fileName.append(date.get(Calendar.YEAR))
					.append(date.get(Calendar.MONTH) + 1)
					.append(date.get(Calendar.DAY_OF_MONTH))
					.append(date.get(Calendar.HOUR_OF_DAY))
					.append(date.get(Calendar.MINUTE))
					.append(date.get(Calendar.MILLISECOND)).append("0")
					.append(i);
		}

		System.out.println(date.toString());
		return fileName.toString();

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值