@RequestMapping("/upload_file_json")
public Object upload_media(StandardMultipartHttpServletRequest req, HttpServletResponse rsp) {
try {
String dirName = Objects.toString(req.getParameter("dir"), "image");
Subject subject = SecurityUtils.getSubject();
Session session = subject.getSession();
String currCompany = (String) session.getAttribute("currCompany");
if(AppUtils.isBlank(currCompany)){
ModelAndView errorMv = new ModelAndView("/redirect:/403");
errorMv.addObject("message", "缺少公司慘數");
return errorMv;
}
//文件保存目錄路徑
//rootPath/dirPath/dirName/ymdPath/fileName
String ymdPath = DateUtils.formatDate(new Date(), "yyyy_MM_dd") + "/";
String dirPath = "/" + currCompany + "/";
String rootPath = FileConfigUtils.getImagesSavePath();
String os = System.getProperty("os.name");
if (os.toLowerCase().startsWith("linux")) {
//rootPath = SystemManager.inst().upload_path;
}
String filePath = dirPath + dirName + "/" + ymdPath;
/新文件名字的命名規則*/
Function<String, String> nameFunc = fileName -> {
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();//檢查後綴名
String newFileName = IDManager.generateID();
return newFileName + "." + fileExt;
};
rsp.setContentType("text/html; charset=UTF-8");
if (!ServletFileUpload.isMultipartContent(req)) {
return getError("請選擇文件。");
}
//檢查目錄
File uploadDir = new File(rootPath);
//檢查目錄
/* if(!uploadDir.isDirectory()){
System.out.println("上傳目錄不存在。");
return getError("上傳目錄不存在.");
}*/
//檢查目錄寫權限
if (!uploadDir.canWrite()) {
System.out.println("上傳目錄沒有寫權限。");
return getError("上傳目錄沒有寫權限。");
}
List<String> url = upload_multi_file(rootPath, filePath, nameFunc, req);
System.out.println("************富文本框上傳圖片地址************" + url);
JSONObject ret = new JSONObject();
ret.put("error", 0);
ret.put("url", url);
return ret;
} catch (Exception e) {
e.printStackTrace();
return getError("未知錯誤");
}
}
private String getError(String message) {
JSONObject ret = new JSONObject();
ret.put("error", 1);
ret.put("url", message);
return ret.toJSONString();
}
public List<String> upload_multi_file(String saveRootPath, String filePath, Function<String, String> nameFunc, StandardMultipartHttpServletRequest freq) {
StopWatch stopWatch = new StopWatch("upfile");
stopWatch.start("save file");
String saveUrl = "/" + filePath;
String realpath = saveRootPath + filePath;
File dir = new File(realpath);
System.out.println("exists = "+dir.exists());
if (!dir.exists()) {
dir.mkdirs();
}
//String file_url[] = new String[1];
List<String> urlList = new ArrayList<>();
try {
freq.getMultiFileMap().values().stream().forEach(files -> {
files.stream().forEach((MultipartFile file) -> {
try {
String fileName = file.getOriginalFilename();
if (AppUtils.isBlank(fileName)) {
System.out.println("upload failed because of nofiles fileName=" + fileName);
return;
}
String _new_file_original_name = fileName;
if (null != nameFunc) {
_new_file_original_name = nameFunc.apply(fileName);
} else {
_new_file_original_name = fileName;
}
File newFile = new File(dir, _new_file_original_name);
file.transferTo(newFile);
//file_url[0] = saveUrl + newFile.getName();
urlList.add(FileConfigUtils.getImagesServerUrl()+saveUrl + newFile.getName());
} catch (IOException e) {
e.printStackTrace();
}
});
});
} catch (Exception e2) {
e2.printStackTrace();
return urlList;
}
stopWatch.stop();
return urlList;
}
}
————————————————
版权声明:本文为CSDN博主「choiHingLeung」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u011562082/article/details/87256083