Java tutorial
/* * The MIT License * * Copyright 2013 jdmr. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.sunflower.petal.controller; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.UUID; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import com.sunflower.petal.entity.Image; import com.sunflower.petal.service.ImageService; import org.apache.commons.io.IOUtils; import org.imgscalr.Scalr; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Import; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; /** * * @author jdmr */ @Controller @RequestMapping("/image") public class ImageController { private static final Logger log = LoggerFactory.getLogger(ImageController.class); @Value("${file.upload.directory}") private String fileUploadDirectory; @Autowired private ImageService imageService; // @Autowired // private ImageService imageService; // @Autowired // private ImageDao imageDao; @RequestMapping public String index() { log.debug("ImageController home"); return "image/index"; } @RequestMapping(value = "/upload", method = RequestMethod.GET) public @ResponseBody Map list() { log.debug("uploadGet called"); List<Image> list = imageService.list(); for (Image image : list) { image.setUrl("/picture/" + image.getId()); image.setThumbnailUrl("/thumbnail/" + image.getId()); image.setDeleteUrl("/delete/" + image.getId()); image.setDeleteType("DELETE"); } Map<String, Object> files = new HashMap<String, Object>(); files.put("files", list); log.debug("Returning: {}", files); return files; } @RequestMapping(value = "/upload", method = RequestMethod.POST) public @ResponseBody Map upload(MultipartHttpServletRequest request, HttpServletResponse response) { log.debug("uploadPost called"); Iterator<String> itr = request.getFileNames(); MultipartFile mpf; List<Image> list = new LinkedList<Image>(); while (itr.hasNext()) { mpf = request.getFile(itr.next()); log.debug("Uploading {}", mpf.getOriginalFilename()); String newFilenameBase = UUID.randomUUID().toString(); String originalFileExtension = mpf.getOriginalFilename() .substring(mpf.getOriginalFilename().lastIndexOf(".")); String newFilename = newFilenameBase + originalFileExtension; String storageDirectory = fileUploadDirectory; String contentType = mpf.getContentType(); File newFile = new File(storageDirectory + "/" + newFilename); try { mpf.transferTo(newFile); BufferedImage thumbnail = Scalr.resize(ImageIO.read(newFile), 290); String thumbnailFilename = newFilenameBase + "-thumbnail.png"; File thumbnailFile = new File(storageDirectory + "/" + thumbnailFilename); ImageIO.write(thumbnail, "png", thumbnailFile); Image image = new Image(); image.setName(mpf.getOriginalFilename()); image.setThumbnailFilename(thumbnailFilename); image.setNewFilename(newFilename); image.setContentType(contentType); image.setSize(mpf.getSize()); image = imageService.create(image); image.setUrl("/picture/" + image.getId()); image.setThumbnailUrl("/thumbnail/" + image.getId()); image.setDeleteUrl("/delete/" + image.getId()); image.setDeleteType("DELETE"); list.add(image); } catch (IOException e) { log.error("Could not upload file " + mpf.getOriginalFilename(), e); } } Map<String, Object> files = new HashMap<String, Object>(); files.put("files", list); return files; } @RequestMapping(value = "/picture/{id}", method = RequestMethod.GET) public void picture(HttpServletResponse response, @PathVariable Long id) { Image image = imageService.get(id); File imageFile = new File(fileUploadDirectory + "/" + image.getNewFilename()); response.setContentType(image.getContentType()); response.setContentLength(image.getSize().intValue()); try { InputStream is = new FileInputStream(imageFile); IOUtils.copy(is, response.getOutputStream()); } catch (IOException e) { log.error("Could not show picture " + id, e); } } @RequestMapping(value = "/thumbnail/{id}", method = RequestMethod.GET) public void thumbnail(HttpServletResponse response, @PathVariable Long id) { Image image = imageService.get(id); File imageFile = new File(fileUploadDirectory + "/" + image.getThumbnailFilename()); response.setContentType(image.getContentType()); response.setContentLength(image.getSize().intValue()); try { InputStream is = new FileInputStream(imageFile); IOUtils.copy(is, response.getOutputStream()); } catch (IOException e) { log.error("Could not show thumbnail " + id, e); } } @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE) public @ResponseBody List delete(@PathVariable Long id) { Image image = imageService.get(id); File imageFile = new File(fileUploadDirectory + "/" + image.getNewFilename()); imageFile.delete(); File thumbnailFile = new File(fileUploadDirectory + "/" + image.getThumbnailFilename()); thumbnailFile.delete(); imageService.delete(image); List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); Map<String, Object> success = new HashMap<String, Object>(); success.put("success", true); results.add(success); return results; } }