java spring mvc上传

CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
				request.getSession().getServletContext());

		// 检查form是否有enctype="multipart/form-data"
		if (multipartResolver.isMultipart(request)) {
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;

			Iterator<String> iter = multiRequest.getFileNames();
			while (iter.hasNext()) {

				// 由CommonsMultipartFile继承而来,拥有上面的方法.
				MultipartFile file = multiRequest.getFile(iter.next());
				if (file != null) {
					imgUrl = file.getOriginalFilename();
					String path = PropertiesUtil
							.getProperty("article_img_path") + imgUrl;
	
					File localFile = new File(path);
					file.transferTo(localFile);
				}

			}
		}

 

你可能感兴趣的:(spring mvc)