李振海的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
最代码最近下载分享源代码列表最近下载
fantesy  LV17 2021年4月7日
zhanss  LV7 2020年5月15日
pzy12345  LV8 2020年3月30日
我是谁0java  LV11 2019年6月11日
哇哩mxa  LV2 2019年6月1日
仪万里  LV12 2019年5月9日
peppa pig  LV1 2019年4月18日
2399431755  LV12 2019年4月3日
bailihui  LV4 2019年2月20日
skipple3  LV39 2019年2月13日
最代码最近浏览分享源代码列表最近浏览
tianli3000  LV7 2022年2月24日
czr2233  LV9 2021年9月14日
1964867825 2021年9月6日
暂无贡献等级
天天  LV7 2021年9月4日
xiaoxiexie  LV13 2021年4月26日
fantesy  LV17 2021年4月7日
stellafull  LV2 2021年1月17日
helloworldsbsb  LV7 2020年12月28日
mengxiaoyao  LV3 2020年8月4日
dl51918  LV3 2020年6月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友