bug--修复本地文件上传中文问题

This commit is contained in:
taochengbo
2020-05-23 20:04:41 +08:00
parent 8184029061
commit 200b48dc4e

View File

@ -24,6 +24,7 @@ import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random;
/** /**
* File工具类扩展 hutool 工具包 * File工具类扩展 hutool 工具包
@ -140,13 +141,11 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
* 将文件名解析成文件的上传路径 * 将文件名解析成文件的上传路径
*/ */
public static File upload(MultipartFile file, String filePath) { public static File upload(MultipartFile file, String filePath) {
Date date = new Date(); //String name = getFileNameNoEx(file.getOriginalFilename());
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmssS");
String name = getFileNameNoEx(file.getOriginalFilename());
String suffix = getExtensionName(file.getOriginalFilename()); String suffix = getExtensionName(file.getOriginalFilename());
String nowStr = "-" + format.format(date); StringBuffer nowStr = fileRename();
try { try {
String fileName = name + nowStr + "." + suffix; String fileName = nowStr + "." + suffix;
String path = filePath + fileName; String path = filePath + fileName;
// getCanonicalFile 可解析正确各种路径 // getCanonicalFile 可解析正确各种路径
File dest = new File(path).getCanonicalFile(); File dest = new File(path).getCanonicalFile();
@ -370,4 +369,20 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
return accuracy; return accuracy;
} }
/**
* 上传文件重命名
* @return 新的文件名
*/
public static StringBuffer fileRename() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String time = sdf.format(new Date());
StringBuffer buf = new StringBuffer(time);
Random r = new Random();
//循环取得三个不大于10的随机整数
for (int x = 0; x < 3; x++) {
buf.append(r.nextInt(10));
}
return buf;
}
} }