javaWeb实现裁剪图片上传整套方案

实现思路

  • 使用jcrop插件手机要裁剪图片的坐标
  • 将收集到的参数传递到后台,在后台使用java图形对象绘制图像进行裁剪 
    • 后台处理流程: 
      1、将上传的图片按按照比例进行压缩后上传到文件服务器,并且将压缩后的图片保存在本地临时目录中。 
      2、将压缩后的图片回显到页面,使用jcrop进行裁剪,手机裁剪坐标(x,y,width,height) 
      • @paramx 目标切片起点坐标X
      • @param y 目标切片起点坐标Y
      • @param width 目标切片宽度
      • @param height 目标切片高度 
        3、后台处理裁剪裁剪,重新上传

jsp页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>上传用户头像</title>
    <link href="${pageContext.request.contextPath}/js/cutImg/css/master.css" rel="stylesheet" />
    <link href="${pageContext.request.contextPath}/js/cutImg/css/upload.css" rel="stylesheet" />
    <link href="${pageContext.request.contextPath}/js/cutImg/css/jquery.rollbar.css" rel="stylesheet" />
    <link href="${pageContext.request.contextPath}/js/cutImg/css/jquery.Jcrop.css" rel="stylesheet" />
</head>
<body>
<div class="user_upload">
    <form id="uploadImgFrom" enctype="multipart/form-data" method="post">
        <div class="upload">
            <div class="upload_left">
                <div class="pic">
                    <div class="pic2" id="pic2" >
                        <div id="uploadFile">
                            <div class="upload_btn" style="margin-left: 75px;">
                                <a href="javascript:;" id="upText" style="color:red;">上传头像</a>
                            </div>
                            <p>
                                支持jpg、png格式<br />
                                图片小于2M
                            </p>
                        </div>
                        <!--回显示图片-->
                        <img src="" name="photo" id="originalImg" alt=""/>
                    </div>
                </div>
                <p class="up_reload" id="up_reload" style="display: none;">
                    <a href="javascript:;" style="color: red">重新上传</a>
                </p>
                <input type="hidden" name="picval" id="picval" />
                <input type="hidden" name="origpicval" id="origpicval" value="" />
                <input type="hidden" name="temporigpic" id="temporigpic" />
                <input type="hidden" name="fileID" id="fileID" size="100" />
                <input type="hidden" name="fileUrl" id="fileUrl" size="100" />
                <input type="hidden" name="imgHeight" id="imgHeight" size="100" />
                <input type="hidden" name="imgWidth" id="imgWidth" size="100" />

            </div>
            <div class="upload_right">
                <div class="upload_view_pic" style="width: 300px; height: 230px; border: solid 1px #ccc; overflow: hidden;">
                    <img id="preview_1" src="${pageContext.request.contextPath}/images/4-3.jpg"/>
                </div>
                <p>
                    400 x 300像素
                </p>
                <%--<div class="upload_view_pic" style="width: 80px; height: 80px; border: solid 1px #ccc; overflow: hidden;">
                    <img id="preview_2" src=${pageContext.request.contextPath}/images/4-3.jpg />
                </div>
                <p>
                    80 x 80像素
                </p>--%>
                <p>
                    拖拽或缩放,生成自己满意的头像
                </p>
            </div>
           <%-- <div id="img_description">
                <textarea
            </div>--%>
            <div class="upload_b">
                <font id="upalert"></font>
            </div>
        </div>
        <input type="hidden" name="x" id="x" size="5" />
        <input type="hidden" name="y" id="y" size="5" />
        <input type="hidden" name="w" id="w" size="5" />
        <input type="hidden" name="h" id="h" size="5" />
        <input type="hidden" name="imgUrl" id="imgUrl"/>
        <input type="hidden" name="imgId" id="imgId"/>
        <div class="upload_submit_contain">
            <div class="upload_submit" id="saveImage">
                <a href="javascript:;" style="color: red">保存头像</a>
            </div>
        </div>
    </form>
</div>
</body>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.form.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.validate.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/check.js"></script>
<script src="${pageContext.request.contextPath}/js/cutImg/js/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/cutImg/js/jquery.form.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/cutImg/js/jquery-custom-file-input.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/cutImg/js/jquery.Jcrop.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/cutImg/js/jquery.rollbar.min.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/js/cutImg/js/upload.ui.photos.js" type="text/javascript"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/dialog/dialog.js?lib=false"></script>
<script type="text/javascript">
    photos.JcorpWidth = 400;//裁剪选择框宽度
    photos.JcorpHeight = 300;//裁剪框选择高度
