controllers包写控制器:
@Controller @RequestMapping(value="/utils") public class UploadController{ /** * 上传文件 * @return * @throws IOException * @throws IllegalStateException */ @RequestMapping(value = "/uploadify", method = RequestMethod.POST) @ResponseBody public String upload(HttpServletRequest request, HttpServletResponse response){ String responseStr=""; MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; Map<String, MultipartFile> fileMap = multipartRequest.getFileMap(); // 文件保存路径 ctxPat本地路徑 String ctxPath=request.getSession().getServletContext().getRealPath("/")+File.separator+"uploadFiles"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM"); String ymd = sdf.format(new Date()); ctxPath += File.separator + ymd + File.separator; // 创建文件夹 File file = new File(ctxPath); if (!file.exists()) { file.mkdirs(); } String fileName = null; for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) { // 上传文件 MultipartFile mf = entity.getValue(); fileName = mf.getOriginalFilename(); String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase(); // 重命名文件 //SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); //String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt; //File uploadFile = new File(ctxPath + newFileName); /** * modify,文件原名 */ File uploadFile = new File(ctxPath + fileName); try { FileCopyUtils.copy(mf.getBytes(), uploadFile); responseStr="上传成功"; } catch (IOException e) { responseStr="上传失败"; e.printStackTrace(); } } return responseStr; } }
core包写Service接口
services包实现core中的接口,一般不写界面代码,主要和数据库打交道。
entity类是实体,类的字段