請求方面: @RequestMapping(value = "/fileUpload", method = RequestMethod.POST) public String upload(MultipartHttpServletRequest request, HttpServletResponse response){ String s = fileUpdateUtil.fileUpload(request, response); try { String referer = request.getHeader("referer"); Pattern p = Pattern.compile("([a-z]*:(//[^/?#]+)?)?", Pattern.CASE_INSENSITIVE); Matcher mathcer = p.matcher(referer); if (mathcer.find()) { String callBackPath = mathcer.group();// 請求來源a.com MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile imgFile = multipartRequest.getFile("fileupload");//k4中fileupload //JsonObject json =本身的處理imgFile // 同域時直接返回json.toString()便可無需redirect String url = "redirect:" + callBackPath + "/redirect?s=" + s + "#" + s; return url; } else { System.out.println("upload referer not find"); } } catch (Exception e) { System.out.println("upload error"+e); } return null; }
工具類: public static String fileUpload(MultipartHttpServletRequest request, HttpServletResponse response){ String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();//獲取項目絕對路徑 String substring = rootPath.substring(1, rootPath.length()).replace("/", "\\"); String savePath = substring + "static\\attached\\"; // 文件保存目錄URL // 定義容許上傳的文件擴展名 HashMap<String, String> extMap = new HashMap<String, String>(); extMap.put("image", "gif,jpg,jpeg,png,bmp"); extMap.put("flash", "swf,flv"); extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb"); extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2"); // 最大文件大小 long maxSize = 100000000; response.reset(); response.setHeader("P3P","CP=CAO PSA OUR"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); // writer.println(json); //想辦法把map轉成json if (!ServletFileUpload.isMultipartContent(request)) { System.out.println("請選擇文件。"); return null; } File uploadDir = new File(savePath); if (!uploadDir.exists()) { uploadDir.mkdirs(); } // 檢查目錄寫權限 if (!uploadDir.canWrite()) { System.out.println("上傳目錄沒有寫權限。"); return null; } String dirName = request.getParameter("dir"); if (dirName == null) { dirName = "image"; } if (!extMap.containsKey(dirName)) { System.out.println("目錄名不正確。"); return null; } // 建立文件夾 savePath += dirName + "/"; File saveDirFile = new File(savePath); if (!saveDirFile.exists()) { saveDirFile.mkdirs(); } SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String ymd = sdf.format(new Date()); savePath += ymd + "/"; File dirFile = new File(savePath); if (!dirFile.exists()) { dirFile.mkdirs(); } List<MultipartFile> files = request.getFiles("imgFile"); Iterator<MultipartFile> itr = files.iterator(); while (itr.hasNext()) { MultipartFile item = itr.next(); String fileName = item.getOriginalFilename(); // 檢查文件大小 if (item.getSize() > maxSize) { System.out.println("上傳文件大小超過限制。"); return null; } // 檢查擴展名 String fileExt = fileName.substring( fileName.lastIndexOf(".") + 1).toLowerCase(); if (!Arrays.<String>asList(extMap.get(dirName).split(",")) .contains(fileExt)) { System.out.println("上傳文件擴展名是不容許的擴展名。只容許" + extMap.get(dirName) + "格式。"); return null; } SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt; try { File uploadedFile = new File(savePath, newFileName); item.transferTo(uploadedFile); } catch (Exception e) { System.out.println("上傳文件失敗"); } //http://192.168.100.157:8080/attached/image/20190923/20190923094646_10.png int fast = savePath.indexOf("static")+6; String savePatha = savePath.substring(fast, savePath.length()); String replace = savePatha.replace("\\", "/"); String path = "http://192.168.100.157:8080"+replace + newFileName; String url = "{\"error\":0,\"url\":\""+path+"\"}"; return url; } return null; }
都是整合別的博主的,還有其餘的一些,後面補充html