李振海的gravatar头像
李振海2017-09-29 12:01:17

Spring MVC+ajax+base64+amazeui框架上传头像带裁剪功能

Spring MVC+ajax+base64+amazeui框架上传头像带裁剪功能

后台关键代码

String filePath = null;

String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();

filePath = session.getServletContext().getRealPath("/upload");

//不存在就创建

File file = new File(filePath);

if (!file.exists() && !file.isDirectory()) {

System.out.println("文件夹不存在");

file.mkdir();

}

filePath = filePath + "\\" + uuid + ".jpg";

try {

//俩种方式解密Base64 后者更简单

/* // Base64

byte[] b;

b = decoder.decodeBuffer(imgStr.split(",")[1]);



for (int i = 0; i < b.length; ++i) {

if (b[i] < 0) {

b[i] += 256;

}

}

*/

//因为BASE64Decoder的jar问题,此处使用spring框架提供的工具包

            byte[] b = Base64Utils.decodeFromString(imgStr.split(",")[1]);

System.out.println(filePath);

OutputStream out = new FileOutputStream(filePath);

out.write(b);

out.flush();

out.close();

} catch (Exception e) {

String json = "{\"result\":\"服务器异常\"}";

return json;

}

String imgePath = "upload/" + uuid + ".jpg";

String json = "{\"result\":\"ok\",\"file\":\""+imgePath+"\"}";

System.out.println(json);

return json;

}

上传成功图

Spring MVC+ajax+base64+amazeui框架上传头像带裁剪功能

项目结构

Spring MVC+ajax+base64+amazeui框架上传头像带裁剪功能

 

SpringMVC上传文件配置

<bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<property name="defaultEncoding" value="utf-8" />

<property name="maxUploadSize" value="1024000" />

<property name="resolveLazily" value="true" />

<!-- <property name="maxInMemorySize" value="40960" /> -->

</bean>

注意springmvc的静态资源配置,


打赏

已有2人打赏

最代码客服的gravatar头像最代码官方的gravatar头像

文件名:ImgUpload.zip,文件大小:9183.749K下载
  • /
      • /ImgUpload
        • /ImgUpload/.classpath
        • /ImgUpload/.project
          • /ImgUpload/.settings
            • /ImgUpload/.settings/.jsdtscope
            • /ImgUpload/.settings/com.genuitec.eclipse.core.prefs
            • /ImgUpload/.settings/org.eclipse.jdt.core.prefs
            • /ImgUpload/.settings/org.eclipse.wst.common.component
            • /ImgUpload/.settings/org.eclipse.wst.common.project.facet.core.xml
            • /ImgUpload/.settings/org.eclipse.wst.jsdt.ui.superType.container
顶部客服微信二维码底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友