//    photos.Widths = ["200", "80"];// 图片大小“宽”数组,根据图片张数由大到小定义不同图片的宽度
//    photos.Heights = ["200", "80"];// 图片大小“高”数组,根据图片张数由大到小定义不同图片的高度
    photos.UpLoad_FileID = "uploadFile";//上传组件属性ID
    photos.UpLoad_FileTextID = "upText";//上传组件按钮文本ID
    photos.UpLoad_AlertID = "upalert";//提示文本ID
    photos.UpLoad_AlertClassName = "error";//提示文本增加的样式
    photos.Upload_FormID = "uploadImgFrom";//上传组件表单ID
    photos.UpLoad_OriginFileID = "originalImg";//底图图片ID
    photos.UpLoad_ReLoadID = "up_reload";//重新上传组件ID
    photos.UpLoad_PicValueID = "picval";//上传的底图片内容ID
    photos.UpLoad_PicFileID = "fileID";上传图片id
    photos.UpLoad_PicFileUrl = "fileUrl";上传图片url
    photos.UpLoad_PicImgHeight="imgHeight";//上传的底图高
    photos.UpLoad_PicImgWidth="imgWidth";//上传的底图宽

    photos.UpLoad_TempOriginValueID = "temporigpic";//本页面重新上传时的原图片内容ID
    photos.UpLoad_OriginValueID = "origpicval";//其他页面进入时的原图片内容ID
    photos.Upload_RollbarID = "pic2";//自定义滚动条ID
    photos.UpLoad_SaveID = "saveImage";//图片保存按钮ID
    photos.UpLoad_PreviewPreFix = "preview_";//多个不同尺寸小图的ID前缀名称,多个小图ID格式“前缀”+数字编号(从1开始),例:id="photos_preview1" ...
    photos.ParentFileImgID = "imgUrl";//父级图片控件ID
    photos.ParentFileValueID = "imgId";//父级图片地址控件ID
    photos.XID = "x";//x
    photos.YID = "y";//y
    photos.WID = "w";//w
    photos.HID = "h";//h
    photos.UpText_Format = "抱歉,暂只支持 jpg、png、bmp 格式";//图片格式验证文本
    /* photos.UpText_Size = "抱歉,图片大小不能超过2M";*///图片大小验证文本
    /*photos.UpText_Error = "图片上传出错";//上传出错文本*/
    photos.Url_Check = "/Enterprise/filescheck";//文件验证大小的地址
    photos.Url_UpLoad = "/Enterprise/uploadphoto?up=original";//上传图片载入底图的地址
    photos.Url_ReLoad = "";//重新上传的地址
    photos.Url_Save = "/Enterprise/saveimg?up=save";//保存图片的地址
</script>
</html>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130

后台java代码

图片上传工具类
package com.shengya.service.utils;


import com.shengya.service.ImgContants;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import sun.misc.BASE64Encoder;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

/**
 * @author Darcy
 *         Created by Administrator on 2016/6/17.
 */
public class UploadUtils {
    // public final static String RADIOURL = "http://211.102.216.237:8011/fileServer/webapi/Attachments/bio?";
    public final static String RADIOURL = "http://192.168.1.15:8088/fileServer/webapi/Attachments/bio?";

    public final static String URL = ImgContants.IMG_UPLOAD + "/file/UpLoadImage?";
    public final static String FILEURL = ImgContants.IMG_UPLOAD + "/file/UpLoadFile?";
    public final static String VEDIOURL = ImgContants.FILE_UPLOAD + "/file/UpLoadFile?";
    public final static String CROPURL = ImgContants.IMG_UPLOAD + "/file/CropImage?";


