html5 java多图片上传

时间:2022-11-16 12:40:12
<form enctype="multipart/form-data" action="url"  method="post">
  <input type="file" name="pic" />
  <input type="file" name="pic" />
  <input type="file" name="pic" />
  <input type="submit" value="提交"/>
</form>


private
List<String> uploadImgs(HttpServletRequest req,String name) throws Exception{ List<String> list = new ArrayList<String>(); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req; // 获得文件: List<MultipartFile> files = multipartRequest.getFiles(name); for(MultipartFile file:files){ String fileExtName = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf(".") + 1); String realPath = req.getSession().getServletContext().getRealPath(""); String uuid = UUID.randomUUID().toString(); //String fileName = DateTimeUtil.getDateTime2Str3S4() + "." + fileExtName; String fileName = uuid + "." + fileExtName; String imgPath = "upload"+File.separatorChar+"appShootme" + File.separatorChar + fileName; String imgPathRealPath = realPath + File.separatorChar+ "upload"+File.separatorChar+"appShootme" + File.separatorChar + fileName; File fi = new File(imgPathRealPath); if (!fi.exists()) { fi.mkdirs(); } file.transferTo(fi); list.add(imgPath); } return list; }