iOS系统上传图片,图片旋转解决方法:


图片exif信息相关包:
           <dependency>
             <groupId>com.drewnoakes</groupId>
             <artifactId>metadata-extractor</artifactId>
             <version>2.10.1</version>
           </dependency>

package com.freecg.green007.map.utils;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.exif.ExifDirectoryBase;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ImageUtils {
    //计算缩放比
    private static int getScale(Long size){
        Long defaultSize=1024*1024L;
        int scale=1;
        if(size>defaultSize){
                scale=Math.round(size/defaultSize);
        }
        return scale;
    }

    /**
     * 翻转图片
     * @param src
     * @param angel
     * @return
     */
     public static BufferedImage rotateImage(Image src, int angel) { 
        int src_width = src.getWidth(null); 
        int src_height = src.getHeight(null); 
        // calculate the new image size 
        Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(src_width, src_height)), angel); 
        BufferedImage res = null; 
        res = new BufferedImage(rect_des.width, rect_des.height,BufferedImage.TYPE_INT_RGB); 
        Graphics2D g2 = res.createGraphics(); 
        // transform 
        g2.translate((rect_des.width - src_width) / 2, 
                (rect_des.height - src_height) / 2); 
        g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2); 

        g2.drawImage(src, null, null); 
        return res; 
  } 

    /**
    * 计算旋转参数
    */ 
    public static Rectangle CalcRotatedSize(Rectangle src,int angel){ 
        // if angel is greater than 90 degree,we need to do some conversion. 
        if(angel > 90){ 
            if(angel / 9%2 ==1){ 
                int temp = src.height; 
                src.height = src.width; 
                src.width = temp; 
            } 
            angel = angel % 90; 
        } 

        double r = Math.sqrt(src.height * src.height + src.width * src.width ) / 2 ; 
        double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r; 
        double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;   
        double angel_dalta_width = Math.atan((double) src.height / src.width);   
        double angel_dalta_height = Math.atan((double) src.width / src.height);   

        int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha   
                - angel_dalta_width));   
        int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha   
                - angel_dalta_height));   
        int des_width = src.width + len_dalta_width * 2;   
        int des_height = src.height + len_dalta_height * 2;   
        return new java.awt.Rectangle(new Dimension(des_width, des_height));   
    } 

    /**
     * 硕正图片后缩放+裁剪
     *@param datas 源文件
     *@param resizeFile 缩放文件保存路径
     *@param cutFile 裁剪文件保存路径
     *@param newWidth 图片宽度
     *@param maxWidth,maxHeight 裁剪最大宽高
     *@param quality 图片质量 0-1之间
     * */
    public static void masterResize(byte[] datas, File resizeFile,File cutFile,int newWidth,int maxWidth,int maxHeight,float quality) throws IOException { 
        ByteArrayInputStream bi = new ByteArrayInputStream(datas);
        Metadata metadata=null;
        //获取图片
        ByteArrayInputStream in = new ByteArrayInputStream(datas);    //将b作为输入流;
        BufferedImage image = ImageIO.read(in);     //将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();   
            try {
                metadata = ImageMetadataReader.readMetadata(bi);

                for (ExifIFD0Directory  directory : metadata.getDirectoriesOfType(ExifIFD0Directory.class)) {
                    int orientation = 0;
                       // Exif信息中方向   
                    int angel = 0;//旋转角度
                    try {
                        orientation = directory.getInt(ExifDirectoryBase.TAG_ORIENTATION);
                        switch (orientation) {
                        case 3:
                            angel=180;
                            break;
                        case 6:
                            angel=90;
                            break;
                        case 8:
                            angel=270;
                            break;
                        default:
                            angel=0;
                            break;
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            if(angel!=0) image = rotateImage(image, angel);
         }
        resize(image, resizeFile,newWidth, quality);
        crop(image, new FileOutputStream(cutFile), maxWidth, maxHeight, false);
        } catch (ImageProcessingException | MetadataException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }



    /**
     * 硕正图片后裁剪
     *@param datas 源文件
     *@param filePath 文件保存路径
     *@param newWidth 图片宽度
     *@param quality 图片质量 0-1之间
     * */
    public static void masterCut(byte[] datas, File filePath, int maxWidth,int maxHeight) throws IOException { 
        ByteArrayInputStream bi = new ByteArrayInputStream(datas);
        Metadata metadata=null;
            try {
                metadata = ImageMetadataReader.readMetadata(bi);
                for (ExifIFD0Directory  directory : metadata.getDirectoriesOfType(ExifIFD0Directory.class)) {
                       // Exif信息中方向   
                      int orientation = directory.getInt(ExifDirectoryBase.TAG_ORIENTATION);
                      int angel = 0;//旋转角度
                      switch (orientation) {
                            case 3:
                                angel=180;
                                break;
                            case 6:
                                angel=90;
                                break;
                            case 8:
                                angel=270;
                                break;
                            default:
                                angel=0;
                                break;
                        }

                       //获取图片
            ByteArrayInputStream in = new ByteArrayInputStream(datas);    //将b作为输入流;
            BufferedImage image = ImageIO.read(in);     //将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();   
            BufferedImage des = rotateImage(image, angel);
            crop(des, new FileOutputStream(filePath), maxWidth, maxHeight, false);
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

    public static void resize(Image i, File filePath, int newWidth, float quality) throws IOException { 
          Image resizedImage = null; 
      int iWidth = i.getWidth(null); 
      int iHeight = i.getHeight(null); 
      if (iWidth > iHeight) { 
          resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH); 
      } else { 
          resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,newWidth, Image.SCALE_SMOOTH); 
      } 
      // This code ensures that all the pixels in the image are loaded. 
      Image temp = new ImageIcon(resizedImage).getImage(); 
      // Create the buffered image. 
      BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),temp.getHeight(null), BufferedImage.TYPE_INT_RGB); 
      // Copy image to buffered image. 
      Graphics g = bufferedImage.createGraphics(); 
      // Clear background and paint the image. 
      g.setColor(Color.white); 
      g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null)); 
      g.drawImage(temp, 0, 0, null); 
      g.dispose(); 

      // Soften. 
      float softenFactor = 0.05f; 
      float[] softenArray = { 0, softenFactor, 0, softenFactor, 
              1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 }; 
      Kernel kernel = new Kernel(3, 3, softenArray); 
      ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); 
      bufferedImage = cOp.filter(bufferedImage, null); 
      // Write the jpeg to a file. 
      FileOutputStream out = new FileOutputStream(filePath); 
      // Encodes image as a JPEG data stream 
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
      JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage); 
      param.setQuality(quality, true); 
      encoder.setJPEGEncodeParam(param); 
      encoder.encode(bufferedImage); 
    }


    /**
     *图片按照原比例缩放
     *@param datas 源文件
     *@param filePath 文件保存路径
     *@param newWidth 图片宽度
     *@param quality 图片质量 0-1之间
     * */
    @SuppressWarnings("restriction")
    public static void resize(byte[] datas, File filePath, int newWidth, float quality) throws IOException { 
            if (quality > 1) {throw new IllegalArgumentException( 
                "Quality has to be between 0 and 1"); 
            } 
            ImageIcon ii = new ImageIcon(datas); 
            Image i = ii.getImage(); 


            Image resizedImage = null; 
            int iWidth = i.getWidth(null); 
            int iHeight = i.getHeight(null); 
            if (iWidth > iHeight) { 
                resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH); 
            } else { 
                resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,newWidth, Image.SCALE_SMOOTH); 
            } 
            // This code ensures that all the pixels in the image are loaded. 
            Image temp = new ImageIcon(resizedImage).getImage(); 
            // Create the buffered image. 
            BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),temp.getHeight(null), BufferedImage.TYPE_INT_RGB); 
            // Copy image to buffered image. 
            Graphics g = bufferedImage.createGraphics(); 
            // Clear background and paint the image. 
            g.setColor(Color.white); 
            g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null)); 
            g.drawImage(temp, 0, 0, null); 
            g.dispose(); 

            // Soften. 
            float softenFactor = 0.05f; 
            float[] softenArray = { 0, softenFactor, 0, softenFactor, 
                    1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 }; 
            Kernel kernel = new Kernel(3, 3, softenArray); 
            ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); 
            bufferedImage = cOp.filter(bufferedImage, null); 
            // Write the jpeg to a file. 
            FileOutputStream out = new FileOutputStream(filePath); 
            // Encodes image as a JPEG data stream 
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 
            JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage); 
            param.setQuality(quality, true); 
            encoder.setJPEGEncodeParam(param); 
            encoder.encode(bufferedImage); 
        } // Example usage 




         /**
         * 图片缩放
         * @param in 输入流
         * @param filePath 图片保存路径
         * @param height 高度
         * @param width 宽度
         * @param isFilling 比例不对时是否需要补白 isFilling true 补白
         */
        public static void resize(InputStream in,File filePath,int width,int height,boolean isFilling){
              try {
             double ratio = 0.0; // 缩放比例
             BufferedImage bi = ImageIO.read(in);
             Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
             // 计算比例
             if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
                 if (bi.getHeight() > bi.getWidth()) { ratio = (new Integer(height)).doubleValue()/ bi.getHeight();
                 } else { ratio = (new Integer(width)).doubleValue() / bi.getWidth();}
                 AffineTransformOp op = new AffineTransformOp(AffineTransform
                         .getScaleInstance(ratio, ratio), null);
                 itemp = op.filter(bi, null);
             }
             if (isFilling) {//补白
                 BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
                 Graphics2D g = image.createGraphics();
                 g.setColor(Color.white);
                 g.fillRect(0, 0, width, height);
                 if (width == itemp.getWidth(null))
                     g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,itemp.getWidth(null), itemp.getHeight(null),Color.white, null);
                 else g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
                 g.dispose();
                 itemp = image;
             }
             ImageIO.write(toBufferedImage(itemp), "JPEG", filePath);
         } catch (IOException e) {
             e.printStackTrace();
         }
      }


        /**图片处理成为固定大小*/
