FileUploadUtil

package com.css.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUploadUtil {

 /**
  * 生成文件名
  *
  * @param sp1
  *            分隔前字符串
  * @param separator
  *            分隔符号
  * @param sp2
  *            分隔后字符串
  * @param fileExt
  *            文件扩展名
  * @return
  */
 public static String createFileName(String sp1, String separator,
   String sp2, String fileExt) {
  StringBuffer fileName = new StringBuffer();
  if (sp1 != null && !"".equals(sp1)) {
   fileName.append(sp1);
  }
  if (separator != null && !"".equals(separator)) {
   fileName.append(separator);
  }
  if (sp2 != null && !"".equals(sp2)) {
   fileName.append(sp2);
  }
  fileName.append(".");
  fileName.append(fileExt);
  return fileName.toString();
 }

 /**
  * 创建唯一文件名
  *
  * @return
  */
 public static String createFileName() {
  String dfile = getCurrentYMR("");
  String unique = createUnique();
  return dfile + "_" + unique;
 }

 /**
  * 创建唯一标识<br/> 描述:6位数 = 4位日期 + 2位随机
  *
  * @return
  */
 public static String createUnique() {
  String curTimeStr = String.valueOf(System.currentTimeMillis());// 获取系统当前时间
  curTimeStr = curTimeStr.substring(curTimeStr.length() - 4, curTimeStr
    .length());// 取系统的后4位数
  int randomNum = new java.util.Random().nextInt(90) + 10;// 随机生成2位数
  String randomNumStr = String.valueOf(randomNum);// 取系统的后4位数 + 随机生成2位数
  return curTimeStr + randomNumStr;
 }

 /**
  * 创建文件名
  *
  * @param imgSize
  *            图片大小
  * @param fileExt
  *            文件扩展名
  * @return
  */
 public static String createFileName(String imgSize, String fileExt) {
  String dfile = getCurrentYMR("");
  String unique = createUnique();
  return createFileName(dfile + "_" + unique, "_", imgSize, fileExt);
 }

 /**
  * 得到当前日期
  *
  * @param separator
  *            分隔符号
  * @return
  */
 public static String getCurrentYMR(String separator) {
  String s = separator == null ? "" : separator;
  String dfile = getYear() + s + getMonth() + s + getDay();
  return dfile;
 }

 /**
  * 年
  *
  * @return
  */
 public static String getYear() {
  Date date = new Date();
  return convertDateToString("yyyy", date);
 }

 /**
  * 月
  *
  * @return
  */
 public static String getMonth() {
  Date date = new Date();
  return convertDateToString("MM", date);
 }

 /**
  * 日
  *
  * @return
  */
 public static String getDay() {
  Date date = new Date();
  return convertDateToString("dd", date);
 }

 /**
  * 日期转换
  *
  * @param aMask
  * @param aDate
  * @return
  */
 public static String convertDateToString(String aMask, Date aDate) {
  SimpleDateFormat df = null;
  try {
   df = new SimpleDateFormat(aMask);
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return df.format(aDate);
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涂作权的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值