HTML和JS官网有:https://www.layui.com/demo/upload.html
java函数可以用通用上传方式,我写了个文件记录表,所以写了个通用的,文件图片都可以,JS限制就可以了。
/** * 文件上传下载 操作处理 * * @author admin * @date Mon Oct 07 09:45:29 CST 2019 */ @Controller @RequestMapping("/system/webFiles") public class UploadDownloadController extends BaseController { private String prefix = "system/webFiles"; @Autowired private IWebPubFilesService webPubFilesService; private static final Logger log = LoggerFactory.getLogger(CommonController.class); /** * 单个文件上传 */ @PostMapping(value = "/upload") @ResponseBody public AjaxJson upload(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException { AjaxJson res = new AjaxJson(); FileUPMessage msg = new FileUPMessage(); if (!file.isEmpty()) { //原文件名 String fileName = file.getOriginalFilename(); //后缀名 String suffix = fileName.substring (fileName.lastIndexOf(".") + 1).toUpperCase(); //后缀名 String suffi = fileName.substring (0,fileName.lastIndexOf(".")).toUpperCase(); boolean isUpload = false; boolean isImg = false; //检查文件合法性 是否包含在UPFILESuffix中 for (UpFileSuffix suffixV : UpFileSuffix.values()) { String str = suffixV.name(); if (suffix.equals(str)) { isUpload = true; break; } } if (isUpload == false) { res.setSuccess(false); msg.setError("不是有效的文件" + fileName); res.put("msg", msg); return res; } //判断是否为图片 for (ImgFileSuffix suffixIMG : ImgFileSuffix.values ()) { String str = suffixIMG.name(); if (suffix.equals(str)) { isImg = true; break; } } String MID = request.getParameter("mid"); if (MID == null || MID.length() == 0) { MID = "0"; } int mid = Integer.valueOf(MID); String model = request.getParameter("model"); if (model == null || model.length() == 0) { model = "DEFAULT"; } String rootPath = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL; String rootPathThumb = Global.getUserfilesBaseDir() + Global.USERFILES_BASE_URL + "ThumbDEFAULT" + File.separator; String MOSOFILES_BASE_URL = rootPath; InputStream in = null; OutputStream out = null; try { String path = MOSOFILES_BASE_URL + "webupload" + File.separator + model + File.separator; String pathThumb = rootPathThumb + "webupload" + File.separator + model + File.separator; String FileURL = Global.USERFILES_BASE_URL + "webupload/" + model + "/"; System.out.println(path); System.out.println(FileURL); File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } File dirThumb = new File(pathThumb); if (!dirThumb.exists()) { dirThumb.mkdirs(); } /*String uuid = UUID.randomUUID().toString ().replace("-", "").toLowerCase();*/ //新文件名 String newFilename = suffi+"-"+ mid + "." + suffix; //新文件全地址 File serverFile = new File(dir.getAbsolutePath() + File.separator + newFilename); //压缩新文件全地址 String serverFileThumb = dirThumb.getAbsolutePath() + File.separator + newFilename; //创建文件记录对象 WebPubFiles webpubFiles = new WebPubFiles(); webpubFiles.setBeginCreateDate(new Date()); in = file.getInputStrea