后台接收前台传来的图片并保存在本地

贴代码

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	public String upLoadImg(MultipartFile file, HttpServletRequest request) throws IOException {
		String path = request.getSession().getServletContext().getRealPath("upload");
		String fileName = file.getOriginalFilename();// 获取到上传文件的名字
		// file.getSize();获取到上传文件的大小
		File dir = new File(path, fileName);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		// MultipartFile自带的解析方法
		file.transferTo(dir);
		String directory = dir.getCanonicalPath();
		System.out.println("directory:" + directory);
		return "/upload" + "/" + fileName;
	}

 

你可能感兴趣的:(java基础)