场景
上传文件后防止文件重名,需要获取当前时间并作为
时间戳给上传的文件重命名。
实现
if (file != null) {
//获取上传文件名
fileName = file1.getOriginalFilename();
//获取后缀名
String sname = fileName.substring(fileName.lastIndexOf("."));
//时间格式化格式
SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyyMMddHHmmssSSS");
//获取当前时间并作为时间戳
String timeStamp=simpleDateFormat.format(new Date());
//拼接新的文件名
String newName ="收货单"+timeStamp+sname;
//指定上传文件的路径
String path = "F:\\" + newName;
//上传保存
file.transferTo(new File(path));
//保存当前文件路径
request.getSession().setAttribute("currFilePath", path);
}