You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.9 KiB

  1. /**
  2. * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package cn.afterturn.easypoi.cache;
  17. import java.awt.image.BufferedImage;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.InputStream;
  20. import javax.imageio.ImageIO;
  21. import org.apache.poi.util.IOUtils;
  22. import cn.afterturn.easypoi.cache.manager.POICacheManager;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. /**
  26. * 图片缓存处理
  27. *
  28. * @author JueYue
  29. * 2016年1月8日 下午4:16:32
  30. */
  31. public class ImageCache {
  32. private static final Logger LOGGER = LoggerFactory
  33. .getLogger(ImageCache.class);
  34. public static byte[] getImage(String imagePath) {
  35. InputStream is = POICacheManager.getFile(imagePath);
  36. ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
  37. try {
  38. BufferedImage bufferImg = ImageIO.read(is);
  39. ImageIO.write(bufferImg,
  40. imagePath.substring(imagePath.lastIndexOf(".") + 1, imagePath.length()),
  41. byteArrayOut);
  42. return byteArrayOut.toByteArray();
  43. } catch (Exception e) {
  44. LOGGER.error(e.getMessage(), e);
  45. return null;
  46. } finally {
  47. IOUtils.closeQuietly(is);
  48. IOUtils.closeQuietly(byteArrayOut);
  49. }
  50. }
  51. }