新增文件接口
This commit is contained in:
@@ -9,6 +9,7 @@ import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.type.filter.TypeFilter;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.annotation.Annotation;
|
||||
@@ -181,4 +182,33 @@ public class AppTools {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 保存上传的文件
|
||||
*
|
||||
* @param path 路径
|
||||
* @param file 文件
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static String createFile(String path, MultipartFile file, String newFileName) throws Exception {
|
||||
String savePath = new File("").getAbsolutePath() + File.separator + "html" + File.separator + path;
|
||||
File servicePath = new File(savePath);
|
||||
if (!servicePath.exists()) {
|
||||
servicePath.mkdirs();
|
||||
}
|
||||
String fileName = file.getOriginalFilename();
|
||||
if (newFileName != null) {
|
||||
fileName = newFileName;
|
||||
}
|
||||
File saveFile = new File(savePath + "/" + fileName);
|
||||
if (saveFile.exists()) {
|
||||
if (!saveFile.delete()) {
|
||||
saveFile = new File(savePath + "/" + fileName.replace(".", "_" + new Date().getTime() + "."));
|
||||
}
|
||||
}
|
||||
file.transferTo(saveFile);
|
||||
fileName = URLEncoder.encode(fileName, "UTF-8");
|
||||
System.out.println("上传文件保存路径:" + saveFile.getAbsolutePath());
|
||||
return path + fileName;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user