    private static byte[] readAsByteArr(InputStream is) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = is.read(bytes)) != -1) {
                baos.write(bytes, 0, length);
            }
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 上传图片
     *
     * @param file
     */
    public static String submitImage(File file) throws UnsupportedEncodingException {
        String fileName = URLEncoder.encode(file.getName(), "UTF-8");
        long fileSize = file.length();
        boolean isReSuffix = true;
        int height = 0;
        int width = 500;
        String mark = "t";
        String mode = "W";
        String url = sign(URL)
                + "&ChannelNo=muying_android"
                + "&FileName="
                + fileName
                + "&FileSize="
                + fileSize
                + "&IsReSuffix="
                + isReSuffix
                + "&Height="
                + height
                + "&Width="
                + width + "&Mark=" + mark + "&Mode=" + mode;
        System.out.println("url:" + url);
        HttpURLConnection conn = null;
        OutputStream outStream = null;
        BufferedInputStream bin = null;
        try {
            bin = new BufferedInputStream(new FileInputStream(file));
            conn = (HttpURLConnection) new URL(url)
                    .openConnection();
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            // conn.setFixedLengthStreamingMode(file.length());
            conn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", file.length()
                    + "");
            // conn.setRequestProperty("Range", "bytes="+"");
            // 设置 HttpURLConnection的字符编码
            conn.setRequestProperty("Accept-Charset", "UTF-8");
            outStream = conn.getOutputStream();

            byte[] buf = new byte[1024];
            int len = 0;
            int lenCount = 0;
            while ((len = bin.read(buf)) != -1) {
                outStream.write(buf, 0, len);//OK
                lenCount = lenCount + len;
            }
            outStream.flush();
            int responseCode = conn.getResponseCode();
            if (responseCode == 200) {
                InputStream inStream = conn.getInputStream();
                byte[] data1 = readAsByteArr(inStream);
                String json = new String(data1);
                inStream.close();
                return json;
            } else {
                InputStream inStream = conn.getInputStream();
                byte[] data1 = readAsByteArr(inStream);
                String json = new String(data1);
                inStream.close();
                return json;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outStream != null) {
                    outStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 上传图片
     *
     * @param file
     */
    public static String submitWebImage(MultipartFile file) throws UnsupportedEncodingException {
        String fileName = URLEncoder.encode(file.getOriginalFilename(), "UTF-8");
        long fileSize = file.getSize();
        boolean isReSuffix = true;
        int height = 0;
        int width = 500;
        String mark = "t";
        String mode = "W";
        String url = sign(URL)
                + "&ChannelNo=muying_android"
                + "&FileName="
                + fileName
                + "&FileSize="
                + fileSize
                + "&IsReSuffix="
                + isReSuffix
                + "&Height="
                + height
                + "&Width="
                + width + "&Mark=" + mark + "&Mode=" + mode;
        System.out.println("url:" + url);
        HttpURLConnection conn = null;
        OutputStream outStream = null;
        BufferedInputStream bin = null;
        try {
            bin = new BufferedInputStream(file.getInputStream());
            conn = (HttpURLConnection) new URL(url)
                    .openConnection();
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            // conn.setFixedLengthStreamingMode(file.length());
            conn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", file.getSize()
                    + "");
            // conn.setRequestProperty("Range", "bytes="+"");
            // 设置 HttpURLConnection的字符编码
            conn.setRequestProperty("Accept-Charset", "UTF-8");
            outStream = conn.getOutputStream();

            byte[] buf = new byte[1024];
            int len = 0;
            int lenCount = 0;
            while ((len = bin.read(buf)) != -1) {
                outStream.write(buf, 0, len);//OK
                lenCount = lenCount + len;
            }
            outStream.flush();
            int responseCode = conn.getResponseCode();
            if (responseCode == 200) {
                InputStream inStream = conn.getInputStream();
                byte[] data1 = readAsByteArr(inStream);
                String json = new String(data1);
                inStream.close();
                return json;
            } else {
                InputStream inStream = conn.getInputStream();
                byte[] data1 = readAsByteArr(inStream);
                String json = new String(data1);
                inStream.close();
                return json;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outStream != null) {
                    outStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 上传视频
     *
     * @param file
     */
    public static String submitRadio(MultipartFile file) throws Exception {
        String url = signRadio(RADIOURL);
        System.out.println("url:" + url);
        HttpURLConnection conn = null;
        OutputStream outStream = null;
        BufferedInputStream bin = null;
        try {
            bin = new BufferedInputStream(file.getInputStream());
            conn = (HttpURLConnection) new URL(url)
                    .openConnection();
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", file.getSize()
                    + "");
            conn.setRequestProperty("Accept-Charset", "UTF-8");
            outStream = conn.getOutputStream();

            byte[] buf = new byte[1024];
            int len = 0;
            int lenCount = 0;
            while ((len = bin.read(buf)) != -1) {
                outStream.write(buf, 0, len);//OK
                lenCount = lenCount + len;
            }
            outStream.flush();
            int responseCode = conn.getResponseCode();
            if (responseCode == 200) {
                InputStream inStream = conn.getInputStream();
                byte[] data1 = readAsByteArr(inStream);
                String json = new String(data1);
                inStream.close();
                return json;
            } else {
                InputStream inStream = conn.getInputStream();
                byte[] data1 = readAsByteArr(inStream);
                String json = new String(data1);
                inStream.close();
                return json;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outStream != null) {
                    outStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 上传简历快照
     *
     * @param fileName
     * @param content
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String submitFile(String fileName, String content) throws UnsupportedEncodingException {
        //content = URLEncoder.encode(content, "UTF-8");
        fileName = URLEncoder.encode(fileName, "UTF-8");
        byte[] contentBytes = content.getBytes();
        long fileSize = contentBytes.length;
        boolean isReSuffix = true;
        int height = 0;
        int width = 500;
        String mark = "t";
        String mode = "W";
        String url = sign(FILEURL)
                + "&ChannelNo=muying_android"
                + "&FileName="
                + fileName
                + "&FileSize="
                + fileSize
                + "&IsReSuffix="
                + isReSuffix
                + "&Height="
                + height
                + "&Width="
                + width + "&Mark=" + mark + "&Mode=" + mode;
        System.out.println("url:" + url);
        HttpURLConnection conn = null;
        OutputStream outStream = null;
        BufferedInputStream bin = null;
        try {
            conn = (HttpURLConnection) new URL(url)
                    .openConnection();
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            // conn.setFixedLengthStreamingMode(file.length());
            conn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", content.length()
                    + "");
            // conn.setRequestProperty("Range", "bytes="+"");
            // 设置 HttpURLConnection的字符编码
            conn.setRequestProperty("Accept-Charset", "UTF-8");
            outStream = conn.getOutputStream();
            outStream.write(contentBytes, 0, contentBytes.length);//OK
            outStream.flush();
            int responseCode = conn.getResponseCode();
            if (responseCode == 200) {
                InputStream inStream = conn.getInputStream();
                byte[] data1 = readAsByteArr(inStream);
                String json = new String(data1);
                inStream.close();
                return json;
            } else {
                InputStream inStream = conn.getInputStream();
                byte[] data1 = readAsByteArr(inStream);
                String json = new String(data1);
                inStream.close();
                return json;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outStream != null) {
                    outStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }




    /**
     * 上传附件
     *
     * @return json 字符串
     * @throws UnsupportedEncodingException
     */
    public static String submitFile(MultipartFile file) throws UnsupportedEncodingException {
        String fileName = URLEncoder.encode(file.getOriginalFilename(), "UTF-8");
        long fileSize = file.getSize();
        boolean isReSuffix = true;
        int height = 0;
        int width = 500;
        String mark = "t";
        String mode = "W";
        String url = sign(FILEURL)
                + "&ChannelNo=muying_android"
                + "&FileName="
                + fileName
                + "&FileSize="
                + fileSize
                + "&IsReSuffix="
                + isReSuffix
                + "&Height="
                + height
                + "&Width="
                + width + "&Mark=" + mark + "&Mode=" + mode;
        System.out.println("url:" + url);
        HttpURLConnection conn = null;
        OutputStream outStream = null;
        BufferedInputStream bin = null;
        try {
            bin = new BufferedInputStream(file.getInputStream());
            conn = (HttpURLConnection) new URL(url)
                    .openConnection();
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            // conn.setFixedLengthStreamingMode(file.length());
            conn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", file.getSize()
                    + "");
            // conn.setRequestProperty("Range", "bytes="+"");
            // 设置 HttpURLConnection的字符编码
            conn.setRequestProperty("Accept-Charset", "UTF-8");
            outStream = conn.getOutputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            int lenCount = 0;
            while ((len = bin.read(buf)) != -1) {
                outStream.write(buf, 0, len);//OK
                lenCount = lenCount + len;
            }
            int responseCode = conn.getResponseCode();
            if (responseCode == 200) {
                InputStream inStream = conn.getInputStream();
                byte[] data1 = readAsByteArr(inStream);
                String json = new String(data1);
                inStream.close();
                return json;
            } else {
                InputStream inStream = conn.getInputStream();
                byte[] data1 = readAsByteArr(inStream);
                String json = new String(data1);
                inStream.close();
                return json;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outStream != null) {
                    outStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    private static String sign(String baseurl) throws UnsupportedEncodingException {
        long currebttime = System.currentTimeMillis();
        long timeStamp = currebttime / 1000;
        String data = "ActionName=UpLoadImage" + "&SecretKey=4b19f08dbf0adb82ce9cc7c207ec1dc9"
                + "&TimeStamp=" + timeStamp;
        String signature = hmac_sha1("4b19f08dbf0adb82ce9cc7c207ec1dc9",
                URLEncoder.encode(data, "UTF-8"));
        signature = signature.replace("%3D", "%3d");
        signature = signature.replace("%3A", "%3a");
        signature = signature.replace("%2F", "%2f");
        return baseurl + data + "&Signature=" + signature;

    }

    private static String signRadio(String url) throws Exception {
        long currebttime = System.currentTimeMillis();
        String data = "secretKey=f1b79c865c424be46183a2f0a49a0f15"
                + "&timeStamp=" + currebttime;
        System.out.println("url:" + url + data);
        System.out.println("url = " + URLEncoder.encode(url + data, "utf-8"));
        String signature = HmacSha1Util.getSignature(URLEncoder.encode(url + data, "utf-8"), "f1b79c865c424be46183a2f0a49a0f15");
        return url + data + "&signature=" + signature;

    }

    private static String hmac_sha1(String key, String datas) {
        String reString = "";
        try {
            datas = datas.replace("%3A", "%3a");
            datas = datas.replace("%2F", "%2f");
            datas = datas.replace("%3D", "%3d");
            byte[] data = key.getBytes("UTF-8");
            // 根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称
            SecretKey secretKey = new SecretKeySpec(data, "HmacSHA1");
            // 生成一个指定 Mac 算法 的 Mac 对象
            Mac mac = Mac.getInstance("HmacSHA1");
            // 用给定密钥初始化 Mac 对象
            mac.init(secretKey);
            byte[] text = datas.getBytes("UTF-8");
            // 完成 Mac 操作
            byte[] text1 = mac.doFinal(text);
            reString = new BASE64Encoder().encodeBuffer(text1);
            reString = URLEncoder.encode(reString, "UTF-8");
            reString = reString.replace("%0A", "");
            reString = reString.replace("%0D", "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return reString;
    }

    /**
     * 上传裁剪图片
     * zxiao 2016/09/01
     *
     * @param fileID
     */
    public static String submitCutedWebImage(MultipartFile file,String fileID,String x,String y,String w,String h) throws UnsupportedEncodingException {
        String FileId=fileID;
        String[] CropWidths=new String[]{"50"};//裁剪宽度
        //List<String> list=new List<String>();
        //List<String> list = new ArrayList<String>(5);
        ArrayList<String>  listCropWidths = new ArrayList<String>();
        listCropWidths.add("400");

//        String[] CropHeights=new String[]{"50"};//裁剪高度
        ArrayList<String>  listCropHeights = new ArrayList<String>();
        listCropHeights.add("300");

        int CropX=Integer.parseInt(x);//裁剪x坐标
        int CropY=Integer.parseInt(y);//裁剪y坐标

//        String[] CropFixs=new String[]{"l"};//裁剪图片的标示集合
        ArrayList<String>  listCropFixs = new ArrayList<String>();
        listCropFixs.add("l");


        String SourceSuffix="jpg";//底图后缀
        String SourceFix="";//底图标示

       /* boolean isReSuffix = true;
        int height = 1000;
        int width = 750;
        String mark = "t";
        String mode = "W";*/
        String url = sign(CROPURL) + "FileId="
                + fileID
                + "&CropWidths="
                + listCropWidths
                + "&CropHeights="
                + listCropHeights
                + "&CropX="
                + CropX
                + "&CropY="
                + CropY + "&CropFixs=" + listCropFixs + "&SourceSuffix=" + SourceSuffix+ "&SourceFix=" + SourceFix;
        System.out.println("url:" + url);
        HttpURLConnection conn = null;
        OutputStream outStream = null;
        BufferedInputStream bin = null;
        try {
            bin = new BufferedInputStream(file.getInputStream());
            conn = (HttpURLConnection) new URL(url)
                    .openConnection();
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            // conn.setFixedLengthStreamingMode(file.length());
            conn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            conn.setRequestProperty("Content-Length", file.getSize()
                    + "");
            // conn.setRequestProperty("Range", "bytes="+"");
            // 设置 HttpURLConnection的字符编码
            conn.setRequestProperty("Accept-Charset", "UTF-8");
            outStream = conn.getOutputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            int lenCount = 0;
            while ((len = bin.read(buf)) != -1) {
                outStream.write(buf, 0, len);//OK
                lenCount = lenCount + len;
            }
            int responseCode = conn.getResponseCode();
            if (responseCode == 200) {
                InputStream inStream = conn.getInputStream();
                byte[] data1 = readAsByteArr(inStream);
                String json = new String(data1);
                inStream.close();
                return json;
            } else {
                InputStream inStream = conn.getInputStream();
                byte[] data1 = readAsByteArr(inStream);
                String json = new String(data1);
                inStream.close();
                return json;
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (outStream != null) {
                    outStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**上传裁剪后图片 需在本地保存副本
     *zxiao 2016/09/01
     * @param ,
     * @param x
     * @param y
     * @param w
     * @param h
     * @return
     * @throws IOException
     */
    public static String submitCropImage(MultipartFile file,Integer x,Integer y,Integer w,Integer h) throws IOException {
        String folder=System.getProperty("java.io.tmpdir");
        File tempDir =new File(folder);
        //如果文件夹不存在则创建
        if  (!tempDir .exists()  && !tempDir .isDirectory())
        {
//            System.out.println("//不存在");
            tempDir .mkdir();
        }
        //压缩后临时文件
        String Srcpath=folder+"scaleTemp.jpg";

        //获取文件的后缀
        /*File uploadFile = new File(Srcpath);
        InputStream inputStream = file.getInputStream();
        FileInputStream fs= (FileInputStream) inputStream;
        String srcImg = URLEncoder.encode(file.getOriginalFilename(), "UTF-8");
        String suffix=srcImg.substring(srcImg.lastIndexOf(".")+1);*/

        //裁剪图片
        cutImgUtils o = new cutImgUtils(x,y,w,h);
        o.setSrcpath(Srcpath);
        o.setSubpath(folder+"uptemp.jpg");
        File tempFile = o.cut("jpg");
        String s = submitImage(tempFile);
        return s;
    }


    /**
     * 获取上传图片的宽
     * zxiao 2016/09/09
     * @param file
     * @return
     */
    public static int getImgWidth(MultipartFile file) throws IOException {
        InputStream is = null;
        BufferedImage src = null;
        int ret = -1;
        try {
            is = file.getInputStream();
            src = javax.imageio.ImageIO.read(is);
            ret = src.getWidth(null); // 得到源图宽
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 获取上传图片的高
     * zxiao 2016/09/09
     * @param file
     * @return
     */
    public static int getImgHeight(MultipartFile file) {
        InputStream is = null;
        BufferedImage src = null;
        int ret = -1;
        try {
            is = file.getInputStream();
            src = javax.imageio.ImageIO.read(is);
            ret = src.getHeight(null); // 得到源图高
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 缩放图像(按高度和宽度缩放)
     * @param file 文件对象
     * @param result 缩放后的图像地址
     * @param height 缩放后的高度
     * @param width 缩放后的宽度
     * @param bb 比例不对时是否需要补白:true为补白; false为不补白;
     */
    public static void scale(MultipartFile file, String result, int height, int width, boolean bb) {
        try {
            double ratio = 0.0; // 缩放比例
            /*CommonsMultipartFile cf= (CommonsMultipartFile)file;
            DiskFileItem fi = (DiskFileItem)cf.getFileItem();
            File f = fi.getStoreLocation();*/
//            File f = new File(srcImageFile);
            InputStream inputStream = file.getInputStream();
            BufferedImage bi = ImageIO.read(inputStream);
            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 (bb) {//补白
                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((BufferedImage) itemp, "jpg", new File(result));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) {
        try {
           /* File file = new File("E:\\bole\\doc\\服务接口文档\\伯乐服务接口说明v1.0.docx");
            System.out.println("file = " + file);
            String json = submitFile(file);
            System.out.println("json = " + json);*/
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

图片裁剪工具类
package com.shengya.service.utils;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;

/**
 * Created by Administrator on 2016/9/1.
 */
public class cutImgUtils {
    // ===源图片路径名称如:c:\1.jpg
    private String srcpath;

    // ===剪切图片存放路径名称。如:c:\2.jpg
    private String subpath;
    public void setSrcpath(String srcpath) {
        this.srcpath = srcpath;
    }
    public void setSubpath(String subpath) {
        this.subpath = subpath;
    }
    // ===剪切点x坐标
    private int x;

    private int y;

    // ===剪切点宽度
    private int width;
    private int height;

    public cutImgUtils() {
    }

    public cutImgUtils(int x, int y, int width, int height) {
        if(x<0){
            x=0;
        }else{
            this.x = x;
        }
        if(y<0){
            y=0;
        }else{
            this.y = y;
        }
        this.width = width;
        this.height = height;
    }

    /**
     *
     * 对图片裁剪,并把裁剪完蛋新图片保存 。
     */

    public File cut() throws IOException {
        FileInputStream is = null;
        ImageInputStream iis = null;
        try {
            // 读取图片文件
            is = new FileInputStream(srcpath);

            /**
             *
             * 返回包含所有当前已注册 ImageReader 的 Iterator,这些 ImageReader
             *
             * 声称能够解码指定格式。 参数:formatName - 包含非正式格式名称 .
             *
             * (例如 "jpeg" 或 "tiff")等 。
             */
            Iterator<ImageReader> it = ImageIO
                    .getImageReadersByFormatName("jpg");

            ImageReader reader = it.next();

            // 获取图片流
            iis = ImageIO.createImageInputStream(is);

            /**
             *
             * <p>
             * iis:读取源。true:只向前搜索
             * </p>
             * .将它标记为 ‘只向前搜索’。
             *
             * 此设置意味着包含在输入源中的图像将只按顺序读取,可能允许 reader
             *
             * 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分。
             */
            reader.setInput(iis, true);

            /**
             *
             * <p>
             * 描述如何对流进行解码的类
             * <p>
             * .用于指定如何在输入时从 Java Image I/O
             *
             * 框架的上下文中的流转换一幅图像或一组图像。用于特定图像格式的插件
             *
             * 将从其 ImageReader 实现的 getDefaultReadParam 方法中返回
             *
             * ImageReadParam 的实例。
             */
            ImageReadParam param = reader.getDefaultReadParam();

            /**
             *
             * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象
             *
             * 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。
             */
            Rectangle rect = new Rectangle(x, y, width, height);

            // 提供一个 BufferedImage,将其用作解码像素数据的目标。
            param.setSourceRegion(rect);

            /**
             *
             * 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象,并将
             *
             * 它作为一个完整的 BufferedImage 返回。
             */
            BufferedImage bi = reader.read(0, param);

            // 保存新图片
            ImageIO.write(bi, "jpg", new File(subpath));
            File file = new File("subpath");
            return file;
        } finally {
            if (is != null)
                is.close();
            if (iis != null)
                iis.close();
        }

    }


    public File cut(String suffix) throws IOException {
        FileInputStream is = null;
        ImageInputStream iis = null;
        try {
            // 读取图片文件
            is = new FileInputStream(srcpath);
//             is = inputStream;
            /**
             *
             * 返回包含所有当前已注册 ImageReader 的 Iterator,这些 ImageReader
             *
             * 声称能够解码指定格式。 参数:formatName - 包含非正式格式名称 .
             *
             * (例如 "jpeg" 或 "tiff")等 。
             * Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName("jpg");
             */
        String suffixName=null;
            if(suffix.equals("gif")){
                suffixName="gif";
            }else  if(suffix.equals("png")){
                suffixName="png";
            } else{
                suffixName="jpg";
            }
            Iterator<ImageReader> it = ImageIO
                    .getImageReadersByFormatName(suffixName);
            ImageReader reader = it.next();

            // 获取图片流
            iis = ImageIO.createImageInputStream(is);

            /**
             *
             * <p>
             * iis:读取源。true:只向前搜索
             * </p>
             * .将它标记为 ‘只向前搜索’。
             *
             * 此设置意味着包含在输入源中的图像将只按顺序读取,可能允许 reader
             *
             * 避免缓存包含与以前已经读取的图像关联的数据的那些输入部分。
             */
            reader.setInput(iis, true);

            /**
             *
             * <p>
             * 描述如何对流进行解码的类
             * <p>
             * .用于指定如何在输入时从 Java Image I/O
             *
             * 框架的上下文中的流转换一幅图像或一组图像。用于特定图像格式的插件
             *
             * 将从其 ImageReader 实现的 getDefaultReadParam 方法中返回
             *
             * ImageReadParam 的实例。
             */
            ImageReadParam param = reader.getDefaultReadParam();

            /**
             *
             * 图片裁剪区域。Rectangle 指定了坐标空间中的一个区域,通过 Rectangle 对象
             *
             * 的左上顶点的坐标(x,y)、宽度和高度可以定义这个区域。
             */

            Rectangle rect = new Rectangle(x, y, width, height);

            // 提供一个 BufferedImage,将其用作解码像素数据的目标。
            param.setSourceRegion(rect);

            /**
             *
             * 使用所提供的 ImageReadParam 读取通过索引 imageIndex 指定的对象,并将
             *
             * 它作为一个完整的 BufferedImage 返回。
             */
            BufferedImage bi = reader.read(0, param);

            // 保存新图片
            ImageIO.write(bi, "jpg", new File(subpath));
            File file = new File(subpath);
            return file;
        } finally {
            if (is != null)
                is.close();
            if (iis != null)
                iis.close();
        }

    }

}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696
  • 697
  • 698
  • 699
  • 700
  • 701
  • 702
  • 703
  • 704
  • 705
  • 706
  • 707
  • 708
  • 709
  • 710
  • 711
  • 712
  • 713
  • 714
  • 715
  • 716
  • 717
  • 718
  • 719
  • 720
  • 721
  • 722
  • 723
  • 724
  • 725
  • 726
  • 727
  • 728
  • 729
  • 730
  • 731
  • 732
  • 733
  • 734
  • 735
  • 736
  • 737
  • 738
  • 739
  • 740
  • 741
  • 742
  • 743
  • 744
  • 745
  • 746
  • 747
  • 748
  • 749
  • 750
  • 751
  • 752
  • 753
  • 754
  • 755
  • 756
  • 757
  • 758
  • 759
  • 760
  • 761
  • 762
  • 763
  • 764
  • 765
  • 766
  • 767
  • 768
  • 769
  • 770
  • 771
  • 772
  • 773
  • 774
  • 775
  • 776
  • 777
  • 778
  • 779
  • 780
  • 781
  • 782
  • 783
  • 784
  • 785
  • 786
  • 787
  • 788
  • 789
  • 790
  • 791
  • 792
  • 793
  • 794
  • 795
  • 796
  • 797
  • 798
  • 799
  • 800
  • 801
  • 802
  • 803
  • 804
  • 805
  • 806
  • 807
  • 808
  • 809
  • 810
  • 811
  • 812
  • 813
  • 814
  • 815
  • 816
  • 817
  • 818
  • 819
  • 820
  • 821
  • 822
  • 823
  • 824
  • 825
  • 826
  • 827
  • 828
  • 829
  • 830
  • 831
  • 832
  • 833
  • 834
  • 835
  • 836
  • 837
  • 838
  • 839
  • 840
  • 841
  • 842
  • 843
  • 844
  • 845
  • 846
  • 847
  • 848
  • 849
  • 850
  • 851
  • 852
  • 853
  • 854
  • 855
  • 856
  • 857
  • 858
  • 859
  • 860
  • 861
  • 862
  • 863
  • 864
  • 865
  • 866
  • 867
  • 868
  • 869
  • 870
  • 871
  • 872
  • 873
  • 874
  • 875
  • 876
  • 877
  • 878
  • 879
  • 880
  • 881
  • 882
  • 883
  • 884
  • 885
  • 886
  • 887
  • 888
  • 889
  • 890
  • 891
  • 892
  • 893
  • 894
  • 895
  • 896
  • 897
  • 898
  • 899
  • 900
  • 901
  • 902
  • 903
  • 904
  • 905
  • 906
  • 907
  • 908
  • 909
  • 910
  • 911
  • 912
  • 913
  • 914
  • 915
  • 916
  • 917
  • 918
  • 919
  • 920
  • 921
  • 922
  • 923
  • 924
  • 925
  • 926
  • 927
  • 928
  • 929
  • 930
  • 931
  • 932
  • 933
  • 934
  • 935
  • 936
  • 937
  • 938
  • 939
  • 940
  • 941
  • 942
  • 943
  • 944
  • 945
  • 946
  • 947
  • 948
  • 949
  • 950
  • 951
  • 952
  • 953
  • 954
  • 955
  • 956
  • 957
  • 958
  • 959
  • 960
  • 961
  • 962
  • 963
  • 964
  • 965
  • 966
  • 967
  • 968
  • 969
  • 970
  • 971
  • 972
  • 973
  • 974
  • 975
  • 976
  • 977
  • 978
  • 979
  • 980
  • 981
  • 982
  • 983
  • 984
  • 985
  • 986
  • 987
  • 988
  • 989
  • 990
  • 991

遇到的问题总结:

-裁剪图片发生偏移现象 
解决办法:使用压缩后的图片等比例收集裁切坐标信息 
-MultipartFile 和 File之间相互转化

第一种方法:

     MultipartFile file = xxx; 
        CommonsMultipartFile cf= (CommonsMultipartFile)file; 
        DiskFileItem fi = (DiskFileItem)cf.getFileItem(); 

        File f = fi.getStoreLocation();

  会在项目的根目录的临时文件夹下生成一个文件;

第二种方法:

    transferTo(File dest);

  会在项目中生成一个新文件;

第三种方法:   

  File f = (File) xxx 强转即可。前提是要配置multipartResolver,要不然会报类型转换失败的异常。

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="104857600"/>
        <property name="maxInMemorySize" value="4096"/>
    </bean>
  没试过;
第四种方法:
  Workbook wb = Workbook.getWorkbook(xxx .getInputStream());
  转换为输入流,直接读取;
第五种方法:
  byte[] buffer = myfile.getBytes();
  先转换为字节数组,没试过;
第六种方法:
直接转成输入流
MultipartFile file=xxx;
 is = file.getInputStream();
 src = javax.imageio.ImageIO.read(is);
 ret = src.getWidth(null); // 得到源图宽


 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

参考资料: 
jcop api:http://code.ciaoca.com/jquery/jcrop/.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值