文件上传时生成“日期+随机数”式文件名前缀的Java代码

如果您喜欢这些文章,欢迎点击此处订阅本Blog

Blog 订阅


 文件上传时生成“日期+随机数”式文件名前缀的Java代码

 

代码实例:

package cn.edu.hbcit.ips.bean; import java.io.IOException; import java.io.PrintWriter; import java.util.*; import org.apache.log4j.Logger; /** * <p>Description: 文件操作</p> * * <p>Copyright: Copyright (c) 2009</p> * * <p>Company: 河北工业职业技术学院</p> * * @author 作者 : [email protected] * @version 创建时间:Feb 6, 2009 5:10:48 PM */ public class FileOperate { protected final Logger log = Logger.getLogger(FileOperate.class.getName()); public FileOperate() { // TODO Auto-generated constructor stub } /* * 生成随机文件名 */ public String generateRandomFilename(){ String RandomFilename = ""; Random rand = new Random();//生成随机数 int random = rand.nextInt(); Calendar calCurrent = Calendar.getInstance(); int intDay = calCurrent.get(Calendar.DATE); int intMonth = calCurrent.get(Calendar.MONTH) + 1; int intYear = calCurrent.get(Calendar.YEAR); String now = String.valueOf(intYear) + "_" + String.valueOf(intMonth) + "_" + String.valueOf(intDay) + "_"; log.debug("生成于今日的文件名前缀为:"+now); RandomFilename = now + String.valueOf(random > 0 ? random : ( -1) * random) + "."; return RandomFilename; } }




你可能感兴趣的:(java,String,calendar,Random,Constructor)