Java tutorial
/* Copyright 2006 - 2010 Under Dusken Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package no.dusken.aranea.service; import com.twmacinta.util.MD5; import no.dusken.aranea.model.Image; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Required; import org.springframework.web.multipart.MultipartFile; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URL; import static org.apache.commons.lang.StringUtils.isBlank; /** * This is a factory used to create images (no.dusken.aranea.model.Image) from * various sources Uses jpedal to read pdf, http://www.jpedal.org/download.php. */ public class StoreImageServiceImpl implements StoreImageService { /** * The directory that images is saved relative to. */ private File imageDirectory; private ImageService imageService; private static final Logger log = LoggerFactory.getLogger(StoreImageServiceImpl.class); public Image createImage(URL url) throws IOException { File file = new File(imageDirectory + "/tmp/" + url.getFile()); FileUtils.copyURLToFile(url, file); return createImage(file); } public Image createImage(MultipartFile mFile) throws IOException { return changeImage(mFile, new Image()); } public Image changeImage(MultipartFile mFile, Image image) throws IOException { File file = new File(imageDirectory + "/tmp/" + mFile.getOriginalFilename()); log.debug("creating file {}", file.getAbsolutePath()); // ensure the needed directories are present: file.getParentFile().mkdirs(); mFile.transferTo(file); return changeImage(file, image); } public Image changeImage(File file, Image image) throws IOException { String hash = MD5.asHex(MD5.getHash(file)); if (isBlank(hash)) { throw new IOException("Could not get hash from " + file.getAbsolutePath()); } Image existingImage = imageService.getImageByHash(hash); if (existingImage != null) { log.info("Imported existing Image: {} from the user", existingImage.toString()); file.delete(); return existingImage; } else { image.setHash(hash); BufferedImage rendImage = ImageIO.read(file); image.setHeight(rendImage.getHeight()); image.setWidth(rendImage.getWidth()); image.setFileSize(file.length()); FileUtils.copyFile(file, new File(imageDirectory + "/" + image.getUrl())); file.delete(); log.debug("Imported Image: {} from {}", image.toString(), file.getAbsolutePath()); return imageService.saveOrUpdate(image); } } public Image createImage(File file) throws IOException { return changeImage(file, new Image()); } @Required @Autowired public void setImageService(ImageService imageService) { this.imageService = imageService; } @Required public void setImageDirectory(File imageDirectory) { this.imageDirectory = imageDirectory; } }