/*        public static void fixedSize(InputStream in,File filePath,int width,int height){
              try {
             double ratio = 0.0; // 缩放比例
             BufferedImage bi = ImageIO.read(in);
             Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);

             BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
             Graphics2D g = image.createGraphics();
             g.setColor(Color.white);
             g.fillRect(0, 0, width, height);
             g.drawImage(itemp,0,0)

             if (width == itemp.getWidth(null))
                 g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,itemp.getWidth(null), itemp.getHeight(null),Color.white, null);
             else g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,itemp.getWidth(null), itemp.getHeight(null), Color.white, null);

             g.dispose();
             itemp = image;

             AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
             itemp = op.filter(bi, null);
             ImageIO.write(toBufferedImage(itemp), "JPEG", filePath);
         } catch (IOException e) {
             e.printStackTrace();
         }
      } */

          /**
       * 给图片添加文字水印
       * @param pressText 水印文字
       * @param srcImageFile 源图像地址
       * @param destImageFile 目标图像地址
       * @param fontName 水印的字体名称
       * @param fontStyle 水印的字体样式
       * @param color 水印的字体颜色
       * @param fontSize 水印的字体大小
       * @param x 修正值
       * @param y 修正值
       * @param alpha 透明度:alpha 必须是范围 [0.0, 1.0] 之内(包含边界值)的一个浮点数字
       */
      public final static void pressText(String pressText,
                      InputStream in, String destImageFile, String fontName,
              int fontStyle, Color color, int fontSize,int x,
              int y, float alpha) {
          try {
              Image src = ImageIO.read(in);
              int width = src.getWidth(null);
              int height = src.getHeight(null);
              BufferedImage image = new BufferedImage(width, height,
                      BufferedImage.TYPE_INT_RGB);
              Graphics2D g = image.createGraphics();
              g.drawImage(src, 0, 0, width, height, null);
              g.setColor(color);
              g.setFont(new Font(fontName, fontStyle, fontSize));
              g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,
                      alpha));
              // 在指定坐标绘制水印文字
              g.drawString(pressText, (width - (getLength(pressText) * fontSize))
                      / 2 + x, (height - fontSize) / 2 + y);
              g.dispose();
              ImageIO.write(toBufferedImage(image), "JPEG", new File(destImageFile));// 输出到文件流
          } catch (Exception e) {
              e.printStackTrace();
          }
      }


      /**
       * 裁剪图片(裁剪为正方形)
       *
       * **/
      public static void resize(InputStream in, File target,int max) throws Exception {
            InputStream input = new BufferedInputStream(in);
            OutputStream output = new BufferedOutputStream(new FileOutputStream(target));
            BufferedImage image = ImageIO.read(input);
                  crop(image, output,max,max,StringUtils.equalsIgnoreCase("png", FilenameUtils.getExtension(target.getName())));
         }

      /**
       * 裁剪图片(裁剪为正方形)
       *
       * **/
      public static void resize(byte[] in, File target,int max) throws Exception {
           ByteArrayInputStream bi = new ByteArrayInputStream(in);    //将b作为输入流;
             OutputStream output = new BufferedOutputStream(new FileOutputStream(target));
           BufferedImage image = ImageIO.read(bi);     //将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream();
              crop(image, output,max,max,false);
        }

      public static void crop(BufferedImage buffer, OutputStream output,int maxWidth,int maxHeight, boolean isPNG) throws Exception {
        try {
              int tmpWidth = buffer.getWidth();
              int tmpHeight = buffer.getHeight();
              int toWidth=tmpWidth;
              int toHeight=tmpHeight;
              if(toWidth>maxWidth && toHeight>maxHeight){
                   double rate =new BigDecimal(toWidth).divide(new BigDecimal(toHeight),2, BigDecimal.ROUND_HALF_UP).doubleValue() ;
                   if(maxWidth > 0 && toWidth > maxWidth){
                                toWidth = maxWidth;
                       toHeight = (int) (toWidth / rate);
                   }
                   if (maxHeight > 0 && toHeight > maxHeight) {
                       toHeight = maxHeight;
                       toWidth = (int) (toHeight * rate);
                 }
                   BufferedImage tmpBuffer= new BufferedImage(toWidth, toHeight, isPNG ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);   
                   tmpBuffer.getGraphics().drawImage(buffer.getScaledInstance(toWidth, toHeight, Image.SCALE_SMOOTH), 0, 0, null);
                   buffer=tmpBuffer;
              }
                BufferedImage dest =null;
                  if(toWidth>toHeight) dest = buffer.getSubimage((toWidth-toHeight)/2, 0, toHeight, toHeight);
                  else dest = buffer.getSubimage(0, (toHeight-toWidth)/2, toWidth, toWidth);
              BufferedImage tag = new BufferedImage(Math.min(toWidth, toHeight), Math.min(toWidth, toHeight), isPNG ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
              tag.getGraphics().drawImage(dest, 0, 0, null);
              ImageIO.write(tag, isPNG ? "png" : "jpg", output);
              // Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              IOUtils.closeQuietly(output);
          }
      }


      /**
       * 计算text的长度(一个中文算两个字符)
       * @param text
       * @return
       */
      public final static int getLength(String text) {
          int length = 0;
          for (int i = 0; i < text.length(); i++) {
              if (new String(text.charAt(i) + "").getBytes().length > 1) {
                  length += 2;
              } else {
                  length += 1;
              }
          }
          return length / 2;
      }


         /**
       * 图像类型转换:GIF->JPG、GIF->PNG、PNG->JPG、PNG->GIF(X)、BMP->PNG
       * @param srcImageFile 源图像地址
       * @param formatName 包含格式非正式名称的 String:如JPG、JPEG、GIF等
       * @param destImageFile 目标图像地址
       */
      public final static void convert(String srcImageFile, String formatName, String destImageFile) {
          try {
              File f = new File(srcImageFile);
              f.canRead();
              f.canWrite();
              BufferedImage src = ImageIO.read(f);
              ImageIO.write(src, formatName, new File(destImageFile));
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
      public static BufferedImage toBufferedImage(Image image) {
          if (image instanceof BufferedImage) {
                      return (BufferedImage)image;
           }
           image = new ImageIcon(image).getImage();
           BufferedImage bimage = null;
           GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
          try {
              int transparency = Transparency.OPAQUE;
               GraphicsDevice gs = ge.getDefaultScreenDevice();
               GraphicsConfiguration gc = gs.getDefaultConfiguration();
               bimage = gc.createCompatibleImage(
               image.getWidth(null), image.getHeight(null), transparency);
           } catch (HeadlessException e) {
              // The system does not have a screen
           }
          if (bimage == null) {
              // Create a buffered image using the default color model
              int type = BufferedImage.TYPE_INT_RGB;
              //int type = BufferedImage.TYPE_3BYTE_BGR;//by wang
              /*if (hasAlpha) {
               type = BufferedImage.TYPE_INT_ARGB;
               }*/
               bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
           }

          // Copy image to buffered image
           Graphics g = bimage.createGraphics();
          // Paint the image onto the buffered image
           g.drawImage(image, 0, 0, null);
           g.dispose();
          return bimage;
      }

}



package com.freecg.green007.map.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传工具类
 */
public class UploadUtils {
    private static Logger logger=LoggerFactory.getLogger(UploadUtils.class);
    // 最大文件大小
    private static long maxSize = 8*1024*1024;
    // 定义允许上传的文件扩展名
    private static Map<String, String> extMap = new HashMap<String, String>();
    // 文件保存目录相对路径
    private static String basePath ="static/uploadfiles";
    // 文件的目录名
    private static String dirName = "images";
    private static  String UPLOAD_CODE="files_code";
    private static  String UPLOAD_MESSAGE="files_message";
    //图片默认宽高
    private static int IMAGE_DEFAULT_WIDTH=640;
    private static int IMAGE_DEFAULT_HEIGHT=640;
    //默认后缀名
    private static String IMAGE_DEFAULT_EXT=".jpg";
    //图片处理值
    private static long IMAGE_SIZE_DO=1024L;
    //文件后缀
    private static String IMAGE_SUFFIX_SCALE = "_w1";
    private static String IMAGE_SUFFIX_BLANK = "_w2";
    private static String IMAGE_SUFFIX_ORIGINAL = "_w3";


    static {
          // 其中images,flashs,medias,files,对应文件夹名称,对应dirName
          // key文件夹名称
          // value该文件夹内可以上传文件的后缀名
          extMap.put("images", "gif,jpg,jpeg,png,bmp");
          extMap.put("flashs", "swf,flv");
          extMap.put("medias", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
          extMap.put("files", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
    }
    public static Map<String, Object> imageUpload(HttpServletRequest request,MultipartFile file){
            Map<String, Object> result=new HashMap<>();
              if(null==file){
                    result.put(UPLOAD_CODE, 404);  //上传图片不能为空
          }else if(file.getSize()<=0 || file.getSize()>=maxSize){
                result.put(UPLOAD_CODE, 405);  //上传图片大小超过限制
          }else{
                  String fileName=file.getOriginalFilename();
                  //获取后缀名
                  String fileExt= fileName.substring(fileName.lastIndexOf("."));
                  if(!Arrays.<String> asList(extMap.get(dirName).split(",")).contains(fileExt)){
                       //添加内容
                       String newFilename=(UUID.randomUUID().toString());
                       String uploadPath=request.getSession().getServletContext().getRealPath("/") + basePath + "/images/"+new SimpleDateFormat("yyyyMMdd").format(new Date())+"/";
                       File destFile=new File(uploadPath);
                       if(!destFile.exists()) destFile.mkdirs();
                       try {
                           long start=System.currentTimeMillis();
                           //缩放
                            ImageUtils.resize(file.getBytes(), new File(uploadPath+newFilename+IMAGE_SUFFIX_SCALE+IMAGE_DEFAULT_EXT), IMAGE_DEFAULT_WIDTH, 0.8f); //缩放
                            System.out.println("缩放执行耗时 : "+(System.currentTimeMillis()-start)/1000f+" 秒 ");
                            start=System.currentTimeMillis();
                            //裁剪
                         ImageUtils.resize(file.getInputStream(), new File(uploadPath+newFilename+IMAGE_SUFFIX_BLANK+IMAGE_DEFAULT_EXT), IMAGE_DEFAULT_WIDTH);
                         System.out.println("裁剪执行耗时 : "+(System.currentTimeMillis()-start)/1000f+" 秒 ");
                         //缩放+原图
                         start=System.currentTimeMillis();
                           file.transferTo(new File(uploadPath+newFilename+IMAGE_SUFFIX_ORIGINAL+IMAGE_DEFAULT_EXT));//原图
                           System.out.println("原图执行耗时 : "+(System.currentTimeMillis()-start)/1000f+" 秒 ");
                           result.put(UPLOAD_CODE, 200);
                       result.put(UPLOAD_MESSAGE, request.getContextPath() + "/" + basePath + "/images/"+new SimpleDateFormat("yyyyMMdd").format(new Date())+"/"+newFilename);
                        } catch (Exception e) {
                                logger.error(e.getMessage());
                                e.printStackTrace();
                                result.put(UPLOAD_CODE, 500);
                        }
            }else{
                    result.put(UPLOAD_CODE, 406);  //图片不符合要求
            }
          }    
              return result;
      }

    /**
     * byte 转 
     * */
    public static Map<String, Object> imageUpload2(HttpServletRequest request,byte[] file){
      Map<String, Object> result=new HashMap<>();
        if(null==file){ result.put(UPLOAD_CODE, 404);  //上传图片不能为空
        }else if(file.length<=0 || file.length>=maxSize){
            result.put(UPLOAD_CODE, 405);  //上传图片大小超过限制
        }else{
                 //添加内容
                 String newFilename=(UUID.randomUUID().toString());
                 String uploadPath=request.getSession().getServletContext().getRealPath("/") + basePath + "/images/"+new SimpleDateFormat("yyyyMMdd").format(new Date())+"/";
                 File destFile=new File(uploadPath);
                 if(!destFile.exists()) destFile.mkdirs();
                 OutputStream out =null;
                 try {
                     long start=System.currentTimeMillis();
                     ImageUtils.masterResize(file, new File(uploadPath+newFilename+IMAGE_SUFFIX_SCALE+IMAGE_DEFAULT_EXT), 
                             new File(uploadPath+newFilename+IMAGE_SUFFIX_BLANK+IMAGE_DEFAULT_EXT), 
                             IMAGE_DEFAULT_WIDTH, IMAGE_DEFAULT_WIDTH, IMAGE_DEFAULT_WIDTH, 0.8f);
                 //原图
                 start=System.currentTimeMillis();
                 out = new FileOutputStream(new File(uploadPath+newFilename+IMAGE_SUFFIX_ORIGINAL+IMAGE_DEFAULT_EXT));
                     // file.transferTo(new File(uploadPath+newFilename+IMAGE_SUFFIX_ORIGINAL+IMAGE_DEFAULT_EXT));//原图
                      out.write(file);
                      System.out.println("原图执行耗时 : "+(System.currentTimeMillis()-start)/1000f+" 秒 ");
                     result.put(UPLOAD_CODE, 200);
               result.put(UPLOAD_MESSAGE, request.getContextPath() + "/" + basePath + "/images/"+new SimpleDateFormat("yyyyMMdd").format(new Date())+"/"+newFilename);
                } catch (Exception e) {
                        logger.error(e.getMessage());
                        try {
                            if(out!=null) out.close();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }
                        e.printStackTrace();
                        result.put(UPLOAD_CODE, 500);
                }
    }
        return result;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值