1 package org.springframework.samples.imagedb.web; 2 3 import javax.servlet.http.HttpServletRequest ; 4 import javax.servlet.http.HttpServletResponse ; 5 6 import org.springframework.samples.imagedb.ImageDatabase; 7 import org.springframework.web.multipart.MultipartFile; 8 import org.springframework.web.multipart.MultipartHttpServletRequest; 9 import org.springframework.web.servlet.ModelAndView; 10 import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 11 12 18 public class ImageController extends MultiActionController { 19 20 private ImageDatabase imageDatabase; 21 22 public void setImageDatabase(ImageDatabase imageDatabase) { 23 this.imageDatabase = imageDatabase; 24 } 25 26 public ModelAndView showImageList(HttpServletRequest request, HttpServletResponse response) throws Exception { 27 return new ModelAndView("imageList", "images", this.imageDatabase.getImages()); 28 } 29 30 public ModelAndView streamImageContent(HttpServletRequest request, HttpServletResponse response) throws Exception { 31 this.imageDatabase.streamImage(request.getParameter("name"), response.getOutputStream()); 32 return null; 33 } 34 35 public ModelAndView processImageUpload(HttpServletRequest request, HttpServletResponse response) throws Exception { 36 String name = request.getParameter("name"); 37 String description = request.getParameter("description"); 38 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; 39 MultipartFile multipartFile = multipartRequest.getFile("image"); 40 this.imageDatabase.storeImage(name, multipartFile.getInputStream(), (int) multipartFile.getSize(), description); 41 return new ModelAndView("redirect:imageList"); 42 } 43 44 public ModelAndView clearDatabase(HttpServletRequest request, HttpServletResponse response) throws Exception { 45 this.imageDatabase.clearDatabase(); 46 return new ModelAndView("redirect:imageList"); 47 } 48 49 } 50 | Popular Tags |